Re: memory and function pointer problem

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

 





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.


> 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 ?

thanks,
Gaurav

--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it." - Brian W. Kernighan


[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