malloc/free of return value

This is a discussion on malloc/free of return value within the C Programming forums, part of the General Programming Boards category; I'm writing a function that returns a C-style string (char *). THe function performs several operations on the 2 incoming ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    278

    malloc/free of return value

    I'm writing a function that returns a C-style string (char *). THe function performs several operations on the 2 incoming strings and returns the result. While I'm performing these operations I use a temp variable...

    Code:
    char * sTemp = malloc((sizeof(s1) * strlen(s1)) + (sizeof(s2) * strlen(s2)));
    Then at the end of the function I return sTemp. But what about deallocating the memory I allocated for sTemp? Is is automatically deallocated when the function completes? if not can I deallocate AFTER the return statement?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by Bladactania
    Then at the end of the function I return sTemp. But what about deallocating the memory I allocated for sTemp?
    The caller (or the caller's caller, etc) should be responsible for deallocating the memory.

    Quote Originally Posted by Bladactania
    Is is automatically deallocated when the function completes?
    No, and that is a Good Thing, otherwise the caller would then have a pointer to deallocated memory.

    Quote Originally Posted by Bladactania
    if not can I deallocate AFTER the return statement?
    No, but then as I noted, the caller can deallocate the memory, so it is kind of like deallocating after the return statement.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Thanks for the quick reply...

    How does the caller deallocate the memory for a pointer with function level scope?

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Another thing... If I'm returning a pointer to a string like this, do I need to allocate the memory for the pointer I'm returning the value into?

    Code:
    test = malloc(.....);
    test = mystringfunction(s1, s2);

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by Bladactania
    How does the caller deallocate the memory for a pointer with function level scope?
    It uses a pointer. The pointer in the function is destroyed when it goes out of scope, so another pointer is needed to store the address of the dynamically allocated object.

    Quote Originally Posted by Bladactania
    If I'm returning a pointer to a string like this, do I need to allocate the memory for the pointer I'm returning the value into?
    Well, you probably do not need to dynamically allocate memory for such a pointer as a local variable will do.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Here's my function Code...

    Code:
     char * String_Concat (const char *s1, const char *s2) {
       char *sTemp = malloc((sizeof(s1) * strlen(s1)) + (sizeof(s2) * strlen(s2)));
    
       strcpy(sTemp, s1);
       strcat(sTemp, s2);
    
       return sTemp;
     }
    In main I call the function like so...

    Code:
    char   *sDiagnostic_Log_Path; // Full path of diagnostic log file
    
    sDiagnostic_Log_Path = String_Concat(sFlash_Card_Base_Path, sDiagnostic_Log_Name);
    Is that sufficient? I'm terribly scared of memory leaks!

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,668
    Code:
    char* foo() { return malloc(10); }
    void foo2(char** p) { *p = malloc(10); }
    int main()
    {
        char* p;
        p = foo();
        free(p);
        foo2(&p);
        free(p);
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > char *sTemp = malloc((sizeof(s1) * strlen(s1)) + (sizeof(s2) * strlen(s2)));
    Why sizeof(s1) ?
    That's the size of the pointer, not a single char.

    Cheaply, it's
    char *sTemp = malloc( strlen(s1) + strlen(s2) + 1);

    Pedantically, in line with what you're doing
    char *sTemp = malloc((sizeof(s1[0]) * strlen(s1)) + (sizeof(s2[0]) * strlen(s2)) + 1);
    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.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Could I use sizeof(*s1)? I suppose I could use sizeof(char) since I know it will be a character.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    If you like, it's the same thing.
    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.

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    when using sizeof(*s1) I get a "Nonportable pointer conversion" warning... Is there a way to avoid this?

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    avoid it by not using sizeof(*s1). sizeof(char) is 1 anyway and N*1 is always going to be N so it's redundant.

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    What a strange error message from applying sizeof to something.
    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.

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    I think the warning comes from the strlen(s1) not the sizeof(char).

  15. #15
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    I am using Turbo C version 3.0... *sigh*

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  2. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 01:39 PM
  3. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  4. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 09:01 PM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 12:54 AM

Tags for this Thread


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