Author: David Rasmussen
Date: 18:04:24 01/18/02
Go up one level in this thread
On January 18, 2002 at 20:23:25, David Hanley wrote: > >The simple answer is that something like that is hard/impossible to do in C. An >array is simply a pointer to some memory. There's no really *good* way to tell >how large that chunk of memory is. If the C compiler is using certian kind of >heap structures you could determine the size of the chunk of memory from the >heap structure. BUT even if you could do that, if you incremented the pointer >so that it was inside another chunk of memory, it would look at the size of >*that* chunk, and the bounds check would be OK. > >The the pascal you used, arrays were really arrays, and carried around a size >tag with them. > >You can do some sorts of bounds checking in C with some tools, but it *CANNOT* >catch everything or even many cases. > >I was using STL recently and i was surperised that vectors don't have bounds >checking. I would think it would be in there as a switch, at least. > >dave I know that you can't do it 100%. But there are a lot of cases where you could do it. And anyway, it would only be a tool. There could of course be no guarantee that it worked correctly. But if I have declared an array statically in C (in my case it is C++), like this: int a[10]; int i = ...; a[i] = 42; or struct S { int a[10]; }; S s; s.a[i] = 42; then a is a constant pointer, not just a pointer. And the compiler could very well assume (at least if I told it to), that array accesses via a cannot be outside bounds. You can write all sorts of code that would make this not work, but using such a feature would be the programmers responsibility. /David
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.