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: Ferdinando F. <s2...@st...> - 2003-12-12 11:00:37
|
On Thu, 11 Dec 2003, Paul Ingelbrant wrote: > 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 Yes, if you call gcc with the -S option you'll see that _every_ global var, unless differently specified, is initialized with the 0 value, but this is true for GCC and not for all compilers. > cleared just like all the rest, only it is messed up by some initialization code > executed before main starts executing. No, the stack is never cleared (push and pop don't delete data, just declare it useless) so on its top there are datas of the other functions you invoked, or random data; but vectors are used from top to bottom, so that loop won't print that, but all internal vars previously allocated, the function (main) arguments, and so on until some of this is 0 or is reached the end of the segment (that results in a segmentation fault on Unix and an access violation in Windows).. > > 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. main is a function like others; simply some compilers can decide to inizialize vars pushing zeros instead of allocating blank spaces. I can't believe compilers can allocate as globals main internal vars. Bye Ferdinando |