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: <age...@co...> - 2003-12-13 02:08:38
|
Also, v[2] is uninitialized. That could lead to a problem. And be careful with C standard code. It is almost obsolete. -- ~~Scott Simontis~~ Programmer > Quoting hu...@cl...: > > > Hi all, > > in others compilers this is the result : > > > > 10 > > 200 > > > > of this program : > > #include > > main <http://pcp.clix.pt/images/canais/webmail2/smile/kiss.gif> > > { > > int v[3],i; > > v[0]=10; > > v[1]=200; > > i=0; > > while(v[i]!='\0') > > { > > printf("%d\n",v[i]); > > i++; > > } > > } > > > > in dev-c++compiler i have the this result : > > 10 > > 200 > > 2293664 > > 4198592 > > 4210688 > > 4210692 > > 2293664 > > 4198757 > > 1 > > 4138088 > > 4139640 > > 4198553 > > > > > > Why ????? > > > > Thanks. > > > > -- > > Hugo Dias > > > > > Well, to me it seems that the explanation is that GCC stores the variables > 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. This > probably is because windoze will clear the memory (setting to zero) allocated to > the program before loading and executing it. The memory occupied by the stack is > cleared just like all the rest, only it is messed up by some initialization 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 main, > because windows don't allow you to allocate that much (at once) on the stack. My > guess is that the other compilers treat declarations inside main as if they were > put outside of main (go ahead and try). > > 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 detecting > this kind of bugs. > > /Paul I > > > > > ------------------------------------------------------- > This SF.net email is sponsored by: IBM Linux Tutorials. > Become an expert in LINUX or just sharpen your skills. Sign up for IBM's > Free Linux Tutorials. Learn everything from the bash shell to sys admin. > Click now! http://ads.osdn.com/?ad_id78&alloc_id371&opick > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users |