471,831 Members | 1,197 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,831 software developers and data experts.

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 14648

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
NeoPa
reply views Thread by NeoPa | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.