Thread: [Dev-C++] Why thisC example don't presents the correct result ?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <hu...@cl...> - 2003-12-11 14:21:04
|
Hi all, <br>in others compilers this is the result : <br> <br>10 <br>200 <br> <br>of this program : <br>#include <stdio.h> <br>main<img src="http://pcp.clix.pt/images/canais/webmail2/smile/kiss.gif"> <br>{ <br>int v[3],i; <br>v[0]=10; <br>v[1]=200; <br>i=0; <br>while(v[i]!='\0') <br>{ <br> printf("%d\n",v[i]); <br> i++; <br>} <br>} <br> <br>in dev-c++compiler i have the this result : <br>10 <br>200 <br>2293664 <br>4198592 <br>4210688 <br>4210692 <br>2293664 <br>4198757 <br>1 <br>4138088 <br>4139640 <br>4198553 <br> <br> <br>Why ????? <br> <br>Thanks.<p>--<br>Hugo Dias<br> <p>Clix Rapidix - Aumente até 6X a velocidade da sua Internet Adira em http://acesso.clix.pt e comece logo a navegar<br> |
|
From: P.D. v. Z. <pd...@xs...> - 2003-12-11 14:38:32
|
Because your code is wrong (aside from the fact that you sent this
(stupidly) as an HTML mail... don't do that!).
=20
I believe you repeatedly tried to ask this very same question on the =
forum
(and thereby annoying the moderators).
Your third v element (v[2]) isn't initialized to anything, so it can =
contain
any arbitrary value. Then in your for loop you use the null character to
check if you've reached the end of the array (which is wrong as well, =
simply
use an int, like 0 (even though they are actually the same value)). =
Because
the third element of v will contain garbage, your program will loop =
until it
finally touches some memory that it _really_ shouldn't, and will
subsequently crash.
This code will work:
#include <stdio.h>
int main()
{
int v[3], i;
v[0] =3D 10;
v[1] =3D 200;
v[2] =3D 0;
=09
i =3D 0;
while (v[i] !=3D 0)
{
printf("%d\n", v[i]);
i++;
}
}
Another thing: By saying "this does work in other compilers" (or =
utterings
of the same sort) you imply that this compiler is at fault because it =
won't
compile your perfect code, because you know it's perfect, because other
compilers do make it work. The compiler Dev-C++ uses is the MinGW port =
of
GCC. GCC is about the most standards-compliant compiler you'll find, so =
you
can safely assume that it's the other compilers that are at fault, not =
GCC.
Regards,
Paul
________________________________
From: dev...@li...
[mailto:dev...@li...] On Behalf Of
hu...@cl...
Sent: Thursday, December 11, 2003 3:21 PM
To: dev...@li...
Subject: [Dev-C++] Why thisC example don't presents the correct result ?
Hi all,=20
in others compilers this is the result :=20
10=20
200=20
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
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
Why ?????=20
Thanks.=20
--
Hugo Dias
=20
Clix Rapidix - Aumente at=E9 6X a velocidade da sua Internet Adira em
http://acesso.clix.pt e comece logo a navegar
------------------------------------------------------- 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_id=3D1278&alloc_id=3D3371&op=3Dclick
_______________________________________________ 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=20
|
|
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 |
|
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 |
|
From: Maya <Esc...@ne...> - 2003-12-12 11:14:48
|
s2...@st... wrote: >No, the stack is never cleared (push and pop don't delete data, just >declare it useless) so on its top there are datas > Ehmm, data is the plural, and datum the singular. Sorry! >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).. > > -- Jorge Escalante FYI http://www.astro.umd.edu/~marshall/abbrev.html E-Mail Policy http://www.vif.com/users/escalante/Email_Policy.html * You cannot exercise your power to a point of humiliation. - Jean Chretien * The media's the most powerful entity on earth. They have the power to make the innocent guilty and to make the guilty innocent, and that's power. - Malcom X * "Innocent until proven guilty", no... not in Canada!! |
|
From: Ferdinando F. <s2...@st...> - 2003-12-12 15:39:30
|
On Fri, 12 Dec 2003, Maya wrote: > Ehmm, data is the plural, and datum the singular. Sorry! Thank you; but I really don't know why I wrote it with the 's' (in my mail were all plurals). Bye Ferdinando |