malloc

G

gNash

Hi all,

i have some question on memory allocation.. if we use strlen()
for const string it will give string length because it will count a
variable upto '\0' character..

Is there any way to know the length of dynamically allocated
memory block..

and malloc(30) how to check 30 bytes has been allocated in memory?

Could you any one resolve my confusion please..

Thanks in advance.

Nash..
 
C

Chris Dollin

gNash said:
i have some question on memory allocation.. if we use strlen()
for const string it will give string length because it will count a
variable upto '\0' character..

Yes. In fact `strlen` will do this for /any/ string, const or not.
Is there any way to know the length of dynamically allocated
memory block..

Yes: remember the size you asked for when you allocated that memory.

There is no other portable way.

Why do you think you need to know this?
and malloc(30) how to check 30 bytes has been allocated in memory?

If `malloc(30)` returns a non-null pointer, then that pointer points
to (at least) thirty bytes of memory. (Or the implementation is broken.)

What actual problem do you have?

--
"It was the first really clever thing the King had said that day."
/Alice in Wonderland/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN
 
F

Flash Gordon

gNash wrote, On 23/04/07 11:49:
Hi all,

i have some question on memory allocation.. if we use strlen()
for const string it will give string length because it will count a
variable upto '\0' character..

Dont' forget that it does not include the nul termination byte, so you
actually have one more byte than strlen reports.
Is there any way to know the length of dynamically allocated
memory block..

and malloc(30) how to check 30 bytes has been allocated in memory?

This is question 7.27 of the comp.lang.c FAQ available at http://c-faq.com/

In future, please check the FAQ before posting, it will save both you
and us time.
 

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,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top