This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: bug?
- From: corey taylor <corey dot taylor at gmail dot com>
- To: sokolhacker at mail dot ru
- Cc: gcc-help at gcc dot gnu dot org
- Date: Thu, 25 Aug 2005 20:06:43 -0500
- Subject: Re: bug?
- References: <430E7F43.8060301@mail.ru>
Alexey,
Array bounds checks are not performed in C or C++.
You are writing to memory outside of the array, and it is an error in
the code. You probably do not crash because the program is simple.
cout does not know the size of the array. It only sees a char* and
keeps looking forward 1 byte at a time until a 0 is found.
corey
On 8/25/05, Alexey Sokolov <sokolhacker@mail.ru> wrote:
> Hallo!
> /**************************/
> //example file "example.cpp"
> //begin
> #include <iostream.h>
>
> main()
> {
> char name[1]; //1 byte(!)
> name[0]='1';
> name[1]='2';
> name[2]='3';
> name[3]='4';
> name[4]='\0';
> cout << name << "\n";
> }
> //end
> /**************************/
>
> $g++ example.cpp
> $./a.out
> 1234
>
> 1,2,3,4 and '\0' = 5 byte!
> 5 byte != 1 byte (char name[1];)!
>
> But why? Forgive for English, I am simple Russian =)
>
> --
> #gcc --version
> gcc (GCC) 3.4.2 [FreeBSD] 20040728
>
>