linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: ratheesh kannoth <ratheesh.ksz@gmail.com>
To: Daniel Baluta <daniel.baluta@gmail.com>
Cc: Randi Botse <nightdecoder@gmail.com>,
	linux-c-programming@vger.kernel.org
Subject: Re: malloc question
Date: Tue, 26 Apr 2011 16:24:05 +0530	[thread overview]
Message-ID: <BANLkTinW-j2DiwbVC5u8BfJMU7jK3_KbwA@mail.gmail.com> (raw)
In-Reply-To: <BANLkTikCz0uyFkU2Df-hiAmmj+-ZRABdVQ@mail.gmail.com>

On Tue, Apr 26, 2011 at 4:10 PM, Daniel Baluta <daniel.baluta@gmail.com> wrote:
> On Tue, Apr 26, 2011 at 1:33 PM, Randi Botse <nightdecoder@gmail.com> 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 ).  Try below code also for more
clarification.....

    char *ptr = malloc(1);
    strcpy(ptr, "what");
    free(ptr);
    strcpy(ptr, "hell");





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

  reply	other threads:[~2011-04-26 10:54 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-26 10:33 malloc question Randi Botse
2011-04-26 10:40 ` Daniel Baluta
2011-04-26 10:54   ` ratheesh kannoth [this message]
2011-04-26 10:57     ` Daniel Baluta
2011-04-26 11:50       ` ratheesh kannoth
2011-04-26 15:05 ` Glynn Clements

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=BANLkTinW-j2DiwbVC5u8BfJMU7jK3_KbwA@mail.gmail.com \
    --to=ratheesh.ksz@gmail.com \
    --cc=daniel.baluta@gmail.com \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=nightdecoder@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).