Like Tree1Likes
  • 1 Post By c99tutorial

How does a string get allocated in memory?

This is a discussion on How does a string get allocated in memory? within the C Programming forums, part of the General Programming Boards category; Hello, I am confused as to how a character array gets stored. Eg. char arr = "string", how does it ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Location
    Kolkata
    Posts
    20

    How does a string get allocated in memory?

    Hello,

    I am confused as to how a character array gets stored. Eg. char arr = "string", how does it get stored in memory?

    Also, what if I intend to perform some arithmetic operations on a particular element from a character array? Eg.where s is a character array, s[i] >= '0'

    Will this work? And if yes, why?

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    985
    For the first question you have two choices:

    char *arr = "string";

    In this case, "string" is stored in read-only memory as an array of characters, terminated by '\0'.

    char arr[] = "string";

    In this case, "string" is stored in writeable memory, so for example arr[0] = 'S' would capitalize the string.

    Characters are just small integers. So '0', 's' and all other characters corresponds to a numeric value. To see the numeric value of '0', try

    printf("%d\n", '0');

  3. #3
    Registered User
    Join Date
    Jan 2013
    Location
    Kolkata
    Posts
    20
    So when I use an array of pointers, the *arr points to the string( no numeric values and the string is stored as it is in memory) whereas when I don't use it the numeric value of 'S' gets stored in arr[0]. Right?

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    985
    char arr[] = "string";

    This produces an array of characters. A character is just a numeric value that ends up being displayed as a "character" on the screen.

    char *arr = "string";

    This produces an array of characters. The only difference is that the compiler puts this array somewhere in "read only memory". It means you're not allowed to change its contents.

    Neither of these examples produces an array of pointers. It's only an array of characters.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Location
    Kolkata
    Posts
    20
    Here is a piece of code which am struggling to comprehend,

    Code:
    void squeeze ( char s[], int c)
    {
       int i, j;
       for ( i=0; j=0; s[i] != '\0'; i++)
            if (s[i] != c)
                    s[j++] = s[i];
       s[j]= '\0';
    }

    Now, why is it that 'int' c been passed as an argument to the function as opposed to char? In the condition ( s[i] != c) , what does exactly happen?

    Thanks in advance.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    985
    I think the for loop should be:

    for ( i=0, j=0; s[i] != '\0'; i++)

    That being said, I think you're right. I would make the parameter c type char so that it matches. Basically, with this function, a caller must make sure that he doesn't give an integer c that is outside the range of valid characters. If that condition is met, then the comparison is fine. However, if you give a value too high or too low for c, undefined behaviour occurs. It means it will give you a result that depends on the machine and the implementation, but it might not be correct.

  7. #7
    Registered User
    Join Date
    Jan 2013
    Location
    Kolkata
    Posts
    20
    Quote Originally Posted by c99tutorial View Post
    The only difference is that the compiler puts this array somewhere in "read only memory". It means you're not allowed to change its contents.
    I don't get by what you mean by ' not allowed to change its contents'. *arr can be used to assign to another string right since neither the pointer nor the string has been declared as a constant. Correct me please.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    985
    When you write a line like

    char *arr = "my string";

    The thing on the right is actually a const char *. Making it char * allows you to attempt to change the contents without a compiler error. However, it probably won't work in practice. It's definitely not guaranteed to work. So in fact you are right, it would be better to write:

    const char *arr = "my string";

    Then the compiler will intercept any attempts to modify what it points to.

  9. #9
    Registered User
    Join Date
    Jan 2013
    Location
    Kolkata
    Posts
    20
    Yeah, I wrote down the wrong for loop. Sorry.

    So, for the above piece of code, the condition ( s[i] != c) compares the numeric value corresponding to the character at s[i] to the integer value c. Am I right?

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    985
    Yes. In C, tests like ==, !=, >, < are always numeric tests, so anytime you see them, you can think of what is being compared as numeric values.

  11. #11
    Registered User
    Join Date
    Jan 2013
    Location
    Kolkata
    Posts
    20
    Okay my doubts are cleared for now. Thanks a lot C99

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allocate memory inside allocated memory block?
    By Heidi_Nayak in forum C Programming
    Replies: 14
    Last Post: 04-15-2009, 04:19 PM
  2. Memory allocated
    By saswatdash83 in forum C Programming
    Replies: 2
    Last Post: 07-15-2008, 02:28 AM
  3. Can you free memory allocated by a std::string?
    By BrianK in forum C++ Programming
    Replies: 17
    Last Post: 05-15-2008, 11:57 AM
  4. Where are pointers allocated in memory?
    By sparks in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2005, 12:52 PM
  5. Memory not allocated??
    By Alexisa in forum C Programming
    Replies: 7
    Last Post: 09-24-2003, 11:19 PM

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