Re: malloc question

Daniel Baluta <[email protected]> Tue, 26 Apr 2011 13:57:56 +0300
Newsgroups org.kernel.vger.linux-c-programming
Message-ID <[email protected]>
On Tue, Apr 26, 2011 at 1:54 PM, ratheesh kannoth
<[email protected]> wrote:
> On Tue, Apr 26, 2011 at 4:10 PM, Daniel Baluta <[email protected]> wrote:
>> On Tue, Apr 26, 2011 at 1:33 PM, Randi Botse <[email protected]> wrote:
>>> Hi All,
>>>
>>> I want to ask malloc() behaviour, consider these codes;
>>>
>>> ...
>>> char *ptr = malloc(1);
>>> strcpy(ptr, "what");
>>> puts(ptr);
>>> ....
>>>
>>> Confusingly, the strcpy() copied all bytes to ptr, but I just manage
>>> to allocate ptr only for 1 byte, I guess I will have segfault here,
>>> why this happen? why the string successfully copied into ptr? , is
>>> those code legal?
>>
>> You didn't get segfault because you were lucky.
>>
>> Memory is allocated in multiples of page size (usually 4K).
>> The memory after your allocated byte is valid in your case.
>>
>> thanks,
>> Daniel.
>> --
>
> U could read a little more about vm_page_struct. ( virtual address
> space to  physical page ).

Can you elaborate on this?

Daniel.
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html