returning an std:string pointer as a parameter

Discussion in 'C++' started by Fred, Feb 9, 2006.

  1. Fred

    Fred Guest

    Hi I'm new to the std library.
    I have a function which reads data and creates a std string from it.
    I want to pass that back to the calling function as a parameter, not as the
    function return.

    I have tried various indirections etc but nothing works.

    I want something like this psudo type code:

    typedef basic_string<TCHAR> tstring; // string of TCHARs

    BOOL readFunction(tstring *pret)
    {
    tstring * ptstrpage = new tstring;
    ..
    ..
    ptstrpage = loaded data
    pret=ptstrpage;
    return(TRUE)
    }

    callingfunc
    {

    tstring * pData1,pData2;

    if(!readFunction(pData1)
    {
    error processing
    }


    if(!readFunction(pData2)
    {
    error processing
    }

    delete pData1,pData2;
    }
     
    Fred, Feb 9, 2006
    #1
    1. Advertising

  2. Fred wrote:
    > BOOL readFunction(tstring *pret)


    This is creating a copy of the string pointer rather than allowing you
    to modify pData1 and pData2.

    Try using a reference to the pointer:

    BOOL readFunction(tstring*& pret)

    Josh McFarlane
     
    Josh Mcfarlane, Feb 9, 2006
    #2
    1. Advertising

  3. Fred wrote:
    > Hi I'm new to the std library.
    > I have a function which reads data and creates a std string from it.
    > I want to pass that back to the calling function as a parameter, not as the
    > function return.
    >
    > I have tried various indirections etc but nothing works.
    >
    > I want something like this psudo type code:
    >
    > typedef basic_string<TCHAR> tstring; // string of TCHARs
    >
    > BOOL readFunction(tstring *pret)


    Use bool instead of BOOL

    > {
    > tstring * ptstrpage = new tstring;

    Why use a pointer here? The following is enough
    tstring ptstrpage;

    > .
    > .
    > ptstrpage = loaded data


    Suspicous: who is owner of `loaded data'?
    I guess it's type is TCHAR* for tstring*
    => most probaly a memory leak.

    > pret=ptstrpage;
    > return(TRUE)


    return true;

    Memory leak: ptstrpage never deleted.

    > }
    >
    > callingfunc
    > {
    >
    > tstring * pData1,pData2;


    No memory allocated for pData1 and pData2!
    Again: why pointers where normal variables would do?

    >
    > if(!readFunction(pData1)
    > {
    > error processing
    > }
    >
    >
    > if(!readFunction(pData2)
    > {
    > error processing
    > }
    >
    > delete pData1,pData2;
    > }


    Make the signature
    bool readFunction(tsting& dest);
    and avoid the usage of pointers completely for the code you've posted.

    Regards, Stephan
     
    =?iso-8859-1?q?Stephan_Br=F6nnimann?=, Feb 9, 2006
    #3
  4. Fred

    Fred Guest

    "Stephan Brönnimann" <> wrote in message
    news:...
    > Fred wrote:
    > > Hi I'm new to the std library.
    > > I have a function which reads data and creates a std string from it.
    > > I want to pass that back to the calling function as a parameter, not as

    the
    > > function return.
    > >
    > > I have tried various indirections etc but nothing works.
    > >
    > > I want something like this psudo type code:
    > >
    > > typedef basic_string<TCHAR> tstring; // string of TCHARs
    > >
    > > BOOL readFunction(tstring *pret)

    >
    > Use bool instead of BOOL
    >
    > > {
    > > tstring * ptstrpage = new tstring;

    > Why use a pointer here? The following is enough
    > tstring ptstrpage;
    >
    > > .
    > > .
    > > ptstrpage = loaded data

    >
    > Suspicous: who is owner of `loaded data'?
    > I guess it's type is TCHAR* for tstring*
    > => most probaly a memory leak.
    >
    > > pret=ptstrpage;
    > > return(TRUE)

    >
    > return true;
    >
    > Memory leak: ptstrpage never deleted.
    >
    > > }
    > >
    > > callingfunc
    > > {
    > >
    > > tstring * pData1,pData2;

    >
    > No memory allocated for pData1 and pData2!
    > Again: why pointers where normal variables would do?
    >
    > >
    > > if(!readFunction(pData1)
    > > {
    > > error processing
    > > }
    > >
    > >
    > > if(!readFunction(pData2)
    > > {
    > > error processing
    > > }
    > >
    > > delete pData1,pData2;
    > > }

    >
    > Make the signature
    > bool readFunction(tsting& dest);
    > and avoid the usage of pointers completely for the code you've posted.



    Sorry. The data will niot be deleted by that function. It was in my pseudo
    to show that I wished it to persist.
    It will "hang around" for a long time, and be accessed by several functions
    called at various times by the message loop.
    "loaded data" is a buffer into which he external data has been read.

    TCHAR lpReadBuff[BUFSIZE];

    In a loop I do

    *ptstrpage+=lpReadBuff;

    after each read.

    If I can pass ptstrpage back through the pointer parameter I wont need to
    delete it in the function, and if pret is defined globally I can delete it
    whenever and wherever I like.

    I realise my pseudo code didn't show the detail of what I wanted and has
    lead to misunderstanding - I just posted it like I did to show how I wanted
    the data returned through a pointer.

    Sorry about that.
     
    Fred, Feb 9, 2006
    #4
  5. Fred

    Fred Guest

    "Fred" <> wrote in message
    news:...
    >
    > "Stephan Brönnimann" <> wrote in message
    > news:...
    > > Fred wrote:
    > > > Hi I'm new to the std library.
    > > > I have a function which reads data and creates a std string from it.
    > > > I want to pass that back to the calling function as a parameter, not

    as
    > the
    > > > function return.
    > > >
    > > > I have tried various indirections etc but nothing works.
    > > >
    > > > I want something like this psudo type code:
    > > >
    > > > typedef basic_string<TCHAR> tstring; // string of TCHARs
    > > >
    > > > BOOL readFunction(tstring *pret)

    > >
    > > Use bool instead of BOOL
    > >
    > > > {
    > > > tstring * ptstrpage = new tstring;

    > > Why use a pointer here? The following is enough
    > > tstring ptstrpage;
    > >
    > > > .
    > > > .
    > > > ptstrpage = loaded data

    > >
    > > Suspicous: who is owner of `loaded data'?
    > > I guess it's type is TCHAR* for tstring*
    > > => most probaly a memory leak.
    > >
    > > > pret=ptstrpage;
    > > > return(TRUE)

    > >
    > > return true;
    > >
    > > Memory leak: ptstrpage never deleted.
    > >
    > > > }
    > > >
    > > > callingfunc
    > > > {
    > > >
    > > > tstring * pData1,pData2;

    > >
    > > No memory allocated for pData1 and pData2!


    I dont want to allocate memory (unless theres a std:string implication I
    dont understand).

    I want to padd the string created in the function back through a pointer. I
    dont want a new one.
     
    Fred, Feb 9, 2006
    #5
  6. Fred

    red floyd Guest

    Fred wrote:
    > Hi I'm new to the std library.
    > I have a function which reads data and creates a std string from it.
    > I want to pass that back to the calling function as a parameter, not as the
    > function return.
    >
    > I have tried various indirections etc but nothing works.
    >
    > [redacted]


    Simple:

    #include <string>
    void my_read(std::string& s)
    {
    s = loaded data; // PSEUDOCODE!!!!
    }

    int main()
    {
    std::string s;
    my_read(s);
    }
     
    red floyd, Feb 9, 2006
    #6
  7. Fred

    Fred Guest


    > I want to padd the string created in the function back through a pointer.

    I
    > dont want a new one.


    padd = pass
     
    Fred, Feb 9, 2006
    #7
  8. Fred wrote:
    > "Stephan Brönnimann" <> wrote in message
    > news:...
    > > Fred wrote:
    > > > Hi I'm new to the std library.
    > > > I have a function which reads data and creates a std string from it.
    > > > I want to pass that back to the calling function as a parameter, not as

    > the
    > > > function return.
    > > >
    > > > I have tried various indirections etc but nothing works.
    > > >
    > > > I want something like this psudo type code:
    > > >
    > > > typedef basic_string<TCHAR> tstring; // string of TCHARs
    > > >
    > > > BOOL readFunction(tstring *pret)

    > >
    > > Use bool instead of BOOL
    > >
    > > > {
    > > > tstring * ptstrpage = new tstring;

    > > Why use a pointer here? The following is enough
    > > tstring ptstrpage;
    > >
    > > > .
    > > > .
    > > > ptstrpage = loaded data

    > >
    > > Suspicous: who is owner of `loaded data'?
    > > I guess it's type is TCHAR* for tstring*
    > > => most probaly a memory leak.
    > >
    > > > pret=ptstrpage;
    > > > return(TRUE)

    > >
    > > return true;
    > >
    > > Memory leak: ptstrpage never deleted.
    > >
    > > > }
    > > >
    > > > callingfunc
    > > > {
    > > >
    > > > tstring * pData1,pData2;

    > >
    > > No memory allocated for pData1 and pData2!
    > > Again: why pointers where normal variables would do?
    > >
    > > >
    > > > if(!readFunction(pData1)
    > > > {
    > > > error processing
    > > > }
    > > >
    > > >
    > > > if(!readFunction(pData2)
    > > > {
    > > > error processing
    > > > }
    > > >
    > > > delete pData1,pData2;
    > > > }

    > >
    > > Make the signature
    > > bool readFunction(tsting& dest);
    > > and avoid the usage of pointers completely for the code you've posted.

    >
    >
    > Sorry. The data will niot be deleted by that function. It was in my pseudo
    > to show that I wished it to persist.
    > It will "hang around" for a long time, and be accessed by several functions
    > called at various times by the message loop.
    > "loaded data" is a buffer into which he external data has been read.
    >
    > TCHAR lpReadBuff[BUFSIZE];
    >
    > In a loop I do
    >
    > *ptstrpage+=lpReadBuff;
    >
    > after each read.
    >
    > If I can pass ptstrpage back through the pointer parameter I wont need to
    > delete it in the function, and if pret is defined globally I can delete it
    > whenever and wherever I like.
    >
    > I realise my pseudo code didn't show the detail of what I wanted and has
    > lead to misunderstanding - I just posted it like I did to show how I wanted
    > the data returned through a pointer.
    >
    > Sorry about that.


    Please post compilable minimum code that exhibits your problem.
    Before that all advice can only be hasardous.

    Regards, Stephan
     
    =?iso-8859-1?q?Stephan_Br=F6nnimann?=, Feb 9, 2006
    #8
  9. Fred

    Fred Guest

    "Josh Mcfarlane" <> wrote in message
    news:...
    > Fred wrote:
    > > BOOL readFunction(tstring *pret)

    >
    > This is creating a copy of the string pointer rather than allowing you
    > to modify pData1 and pData2.
    >
    > Try using a reference to the pointer:
    >
    > BOOL readFunction(tstring*& pret)
    >
    > Josh McFarlane
    >


    Yes that seems to work

    cheers one and all.
     
    Fred, Feb 9, 2006
    #9
  10. Fred

    Fred Guest

    "Stephan Brönnimann" <> wrote in message
    news:...
    Fred wrote:
    > "Stephan Brönnimann" <> wrote in message
    > news:...
    > > Fred wrote:
    > > > Hi I'm new to the std library.
    > > > I have a function which reads data and creates a std string from it.
    > > > I want to pass that back to the calling function as a parameter, not

    as
    > the
    > > > function return.
    > > >
    > > > I have tried various indirections etc but nothing works.
    > > >
    > > > I want something like this psudo type code:
    > > >
    > > > typedef basic_string<TCHAR> tstring; // string of TCHARs
    > > >
    > > > BOOL readFunction(tstring *pret)

    > >
    > > Use bool instead of BOOL
    > >
    > > > {
    > > > tstring * ptstrpage = new tstring;

    > > Why use a pointer here? The following is enough
    > > tstring ptstrpage;
    > >
    > > > .
    > > > .
    > > > ptstrpage = loaded data

    > >
    > > Suspicous: who is owner of `loaded data'?
    > > I guess it's type is TCHAR* for tstring*
    > > => most probaly a memory leak.
    > >
    > > > pret=ptstrpage;
    > > > return(TRUE)

    > >
    > > return true;
    > >
    > > Memory leak: ptstrpage never deleted.
    > >
    > > > }
    > > >
    > > > callingfunc
    > > > {
    > > >
    > > > tstring * pData1,pData2;

    > >
    > > No memory allocated for pData1 and pData2!
    > > Again: why pointers where normal variables would do?
    > >
    > > >
    > > > if(!readFunction(pData1)
    > > > {
    > > > error processing
    > > > }
    > > >
    > > >
    > > > if(!readFunction(pData2)
    > > > {
    > > > error processing
    > > > }
    > > >
    > > > delete pData1,pData2;
    > > > }

    > >
    > > Make the signature
    > > bool readFunction(tsting& dest);
    > > and avoid the usage of pointers completely for the code you've posted.

    >
    >
    > Sorry. The data will niot be deleted by that function. It was in my pseudo
    > to show that I wished it to persist.
    > It will "hang around" for a long time, and be accessed by several

    functions
    > called at various times by the message loop.
    > "loaded data" is a buffer into which he external data has been read.
    >
    > TCHAR lpReadBuff[BUFSIZE];
    >
    > In a loop I do
    >
    > *ptstrpage+=lpReadBuff;
    >
    > after each read.
    >
    > If I can pass ptstrpage back through the pointer parameter I wont need to
    > delete it in the function, and if pret is defined globally I can delete it
    > whenever and wherever I like.
    >
    > I realise my pseudo code didn't show the detail of what I wanted and has
    > lead to misunderstanding - I just posted it like I did to show how I

    wanted
    > the data returned through a pointer.
    >
    > Sorry about that.


    >Please post compilable minimum code that exhibits your problem.
    >Before that all advice can only be hasardous.


    >Regards, Stephan


    As I said earlier my attempts wouldn't compile :-(
     
    Fred, Feb 9, 2006
    #10
  11. Fred

    Gavin Deane Guest

    Fred wrote:
    > "Stephan Brönnimann" <> wrote in message
    > >Please post compilable minimum code that exhibits your problem.
    > >Before that all advice can only be hasardous.

    >
    > >Regards, Stephan

    >
    > As I said earlier my attempts wouldn't compile :-(


    Post a *minimal* piece of code that exhibits your problem.

    "compilable" in this context does not mean "can be compiled with no
    errors", it means "able to be copied and pasted into a compiler to
    demonstrate precisely the problem you are seeing". So copy and paste it
    exactly from your compiler into your message. Don't retype it. Don't
    elide bits and replace them with (...) or comments or pseudo-code.

    That way, people can copy and paste out of your message into their
    compiler and reproduce exactly your problem.

    If your problem is that your code doesn't compile, post the exact code.

    If your problem is that your code compiles but doesn't do what you
    expect, post the exact code.

    Either way, if you only post pseudo-code or a description of the code,
    you are obfuscating the question.

    HTH
    Gavin Deane
     
    Gavin Deane, Feb 9, 2006
    #11
  12. Fred

    Marcus Kwok Guest

    Stephan Br?nnimann <> wrote:
    > Fred wrote:
    >> tstring * pData1,pData2;

    >
    > No memory allocated for pData1 and pData2!


    Actually, only pData1 is a pointer to a tstring. pData2 is a tstring,
    not a pointer.

    --
    Marcus Kwok
     
    Marcus Kwok, Feb 9, 2006
    #12
    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.

Share This Page