Amount of memory space need?

This is a discussion on Amount of memory space need? within the C Programming forums, part of the General Programming Boards category; char *string2 ="HELLO"; What is the reservatin of memory required for initialisation for the above declaration?? Lecturer said 10 bytes, ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    16

    Amount of memory space need?

    char *string2 ="HELLO";

    What is the reservatin of memory required for initialisation for the above declaration??

    Lecturer said 10 bytes, tutor says 6 bytes while lecture notes has
    one similar examples which would lead to 6 bytes for the above.

    Need you all experts help.

    Thank a lot.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,336
    At a minimum, its
    strlen("hello") + 1 + sizeof(char*)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    16
    Originally posted by Salem
    At a minimum, its
    strlen("hello") + 1 + sizeof(char*)
    I dun quite get what you mean? Can you tell me if it is 10 or 6 bytes?

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    16
    Originally posted by Salem
    At a minimum, its
    strlen("hello") + 1 + sizeof(char*)
    Oh I think I know whta you are trying to say. 10 bytes right? But why do we add the size of char?

    But I need your confirmation for this:

    char *pointers = "Toh Chin";

    My lecture notes put 9 bytes required. So is it 9 or 13 bytes?

    Thanks.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Try putting the code Salem gave you into a program and try it out.

    But in english:
    Take the number of characters in a string "hello" = 5, "Toh Chin" = 8
    Take that number at 1 to it
    Take that number and add the size of the pointer to it (4)

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    161
    The string itself requires 6 bytes - one for each character plus one for the null terminator.

    The entire defintion requires 10 bytes - 6 for the string and 4 for the char pointer (assuming pointers are 4 bytes).

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    thefroggy, he's asking about "Toh Chin", not "hello".
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    161
    It's the same concept -- the answer depends on whether you want the size of the string or the size of the entire definition. The entire definition is going to include the size of the variable used to point to the string, unless you use an array instead:

    char str[] = "Toh Chin";

    In which case, only strlen(str)+1 bytes are used.

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Just to clarify the extra 1 is for the null character (for jnwk888hwq)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Determing the amount of change(money)
    By Kyeong in forum C Programming
    Replies: 11
    Last Post: 09-30-2008, 04:36 PM
  2. Program that displays amount of free memory
    By trancedeejay in forum Linux Programming
    Replies: 3
    Last Post: 01-13-2006, 12:27 PM
  3. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 01:15 AM
  4. Accessing Video Memory Information...need help
    By KneeLess in forum C++ Programming
    Replies: 8
    Last Post: 08-24-2003, 03:53 PM
  5. amount of allocated memory...
    By lobo in forum Windows Programming
    Replies: 2
    Last Post: 06-20-2002, 10:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21