Thread: 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: Glenn W. <gl...@mr...> - 2003-12-11 14:37:18
|
I'm not sure exactly what you are trying to do. Bounds checking in c =
could be done this way but only if you actually initialized all of your =
subscripts. Since v[2] is never initialized you are getting whatever =
value is stored in this memory space which could be anything. Character =
arrays used for strings are the only thing I would know of that would =
store a null in the last position, but even this would only be true if =
you initialized the c string with an actual string instead of =
characters.
=20
char v[3] =3D "AB"; will result in 'A', 'B' and '\0'
char v[3] =3D {'A', 'B'} will result in the 3rd position being =
uninitialized
=20
In short there isn't bounds checking in C and you can't assume that a =
value exists unless you place it in the array yourself. It would be =
safest to keep the size of the array in something like a constant and =
use that for looping through your array.
=20
Sincerely,=20
Glenn Welker=20
Director of Product Development=20
MRH Technology Group=20
-----Original Message-----
From: hu...@cl... [mailto:hu...@cl...]
Sent: Thursday, December 11, 2003 9:21 AM
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: Reid T. <Rei...@at...> - 2003-12-11 14:48:48
|
Seems simpler, does not require you to keep up with size of array for =
looping:
=20
int main(int argc, char *argv[])
{
=20
int v[3],i;=20
memset(v, '\0', sizeof(v));
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
reid=20
-----Original Message-----
From: Glenn Welker [mailto:gl...@mr...]=20
Sent: Thursday, December 11, 2003 9:37 AM
To: hu...@cl...; dev...@li...
Subject: RE: [Dev-C++] Why thisC example don't presents the correct =
result ?
=09
=09
I'm not sure exactly what you are trying to do. Bounds checking in c =
could be done this way but only if you actually initialized all of your =
subscripts. Since v[2] is never initialized you are getting whatever =
value is stored in this memory space which could be anything. Character =
arrays used for strings are the only thing I would know of that would =
store a null in the last position, but even this would only be true if =
you initialized the c string with an actual string instead of =
characters.
=20
char v[3] =3D "AB"; will result in 'A', 'B' and '\0'
char v[3] =3D {'A', 'B'} will result in the 3rd position being =
uninitialized
=20
In short there isn't bounds checking in C and you can't assume that a =
value exists unless you place it in the array yourself. It would be =
safest to keep the size of the array in something like a constant and =
use that for looping through your array.
=20
Sincerely,=20
Glenn Welker=20
Director of Product Development=20
MRH Technology Group=20
-----Original Message-----
From: hu...@cl... [mailto:hu...@cl...]
Sent: Thursday, December 11, 2003 9:21 AM
To: dev...@li...
Subject: [Dev-C++] Why thisC example don't presents the correct result =
?
=09
=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
--
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: Reid T. <Rei...@at...> - 2003-12-11 14:51:58
|
i think he's getting whats in v[2] plus whatever is in the next =
incremental integer place in memory continously until he happens to =
reach a '\0'.
=20
=20
reid=20
-----Original Message-----
From: Glenn Welker [mailto:gl...@mr...]=20
Sent: Thursday, December 11, 2003 9:37 AM
To: hu...@cl...; dev...@li...
Subject: RE: [Dev-C++] Why thisC example don't presents the correct =
result ?
=09
=09
I'm not sure exactly what you are trying to do. Bounds checking in c =
could be done this way but only if you actually initialized all of your =
subscripts. Since v[2] is never initialized you are getting whatever =
value is stored in this memory space which could be anything. Character =
arrays used for strings are the only thing I would know of that would =
store a null in the last position, but even this would only be true if =
you initialized the c string with an actual string instead of =
characters.
=20
char v[3] =3D "AB"; will result in 'A', 'B' and '\0'
char v[3] =3D {'A', 'B'} will result in the 3rd position being =
uninitialized
=20
In short there isn't bounds checking in C and you can't assume that a =
value exists unless you place it in the array yourself. It would be =
safest to keep the size of the array in something like a constant and =
use that for looping through your array.
=20
Sincerely,=20
Glenn Welker=20
Director of Product Development=20
MRH Technology Group=20
-----Original Message-----
From: hu...@cl... [mailto:hu...@cl...]
Sent: Thursday, December 11, 2003 9:21 AM
To: dev...@li...
Subject: [Dev-C++] Why thisC example don't presents the correct result =
?
=09
=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
--
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: <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 |