Re: memory and function pointer problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 10/24/05, gaurav sharma <togsharma@xxxxxxxxx> wrote:
>
>
> On 10/22/05, Rik van Riel <riel@xxxxxxxxxxx> wrote:
> > On Sat, 22 Oct 2005, gaurav sharma wrote:
> >
> > > p = malloc(500*sizeof(int));
> >
> > > for(i=0;i<512;i++) {
> > > printf("p[%d]: %d\n",i,p[i]);
> > > }
> >
> > > Can someone please explain why this is happening.I am using gcc4
> > > compiler on 2.6 linux kernel. Shouldn't it give seg fault. I read that
> > > accessing more memory than allocated results in seg fault.
> >
> > Memory for processes is allocated on a page size (4kB)
> > granularity.  I suspect there is some space left in the
> > page, beyond the size of your array.
>
>  Then According to you, If I allocate 500*sizeof(int) ie. 2000 bytes, I'll
> get 4KB(default page size) so I can access upto 4096 bytes. however,  I was
> able to access upto 33789 bytes after which I got segmentation fault.
>
>  p[33787]: 33787
>  p[33788]: 33788
>  p[33789]: 33789
>  Segmentation fault
>
>  I found that till allocating till 1019*sizeof(int), I can access upto 33789
> bytes. from 1020 to 2043 I can access 1024 more bytes and so on...Don't know
> why I am getting this. Is this alright ? Please explain.
>

The point mentioned by Rik that memory for processes are allocated to
page_size granularity is valid for kernel processes and might possible
you will get the exact amount of memory in kernel because AFAIK in
kernel memory allocation is very strict. But for user space memory
allocations (like malloc) depends on the Glibc and the heap (and heap
size) from which is memory is going to be allocated, so you might be
able to access memory beyond what you asked for till the heap size
from where memory is allocated ..... (This is what I can think about
memory allocation in user space, I might be wrong). Also have you
noticed that after passing the last byte of memory which you requested
(in above case 500-bytes) to 501-Byte you will be getting garbage
value (or might be some constant value as i am getting this value
133161 every time i run the code above)

>
> > > If I am not getting seg fault here, then in which case I should expect
> > > it ?
> >
> > You will get a segfault when you try to access virtual
> > memory that is not in a page assigned to your process.
>
>  If this is the case then why I am not getting seg fault above as my program
> is also accessing more virtual memory than was allocated ?
>

If my above point is valid then you will be going to get seg fault
when you try to access memory greater than the heap size from which
you got the allocation. (CMIIW)

By the way I think you better ask this question to Glibc lists too.

--
Fawad Lateef

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux