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


Groups > comp.lang.c > #17543

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

From Shao Miller <sha0.miller@gmail.com>
Newsgroups comp.lang.c
Subject Re: how to figure out the size of buffer from malloc
Date 2012-02-17 17:54 -0500
Organization A noiseless patient Spider
Message-ID <jhmlkf$ngv$1@dont-email.me> (permalink)
References (2 earlier) <jhk0oi$8cu$1@speranza.aioe.org> <lnipj6fl5m.fsf@nuthaus.mib.org> <jhmaol$qn4$1@speranza.aioe.org> <jhmdjn$7f9$1@dont-email.me> <jhmjds$is4$1@speranza.aioe.org>

Show all headers | View raw


On 2/17/2012 17:13, pembed2012 wrote:
> On Fri, 17 Feb 2012 15:33:58 -0500, James Kuyper wrote:
>> On 02/17/2012 02:45 PM, pembed2012 wrote: ...
>>> Sorry it wasn't clear, I need to take a pointer from malloc and find
>>> out the size of the allocation. To allow for dynamic pointers as well
>>> as stack arrays. Like this:
>>>
>>> myfunc(void* buf){
>>>    int bufsiz=-1;
>>
>> It's absolutely guaranteed, at this point, that sizeof(buf) ==
>> sizeof(void*). As a result, 2 of your next three lines are pointless,
>> which suggests that you were not aware of that fact.
>>
>>>    if(sizeof(buf)==sizeof(void*)) { // pointer
>>>      bufsiz=get_malloc_size(buf)'
>>>    }
>>
>> Why did you think it was possible for the condition of that if() to not
>> be met?
>
> Often this function will be called with a dynamic pointer from malloc.
> However, it is also possible to call it with a stack array:
>
> char ary[10];
> char* ptr=malloc(10);
> myfunc((void*)ary);  // should work
> myfunc((void*)ptr);  // should also work
>
> It started off with the buffers being NULL-free strings. However now I
> need to work with strings with embedded NULL characters. This means I
> can't find the length of the buffer with strlen. And it would be too much
> change to add a new argument to pass the length of the buffer. So the
> only good solution is for myfunc to work out by it's self 1) if the
> buffer is a dynamic pointer or stack array and 2) the size of the buffer.

I don't think a string has 'NULL' in it.  I think a string can have a 
null terminator.  I think that 'NULL' is an invalid pointer value.

I think you are mistaken about what you are doing and expecting.

It looks like 'ary' has type 'char[10]'.  When 'ary' is used in your 
examples, it yields a 'char *' value.  All pointer-to-object types can 
be implicitly converted to 'void *', so your cast is redundant.

It looks like the type of 'ptr' is 'char *'.  All pointer-to-object 
types can be implicitly converted to 'void *', so your cast is redundant.

In C, if you have a pointer into some object (single object, array 
object, "stack," allocated, static-duration; no difference), I do not 
believe that you can expect that a pointer value carries size 
information nor that even if it does, that you can access that information.

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