Hello C/C++ experts,
I write the following code

int a[10];
int b[10];
// initiliaze all element of array a and b to 0.
a[11]=255;

Neither the program crashes nor the value of b[0] becomes 255
What could be the reason?

That would be silent and notorious buffer overflows you are talking about...

try

int a[16] = { 0 }, b[16] = { 0 } ;
  b[16] = 25 ;
  printf( "%p -> %d   %p -> %d\n", a, a[0], b, b[0] ) ;

notes
a. an all hw architectures that i know of, the stack grows from higher towards
lower addresses.
b. most compilers will allocate more memory than seems is necessary
for data. (why?)
c. play around with the size of the array and the storage class of the array and
try to figure out what happens.
d. generate a lising with assembly, source and machine code to see what really
happens.
e. if possible, try to use different compilers and repeat the experiment
.

OMG vijay where did you learn programming :eek: The code you posted still contains buffer overflow.

>>most compilers will allocate more memory than seems is necessary
for data
compiler-specific behavior and something you must never ever count on. talk about taking advantage of undefined behavior :eek:

I guess he wanted to illustrate buffer overflows hence the example.. :D

the code i posted will cause a buffer overflow on most compilers on a 32 bit architecture.
you may need to increase the size of the array to 32 on 64-bit.
the printf should illustrate that overflow has occurred (into the first element of array a)

Hello C/C++ experts,
I write the following code

int a[10];
int b[10];
// initiliaze all element of array a and b to 0.
a[11]=255;

Neither the program crashes nor the value of b[0] becomes 255
What could be the reason?

Because
1) in some (most?) compilers the variables are laid out in memory in reverse order. When I do this I always add a third array c and overwrite the middle array. (I've illustrated this behavior many times)
2) Variables are (generally) allocated on 4-byte boundaries. Therefore there's an extra 2 bytes at the end of each array that is 'no-mans-land', so writing to that will just loose the data. So I always write 4 or more bytes to be sure to reach the next array.
3) You did not blow the bounds of your data space, only your array. Things crash when you try to access outside your data space. I believe you can corrupt your own data all day without crashing.

the code i posted will cause a buffer overflow on most compilers on a 32 bit architecture.


No -- it will create buffer overflow on every ansi standard compilant compiler, which is almost every c compiler in the world.

the code i posted will cause a buffer overflow on most compilers on a 32 bit architecture.


No -- it will create buffer overflow on every ansi standard compilant compiler, which is almost every c compiler in the world.

true. i stand corrected.
i should have said ".. will cause a buffer overflow into a[0] on most
compilers on a 32 bit architecture."

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.