On 11/20/05, Andrew Pinski <[EMAIL PROTECTED]> wrote:
> >
> > Hi Everyone,
> >
> > int
> >  main ()
> >  {
> >     int *p;
> >     int i;
> >
> >     p = (int *) malloc (400 * sizeof (int));
> >     for (i = 0; i < 512; i++)
> >      {
> >          p[i] = i;
> >          printf ("p[%d]: %d\n", i, p[i]);
> >      }
> >    return 0;
> >  }
> >
> > The following code should terminate with Segmentation fault, right ?
> >  But when I tried, it works even beyond the limits of allocated area ...
> >  I am using Glibc - 2.3.2-11.9 , gcc - 3.2.2
> >
> >  Is it a bug - malloc's implementation in Glibc.
> >  Does malloc allocates a complete 4k page for certain size of memory asked 
> > for?
> >
> >  Infact, it gives a segmentation fault at 1681 location =>
> >  p[1681]: 1681
> >  Segmentation fault
> >
> > Or is it using Heap area ?
>
> First why do you think this is a GCC bug?

Actually i felt that if its not coming up witha  segmentation fault,
Then might be something goes behind during the compile time. How much
memory is actually allocated by the malloc, I googled through the
sources of Glibc but i was not able to find any info.
If I am wrong somewhere please help.

> Second you are invoking undefined behavior by accessing passed the array 
> bounds (or allocated
> bounds by malloc).

I didnt get your point. I am allocating space only for 400 inregers
then as soon as in the loop if it crosses the value of 400 , it should
have given a segementation voilation ?
But is isint. Fine i agree this is not the job of the compiler, It
might never come into exesitence during the execution time, But Still
me must get a segmentation voilation by the operating system.

Regards,
Sandeep

Reply via email to