Size of malloc'd?

B

bwaichu

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?
 
B

bert

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.
--
 
E

Eric Sosman

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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top