malloc

Discussion in 'C Programming' started by gNash, Apr 23, 2007.

  1. gNash

    gNash Guest

    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..
     
    gNash, Apr 23, 2007
    #1
    1. Advertising

  2. gNash

    Chris Dollin Guest

    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
     
    Chris Dollin, Apr 23, 2007
    #2
    1. Advertising

  3. gNash

    Flash Gordon Guest

    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
     
    Flash Gordon, Apr 23, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John
    Replies:
    13
    Views:
    732
  2. ravi
    Replies:
    0
    Views:
    479
  3. Peter
    Replies:
    34
    Views:
    2,052
    Richard Tobin
    Oct 22, 2004
  4. porting non-malloc code to malloc

    , Feb 18, 2005, in forum: C Programming
    Replies:
    3
    Views:
    501
    Walter Roberson
    Feb 19, 2005
  5. Johs32

    to malloc or not to malloc??

    Johs32, Mar 30, 2006, in forum: C Programming
    Replies:
    4
    Views:
    338
    Captain Winston
    Mar 30, 2006
Loading...

Share This Page