Query about strings and pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • srbakshi
    New Member
    • Aug 2008
    • 18

    Query about strings and pointers

    Hi all! I have the following code:

    Code:
    typedef struct A
    {
          char* string;
    }A;
    
    //Function prototype
    void fill_up_A(A* a);
    
    int main()
    {
          A var_A;
          fill_up_A( &var_A);
          printf("String is : %s", var_A.string);
          return 1;
    }
    
    //Function definition
    void fill_up_A(A* a)
    {
          a->string = "Hello World!";
    }

    The definition of fill_up_A could ALSO be:

    Code:
    //Function definition
    void fill_up_A(A* a)
    {
          a->string = (char *)malloc(20);
          strcpy(a->string, "Hello World!");
    }
    Now my question is, which function definition is a better way of filling up the structure? Is there a difference in the two ways of implementing this at all? Or are they absolutely similar and any subsequent operations on a->string would have exactly the same effect in both scenarios?

    Thanks in advance,
    Sid
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Do you intend to alter the strings pointed to by A? Whether string literals are writeable is implementation-dependent; so your first example could lead to a run-time exception if you tried to alter the string. If you malloc the string storage then the string is certainly writeable; but you have to be careful about writing past the end of the malloc'ed block (which would certainly cause a run-time error).

    If you truly don't intend to alter the strings then you should declare the string field as a const char pointer. If you do this, then I would personally prefer the string literal approach.

    By the way, if you use the malloc technique then I'm troubled by your example. You malloc 20 bytes. Then you strcpy a literal string into the malloc'ed block, trusting that the literal is smaller than the malloc block.

    Comment

    • srbakshi
      New Member
      • Aug 2008
      • 18

      #3
      Do you intend to alter the strings pointed to by A? Whether string literals are writeable is implementation-dependent; so your first example could lead to a run-time exception if you tried to alter the string. If you malloc the string storage then the string is certainly writeable; but you have to be careful about writing past the end of the malloc'ed block (which would certainly cause a run-time error).

      If you truly don't intend to alter the strings then you should declare the string field as a const char pointer. If you do this, then I would personally prefer the string literal approach.

      By the way, if you use the malloc technique then I'm troubled by your example. You malloc 20 bytes. Then you strcpy a literal string into the malloc'ed block, trusting that the literal is smaller than the malloc block.
      Thank you Mr.Donbock for your informative reply. :)

      I actually did not intend to alter the string. So going by your word I used the 1st approach. Thank you for letting me know how this works. The malloc'ing of extra memory was just because I was too lazy to count the number of chars in "Hello World!'. :p

      Thanks again Mr.Donbok.
      Sid

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Originally posted by srbakshi
        The malloc'ing of extra memory was just because I was too lazy to count the number of chars in "Hello World!'.
        I wasn't troubled by the fact that you allocated extra memory; I was troubled that the connection between the amount of memory malloc'ed and the number of characters copied was accomplished manually.

        Suppose somebody has to modified your program to change "Hello World!" to "Hello You Great and Wonderful World!". What are the chances that they will notice the need to change the malloc argument accordingly?

        Compare your code to this:
        Code:
        const char * const text = "Hello World!";
        a->string = malloc(strlen(text)+1);
        if (a->string == NULL) {
           <do something dramatic>
           }
        strcpy(a->string, text);
        Notice that the size of the malloc'ed block automatically adjusts to the length of the string. Modifying the text is a one-line change.

        Also notice the need to explicitly check for malloc failure.

        Comment

        • srbakshi
          New Member
          • Aug 2008
          • 18

          #5
          Originally posted by donbock
          I wasn't troubled by the fact that you allocated extra memory; I was troubled that the connection between the amount of memory malloc'ed and the number of characters copied was accomplished manually.

          Suppose somebody has to modified your program to change "Hello World!" to "Hello You Great and Wonderful World!". What are the chances that they will notice the need to change the malloc argument accordingly?

          Compare your code to this:
          Code:
          const char * const text = "Hello World!";
          a->string = malloc(strlen(text)+1);
          if (a->string == NULL) {
             <do something dramatic>
             }
          strcpy(a->string, text);
          Notice that the size of the malloc'ed block automatically adjusts to the length of the string. Modifying the text is a one-line change.

          Also notice the need to explicitly check for malloc failure.
          ooooh! That's a great way of dealing with malloc. I'm glad I posted my query here. Got to learn new things.
          Thank you Mr.Donbock.
          :O)
          Sid

          Comment

          Working...