From: XXguille AT XXiies DOT XXes (Guillermo Rodriguez Garcia) Newsgroups: comp.os.msdos.djgpp Subject: Re: ? array[n] or array[n+1] ? Date: Tue, 11 May 1999 11:43:42 GMT Organization: Telefonica Transmision de Datos Lines: 63 Message-ID: <37390d5c.2690408@noticias.iies.es> References: <37367f71 DOT 6271443 AT news DOT wanadoo DOT fr> <3736A098 DOT 55E268BE AT softhome DOT net> <373c0195 DOT 3902906 AT news DOT wanadoo DOT fr> <3737f542 DOT 0 AT nnrp1 DOT news DOT uk DOT psi DOT net> NNTP-Posting-Host: iies223.iies.es Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Newsreader: Forte Agent 1.5/32.451 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com El día Tue, 11 May 1999 10:19:16 +0100, "Ya'qub" escribió: >Greetings, > With all due respect, this explanation does not seem very convincing. >Surely, the compiler *should* complain when you do such a thing as A C compiler usually doesn't know if you will overrun an array (sometimes it is just impossible to know this at compile time), but even if the compiler knew, it would assume that you really *want* to do that. And indeed, sometimes it is done! I think I've seen some zero-length arrays in structures, where the size of the array isn't actually known until run time, and for some reason an equivalent pointer to is not appropiate. Now you could have runtime checks for array bounds, uninitialized memory, etc., but the C language was designed to trust the programmer so that you can freely do almost whatever you want. It is up to you if your code crashes and your computer burns. If you want those checks, just go for Pascal or Java. >overrunning an array. I hoped that the compiler would help such a poor >programmer as myself in catching such bugs which I suppose could cause quite >a lot of headache to locate. Can anybody else confirm that gcc makes such >assumptions that we know what we are doing and let us carry on regardless. Not just gcc; almost every C compiler will. >Also, out of curiosity, if you do overrun an array and the program has >compiled without any warning or error, is there anything that can be said >about its execution? i.e. do we know what is going to happen when the >program tries to access an element from the array beyond its length. Thanks >Regards, According to ANSI, the behaviour is undefined. That means that anything could happen. If you overwrite some system data or unitialized memory, probably your program will crash with a GPF, core dump, system exception or whatever. OTOH, if you just overwrite *your* data, your program could even run normally, and the bug would go unnoticed! For example, try this: it will probably work (I haven't tried it), but then, that doesn't mean it is correct! #include int main() { int array1[10]; int array2[10]; array1[15] = 23; printf("array1[15] = %i\n", array1[15]); return 0; } Regards, GUILLE ---- Guillermo Rodriguez Garcia XXguille AT XXiies DOT XXes (ya sabes :-)