On Tue, May 29, 2001 at 05:29:12PM -0500, hagel@comp.tamu.edu wrote: > > Is there a g++ equivalent of $FORTRAN/CHECK=BOUNDS? > The short answer is: no The long answer is: yes (but note that g77 does have "-fbounds-check") Let me explain- the regular "C-style" arrays are not arrays at all- they are merely pointers to the first element of the array. The size information is not available- it is not automatically saved and passed around when you use these so called arrays. So it is not possible to do bounds checking- there is nothing to check against. Hence the short answer- no, there is no bounds checking for C and C++ arrays. The better C and C++ programs cope with this by hand by explicitely passing around the array sizes and by using "assert()" to check the bounds in strategic places. There have been attempts to automate this, for example, by extending the C compiler to catch things like "int a[10]; a[25]=0;", but the definition of the C langage (and, by extension, C++) makes it hard-to-impossible to make it generally useful and bullet-proof as in Fortran, except by breaking binary compatibility or by deviating from the C/C++ ANSI/ISO language definition. In C++, however, you can define an "array" object, that would look and behave like a C-style array (i.e. will have the "[]" operators, etc) and will do bounds-checking behind the scenes. I have seen toy implementations of such objects in C++ books, and the STL contains a number of similar array-type objects. Reportedly, using current compilers, such objects are almost as efficient as the built-in C-style arrays. > > I do not find it and I find it absolutely archaic and rediculous that in > Y2k and beyond that we have regressed so far that our stupid compilers > cannot check for bounds overflows if we so desire. VAX fortran did that > since 1979!!!!! That is probably the single most common problem that > stops us cold and we have no way of diagnosing it. Going from VMS to > unix is bad enough. This, if true, gets really rediculous. > It does reflect on the sorry fact that the dominant programming languages, C and C++, are by design not safe and by design prone to extremely hard to find and debug errors. > > P. S. Hopefully someone will make a fool of me and tell me how to do it > instead of admitting that no, g++ can't do it. I will gladly be taken > for a fool in that case. > Yes, Virginia, C++ can do it. -- Konstantin Olchanski olchansk@panix.com
This archive was generated by hypermail 2b29 : Tue May 29 2001 - 19:34:45 EDT