An allocated variable returning by a fuction is unallocated automatically ?

Discussion in 'C++' 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:19:34 +0200, "orion30" <> wrote:

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


    No.



    >If no how to proceed ?


    Use smart-pointers. Use std::auto_ptr for simple
    transfer of ownership (deallocation responsibility).
    Use boost::shared_ptr for a pointer that isn't
    necessarily "owned" by a single something.

    std::auto_ptr lives in standard header <memory>.

    boost::shared_ptr: go to [http://www.boost.org].



    >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);


    Should be

    aa = new char[ strlen(Result) + 1];

    in C++.


    >return(aa);
    >}
     
    Alf P. Steinbach, Jul 14, 2003
    #2
    1. Advertising

  3. 0On Mon, 14 Jul 2003 10:27:52 GMT, (Alf P. Steinbach) wrote:

    >On Mon, 14 Jul 2003 12:19:34 +0200, "orion30" <> wrote:
    >
    >>
    >>I would like to know, if an allocated variable returning by a fuction is
    >>unallocated automatically ?

    >
    >No.
    >
    >
    >
    >>If no how to proceed ?

    >
    >Use smart-pointers. Use std::auto_ptr for simple
    >transfer of ownership (deallocation responsibility).
    >Use boost::shared_ptr for a pointer that isn't
    >necessarily "owned" by a single something.
    >
    >std::auto_ptr lives in standard header <memory>.
    >
    >boost::shared_ptr: go to [http://www.boost.org].
    >
    >
    >
    >>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);

    >
    >Should be
    >
    > aa = new char[ strlen(Result) + 1];
    >
    >in C++.
    >
    >
    >>return(aa);
    >>}

    >



    Sorry, I didn't think. You're probably trying to achieve something
    else than the code indicates. In that case, use std::string.
     
    Alf P. Steinbach, Jul 14, 2003
    #3
    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. Replies:
    5
    Views:
    629
    Matt Wharton
    Dec 9, 2004
  2. orion30
    Replies:
    1
    Views:
    305
    Barry Schwarz
    Jul 15, 2003
  3. PCHOME
    Replies:
    5
    Views:
    454
    Darius
    Apr 13, 2005
  4. Replies:
    1
    Views:
    343
    Bernd Strieder
    Aug 23, 2006
  5. gobotsoup
    Replies:
    1
    Views:
    349
    gobotsoup
    Jan 7, 2008
Loading...

Share This Page