Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #17485

Re: how to figure out the size of buffer from malloc

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: how to figure out the size of buffer from malloc
Date 2012-02-16 14:50 -0800
Organization None to speak of
Message-ID <lnipj6fl5m.fsf@nuthaus.mib.org> (permalink)
References <jhjofu$jer$1@speranza.aioe.org> <jhjq26$rm9$1@reader1.panix.com> <jhk0oi$8cu$1@speranza.aioe.org>

Show all headers | View raw


pembed2012 <once@was.enough.invalid> writes:
> On Thu, 16 Feb 2012 20:48:06 +0000, John Gordon wrote:
>
>> In <jhjofu$jer$1@speranza.aioe.org> pembed2012 <once@was.enough.invalid>
>> writes:
[...]
>>> How does free know how many bytes to free? I have heard that malloc
>>> also allocate additional memory to hold some header information. Where
>>> is this header information stored? If we have access to the header, we
>>> can find that out?
>> 
>> It's all compiler- and library-specific.  The language itself doesn't
>> specify how it is done.
>
> Thanks, I've tried this but it does NOT work :(
>
> This code prints: 0, it should print: 10
>
> int get_malloc_size(void *vp)
> {
>   char *cp = (char*)vp;
>   static int __done = -1;
>   static int __offset = -1;

Identifiers starting with two underscores are reserved to the
implementation.  You should *never* define such identifiers in
your own code.  The rules are a little more complex for identifiers
starting with a single underscore but you should still avoid them.

[snip]

>   return *(int*)(cp-__offset);
> }
>
> main()

Should be "int main(void)".

> {
>   char *p=(char*)malloc(10);
>   printf("%d\n", get_malloc_size((void*)p));
>   free(p);
> }

Why would you expect it to work?  There is *no* portable way to
determine the size of a malloc()ed chunk of memory.

Actually, there is: just remember the argument you passed to malloc():

    size_t requested_size = 10;
    char *p = malloc(requested_size);
    if (p != NULL) {
        /* You know you've allocated requested_size bytes. */
    }

Note that the implementation could allocate more memory than you
request; it won't necessarily store the requested size anywhere.
But that shouldn't matter to your program.

If you think it does matter, explain what you're trying to accomplish
and we can probably help you find another way.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

how to figure out the size of buffer from malloc pembed2012 <once@was.enough.invalid> - 2012-02-16 20:21 +0000
  Re: how to figure out the size of buffer from malloc James Kuyper <jameskuyper@verizon.net> - 2012-02-16 15:38 -0500
  Re: how to figure out the size of buffer from malloc John Gordon <gordon@panix.com> - 2012-02-16 20:48 +0000
    Re: how to figure out the size of buffer from malloc pembed2012 <once@was.enough.invalid> - 2012-02-16 22:42 +0000
      Re: how to figure out the size of buffer from malloc John Gordon <gordon@panix.com> - 2012-02-16 22:50 +0000
      Re: how to figure out the size of buffer from malloc Keith Thompson <kst-u@mib.org> - 2012-02-16 14:50 -0800
        Re: how to figure out the size of buffer from malloc pembed2012 <once@was.enough.invalid> - 2012-02-17 19:45 +0000
          Re: how to figure out the size of buffer from malloc China Blue Sea <chine.bleu@yahoo.com> - 2012-02-17 11:53 -0800
          Re: how to figure out the size of buffer from malloc Ike Naar <ike@sverige.freeshell.org> - 2012-02-17 20:23 +0000
          Re: how to figure out the size of buffer from malloc James Kuyper <jameskuyper@verizon.net> - 2012-02-17 15:33 -0500
            Re: how to figure out the size of buffer from malloc pembed2012 <once@was.enough.invalid> - 2012-02-17 22:13 +0000
              Re: how to figure out the size of buffer from malloc James Kuyper <jameskuyper@verizon.net> - 2012-02-17 17:25 -0500
              Re: how to figure out the size of buffer from malloc Shao Miller <sha0.miller@gmail.com> - 2012-02-17 17:54 -0500
              Re: how to figure out the size of buffer from malloc Keith Thompson <kst-u@mib.org> - 2012-02-17 15:01 -0800
                Re: how to figure out the size of buffer from malloc Shao Miller <sha0.miller@gmail.com> - 2012-02-17 18:10 -0500
              Re: how to figure out the size of buffer from malloc Ian Pilcher <arequipeno@gmail.com> - 2012-02-17 18:14 -0600
              Re: how to figure out the size of buffer from malloc "BartC" <bc@freeuk.com> - 2012-02-18 11:07 +0000
              Re: how to figure out the size of buffer from malloc ImpalerCore <jadill33@gmail.com> - 2012-02-18 04:40 -0800
              Re: how to figure out the size of buffer from malloc Kaz Kylheku <kaz@kylheku.com> - 2012-02-18 12:48 +0000
          Re: how to figure out the size of buffer from malloc Shao Miller <sha0.miller@gmail.com> - 2012-02-17 17:45 -0500
      Re: how to figure out the size of buffer from malloc Shao Miller <sha0.miller@gmail.com> - 2012-02-16 22:18 -0500
  Re: how to figure out the size of buffer from malloc Devil with the China Blue Dress <chine.bleu@yahoo.com> - 2012-02-16 13:20 -0800
  Re: how to figure out the size of buffer from malloc Lew Pitcher <lpitcher@teksavvy.com> - 2012-02-16 17:07 -0500
  Re: how to figure out the size of buffer from malloc Keith Thompson <kst-u@mib.org> - 2012-02-16 14:28 -0800
  Re: how to figure out the size of buffer from malloc Mark Adler <madler@alumni.caltech.edu> - 2012-02-16 23:49 -0800
  Re: how to figure out the size of buffer from malloc Michael Angelo Ravera <maravera@prodigy.net> - 2012-02-18 09:54 -0800
    Re: how to figure out the size of buffer from malloc gazelle@shell.xmission.com (Kenny McCormack) - 2012-02-18 18:21 +0000
      Re: how to figure out the size of buffer from malloc Shao Miller <sha0.miller@gmail.com> - 2012-02-18 13:56 -0500
      Re: how to figure out the size of buffer from malloc Stephen Sprunk <stephen@sprunk.org> - 2012-02-18 13:42 -0600

csiph-web