Re: memory and function pointer problem

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

 



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.

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


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

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