Thread: clearing a c-string vs. freeing the memory there

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    13

    clearing a c-string vs. freeing the memory there

    Hey guys,
    In my program I am gradually clearing c-strings in an array by setting array[0] to '\0'.

    Does this free the memory too? I didn't think that it would but then if I clear one of the strings and then try to free the memory I get errors (though the memory freeing worked perfectly before any of the strings were cleared).

    If it does free the memory, is there a way to clear the c-string without freeing the memory?

    Thanks!

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    If you allocated the memory via malloc/calloc/realloc, no it does not free the memory. If you did not allocate the memory with malloc/calloc/realloc, then there's no need to free the memory.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    13
    Got it, thank you for clearing that up!

    Also, another question involving strcat-when you use strcat(s1,s2) it appends s2 to s1 right? but then if I try to change s2, then the change will be manifested in s1?

    For example, if
    s1="abc" and s2="def"
    and I use
    strcat(s1, s2)
    and then try to clear s2 by
    s2[0]='\0'
    then my concatenated s1 will now be
    "abc" and not "abcdef"

    Is that correct?

    And if so, is there a way to in a sense "clear" s2 if I've concatenated it onto s1 without changing s1?

  4. #4
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by donthate View Post
    Got it, thank you for clearing that up!

    Also, another question involving strcat-when you use strcat(s1,s2) it appends s2 to s1 right? but then if I try to change s2, then the change will be manifested in s1?
    nope
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    13
    Quote Originally Posted by ಠ_ಠ View Post
    nope
    Oh ok-even if the arrays were malloc'ed?

    So how exactly does strcat work?

  6. #6
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by donthate View Post
    Oh ok-even if the arrays were malloc'ed?

    So how exactly does strcat work?
    strcat looks at the value of str2, and changes the value of str1
    http://www.cplusplus.com/reference/c...string/strcat/
    Last edited by ಠ_ಠ; 10-03-2010 at 06:03 PM.
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by donthate View Post
    Oh ok-even if the arrays were malloc'ed?

    So how exactly does strcat work?
    S2 is added to the end of S1... in S1.

    S2 is unchanged and not part of S1.

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    13
    So what if s1 was malloc'ed 4 characters and is "abcd" and s2 is "efg." Then how is the space allocated and added to s1 to be able to fit in s2?

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by donthate View Post
    So what if s1 was malloc'ed 4 characters and is "abcd" and s2 is "efg." Then how is the space allocated and added to s1 to be able to fit in s2?
    It isn't. You are responsible for providing large enough buffers before calling strcat (or any of the other library functions that manipulate strings).

    Moreover; if you allocate 4 bytes for "abcd" you're asking for trouble. Most C library functions expect a null (0) at the end of each string. That's how they know to stop processing. So when you have "abcd" you actually need 5 characters, not 4.

  10. #10
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    You can reallocate memory with realloc().
    Or you can allocate enough space from the beginning for the array. If you want to store "abcd" you can do it in a 1000 char array because you will store 'a','b','c','d','\0'. The null character (\0) is what determines the end of the string and not the lenght of the array

  11. #11
    Novice
    Join Date
    Jul 2009
    Posts
    568
    To reiterate the above, to reliably strcat(A, B) where A and B are strings, memory allocated, either with a variation of malloc() or as an array, should be no less then ( strlen(A) + strlen(B) + 1 ). 1 is for the terminating null character.

    It is almost always a better idea to use strncat(), which takes the maximum number of characters to append to string A as an argument, and adds the terminating null character.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define LEN 10
    
    int main( void )
    {
        char A[LEN] = "Quick";
        char B[] = " red fox.";
        strncat( A, B, LEN - strlen( A ) );
    
        printf( "%s\n", A );
    
        return 0;
    }
    
    // Output: "Quick red"; 9 characters + \0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism and generic lists
    By Shibby3 in forum C# Programming
    Replies: 9
    Last Post: 07-26-2010, 05:27 AM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Memory Allocation In A String Class
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 09-18-2007, 10:34 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM