Re: [Dev-C++] Why thisC example don't presents the correct result ?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Paul I. <pi...@ot...> - 2003-12-11 21:39:33
|
Quoting hu...@cl...: =09 > Hi all,=20 > in others compilers this is the result :=20 > =09 > 10=20 > 200=20 > =09 > of this program :=20 > #include=20 > main <http://pcp.clix.pt/images/canais/webmail2/smile/kiss.gif>=20 > {=20 > int v[3],i;=20 > v[0]=3D10;=20 > v[1]=3D200;=20 > i=3D0;=20 > while(v[i]!=3D'\0')=20 > {=20 > printf("%d\n",v[i]);=20 > i++;=20 > }=20 > }=20 > =09 > in dev-c++compiler i have the this result :=20 > 10=20 > 200=20 > 2293664=20 > 4198592=20 > 4210688=20 > 4210692=20 > 2293664=20 > 4198757=20 > 1=20 > 4138088=20 > 4139640=20 > 4198553=20 > =09 > =09 > Why ?????=20 > =09 > Thanks.=20 >=20 > -- > Hugo Dias > =20 Well, to me it seems that the explanation is that GCC stores the variable= s declared in main on the stack! If you put the declaration of the v vector outside main, the program will "work" just as with the other compilers. T= his probably is because windoze will clear the memory (setting to zero) alloc= ated to the program before loading and executing it. The memory occupied by the s= tack is cleared just like all the rest, only it is messed up by some initializati= on code executed before main starts executing. If you put the same code in some other function than main it won't work (= except by chance) regardless of which compiler you use. A way to verify this behavior is to put "int v[3000000]" outside of main = and inside. And yes, the program crashes if you put the declaration inside ma= in, because windows don't allow you to allocate that much (at once) on the st= ack. My guess is that the other compilers treat declarations inside main as if th= ey were put outside of main (go ahead and try).=20 Note that the clearing of memory before loading and executing the program= is not part of the C/C++ standards but is dependent on the OS. Visal C++ (when debugging) even initialize memory to 0xaa (or was it 0x55?) to help detec= ting this kind of bugs.=20 /Paul I |