468,496 Members | 1,790 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,496 developers. It's quick & easy.

Size of malloc'd?

How do I determine the size of a previously malloc'd buffer?

char *buffer;

buffer = calloc(18, sizeof *buffer);

I cannot do a sizeof(buffer) since it would only return the size of the
pointer. I cannot do sizeof *buffer since it would only return the
size of the pointer type. Is there a right way to do this? Do I need
to manage this information in the program?

Aug 29 '06 #1
3 14544

bw*****@yahoo.com wrote:
How do I determine the size of a previously malloc'd buffer?

char *buffer;

buffer = calloc(18, sizeof *buffer);

I cannot do a sizeof(buffer) since it would only return the size of the
pointer. I cannot do sizeof *buffer since it would only return the
size of the pointer type. Is there a right way to do this? Do I need
to manage this information in the program?
This is covered by questions 7.27 and 7.28 in the FAQ list.
Basically, yes, you need to keep track of this for yourself.
--

Aug 29 '06 #2


bw*****@yahoo.com wrote On 08/29/06 14:20,:
How do I determine the size of a previously malloc'd buffer?

char *buffer;

buffer = calloc(18, sizeof *buffer);

I cannot do a sizeof(buffer) since it would only return the size of the
pointer. I cannot do sizeof *buffer since it would only return the
size of the pointer type. Is there a right way to do this? Do I need
to manage this information in the program?
This is Question 7.27 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.c-faq.com/

--
Er*********@sun.com

Aug 29 '06 #3

Eric Sosman wrote:
bw*****@yahoo.com wrote On 08/29/06 14:20,:
How do I determine the size of a previously malloc'd buffer?

char *buffer;

buffer = calloc(18, sizeof *buffer);

I cannot do a sizeof(buffer) since it would only return the size of the
pointer. I cannot do sizeof *buffer since it would only return the
size of the pointer type. Is there a right way to do this? Do I need
to manage this information in the program?

This is Question 7.27 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.c-faq.com/
Thanks for the link. The answer is what I expected.

Thanks!

Aug 29 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Hongzheng Wang | last post: by
22 posts views Thread by Wynand Winterbach | last post: by
54 posts views Thread by Neo | last post: by
31 posts views Thread by bilbothebagginsbab5 AT freenet DOT de | last post: by
40 posts views Thread by madireddy | last post: by
27 posts views Thread by Deephay | last post: by
33 posts views Thread by Adam Chapman | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.