Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > malloc

Reply
Thread Tools

malloc

 
 
gNash
Guest
Posts: n/a
 
      04-23-2007
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..

 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      04-23-2007
gNash wrote:

> 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

 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      04-23-2007
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.
--
Flash Gordon
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
to malloc or not to malloc?? Johs32 C Programming 4 03-30-2006 10:03 AM
porting non-malloc code to malloc micromysore@gmail.com C Programming 3 02-19-2005 05:39 AM
Malloc/Free - freeing memory allocated by malloc Peter C Programming 34 10-22-2004 10:23 AM
free'ing malloc'd structure with malloc'd members John C Programming 13 08-02-2004 11:45 AM
Re: free'ing malloc'd structure with malloc'd members ravi C Programming 0 07-30-2004 12:42 PM



Advertisments