Thread: Memory allocation problem

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    3

    Memory allocation problem

    Hi all, I am new to C and am having a memory allocation problem. I am creating a char* and initializing to NULL, then passing it to another function which allocates some memory for it, and copies some characters. When the second function returns, my char* returns to NULL. I'm sure I am missing something simple here but cannot figure it out. Any help would be greatly appreciated. See the example code below:

    Code:
    void functionA() {
      char *text = NULL;
      functionB(text);
      //variable text is still NULL
    }
    
    void functionB(char *text){
      char *tempText = "12345";
      size_t len = strlen(tempText);
      text = (char*)malloc(len * sizeof(char) + 1);
      strcpy(text, tempText );
    }
    Last edited by rtlm; 08-26-2011 at 01:47 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Because C only has pass by value.
    So if you want functionB() to change a pointer declared in functionA, then you need to pass a pointer to that pointer.

    Code:
    void functionA() {
      char *text = NULL;
      functionB(&text);
    }
    
    void functionB(char **text){
      char *tempText = "12345";
      size_t len = strlen(tempText);
      *text = malloc(len * sizeof(char) + 1);  // cast not needed in C
      strcpy(*text, tempText );
    }
    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.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    3
    Thanks for the reply, that has clarified a lot for me however I do have one more question. If the function that allocates the memory for the char* is another level deep (say functionC() called from functionB()), I am assuming that I will need to pass the char** in functionB() as a reference, and functionC() will pick it up as a char***. I was wondering if there was any way to simply pass the memory address from start to finish? I'm asking as the software I am working on is quite badly written, and may require the allocation to happen in perhaps 3 or 4 nested functions (unless I do a significant re-write). I'd rather not be working with char******...etc

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    If the function that allocates the memory for the char* is another level deep (say functionC() called from functionB()), I am assuming that I will need to pass the char** in functionB() as a reference, and functionC() will pick it up as a char***
    No..(AFAIK), just passing the same pointer should suffice ..(As long as all the allocation take place on the heap...and you don't return local pointers .)

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by rtlm View Post
    Thanks for the reply, that has clarified a lot for me however I do have one more question. If the function that allocates the memory for the char* is another level deep (say functionC() called from functionB()), I am assuming that I will need to pass the char** in functionB() as a reference, and functionC() will pick it up as a char***. I was wondering if there was any way to simply pass the memory address from start to finish? I'm asking as the software I am working on is quite badly written, and may require the allocation to happen in perhaps 3 or 4 nested functions (unless I do a significant re-write). I'd rather not be working with char******...etc
    Quote Originally Posted by manasij7479 View Post
    No..(AFAIK), just passing the same pointer should suffice ..(As long as all the allocation take place on the heap...and you don't return local pointers .)
    Manasij7479 is correct, you are not going to keep adding layers of redirection. Just keeping passing the pointer the same way you did in the first function, where the original pointer is referred to as *text.
    Code:
    void foo(void){
         char *text=NULL;
         fooA(&text);
    }
    void fooA(char **textA){
         *textA = <whatever>
         fooB(&*textA);
    }
    void fooB(char **textB){
         *textB = <whatever>
    }
    ect...
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    3
    thanks for all the help guys!

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Code:
    fooB(&*textA);
    A little redundant, no?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory allocation problem
    By ariko in forum C Programming
    Replies: 10
    Last Post: 07-24-2010, 09:09 PM
  2. Memory allocation problem
    By spank in forum C Programming
    Replies: 3
    Last Post: 04-19-2006, 01:37 AM
  3. Memory allocation problem
    By Machado in forum C++ Programming
    Replies: 1
    Last Post: 11-03-2005, 11:11 PM
  4. memory allocation problem
    By ccoder01 in forum C Programming
    Replies: 12
    Last Post: 04-24-2004, 08:22 PM
  5. Memory allocation problem
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 01-23-2004, 12:56 AM