linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Suma C <gsumac@gmail.com>
To: Hareesh Nagarajan <hareesh.nagarajan@gmail.com>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: malloc with size 0
Date: Thu, 22 Dec 2005 13:12:59 +0530	[thread overview]
Message-ID: <1f5f62170512212342odf496f5j1161a3965c9141a@mail.gmail.com> (raw)
In-Reply-To: <7728232c0512212221y368e782br7fe52d2caee76231@mail.gmail.com>

http://www.ussg.iu.edu/hypermail/linux/kernel/0011.0/1078.html   got
this one...the thread discusses the same situation

I guess it talks about malloc allocating  a minimum memory (the header
space !!??)...
and so the next question will be: wt about the unique pointer which is
given in the man page!!!


regards
suma

PS
snip from the link above...
All true, but the reason it "works" is that malloc WILL allocate some memory,
even if it's only a few bytes of header.:

       | | (other memory block controled by malloc/free...)
       |-------|
       | header|
       | | - address returned to program
       | next |
       | header| (next memory block...)
Now the strcpy may have copied the string "fffff" over the next header.
The copy worked, the printf worked (its buffers were already allocated...)
BUT... If you allocate more memory via malloc, you will get an error
(eventually). I believe malloc(0) allocates 4 bytes as a minimum, though
this particular call IS undefined. You also did not check to see if
malloc did return something (It did, or you would have gotten a segmentation
fault from writing to location 0 with strcpy).


On 12/22/05, Hareesh Nagarajan <hareesh.nagarajan@gmail.com> wrote:
> On 12/22/05, Suma C <gsumac@gmail.com> wrote:
>
> > When  a 0 is passed to malloc, I get a valid pointer instead of a NULL
> > .What does this mean?
>
> A search on Google would have given you this thread:
> http://www.theone.ru/lists/comp.lang.c/msg00504.html
>
> hth,
>
> ./hareesh
>

  reply	other threads:[~2005-12-22  7:42 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-12-22  4:43 malloc with size 0 Suma C
2005-12-22  6:21 ` Hareesh Nagarajan
2005-12-22  7:42   ` Suma C [this message]
2005-12-22 13:02 ` Neil Horman
2005-12-30 14:16 ` mail-lists

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=1f5f62170512212342odf496f5j1161a3965c9141a@mail.gmail.com \
    --to=gsumac@gmail.com \
    --cc=hareesh.nagarajan@gmail.com \
    --cc=linux-c-programming@vger.kernel.org \
    /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).