Allocated variable returning by a fuction is unallocated automatically ?

Discussion in 'C Programming' started by orion30, Jul 14, 2003.

  1. orion30

    orion30 Guest

    I would like to know, if an allocated variable returning by a fuction is
    unallocated automatically ?

    If no how to proceed ?

    Best regards

    ------------------------------------------------------------------------

    In my case, I want to return aa. After the call of polit, is aa is
    unallocated automatically ?

    char * polit(char *expressions)
    {
    ..
    ..
    ..
    aa = (char *)malloc(strlen(Result)*sizeof(char)+1);
    ..
    ..
    ..
    return(aa);
    }
    orion30, Jul 14, 2003
    #1
    1. Advertising

  2. On Mon, 14 Jul 2003 12:13:41 +0200, "orion30" <>
    wrote:

    >
    >I would like to know, if an allocated variable returning by a fuction is
    >unallocated automatically ?
    >
    >If no how to proceed ?
    >
    >Best regards
    >
    >------------------------------------------------------------------------
    >
    >In my case, I want to return aa. After the call of polit, is aa is
    >unallocated automatically ?
    >
    >char * polit(char *expressions)
    >{
    >.
    >.
    >.
    >aa = (char *)malloc(strlen(Result)*sizeof(char)+1);
    >.
    >.
    >.
    >return(aa);
    >}
    >

    If aa is an automatic variable defined in polit, then the variable
    itself will go out of scope at the end of the function. The value
    contained in the variable will be returned to the calling function
    properly. Since this value is the address of a dynamically allocated
    block of memory and since explicitly allocated memory must be
    explicitly freed (which does not occur here), this value continues to
    point to memory which remains available to the calling function.

    To avoid memory leaks, the calling function should eventually free the
    memory when it has served its purpose.


    <<Remove the del for email>>
    Barry Schwarz, Jul 15, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. orion30
    Replies:
    2
    Views:
    300
    Alf P. Steinbach
    Jul 14, 2003
  2. Replies:
    5
    Views:
    601
    Matt Wharton
    Dec 9, 2004
  3. PCHOME
    Replies:
    5
    Views:
    438
    Darius
    Apr 13, 2005
  4. Replies:
    1
    Views:
    330
    Bernd Strieder
    Aug 23, 2006
  5. gobotsoup
    Replies:
    1
    Views:
    331
    gobotsoup
    Jan 7, 2008
Loading...

Share This Page