returning an std:string pointer as a parameter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fred

    returning an std:string pointer as a parameter

    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<TC HAR> tstring; // string of TCHARs

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

    callingfunc
    {

    tstring * pData1,pData2;

    if(!readFunctio n(pData1)
    {
    error processing
    }


    if(!readFunctio n(pData2)
    {
    error processing
    }

    delete pData1,pData2;
    }






  • Josh Mcfarlane

    #2
    Re: returning an std:string pointer as a parameter

    Fred wrote:[color=blue]
    > BOOL readFunction(ts tring *pret)[/color]

    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(ts tring*& pret)

    Josh McFarlane

    Comment

    • Stephan Brönnimann

      #3
      Re: returning an std:string pointer as a parameter

      Fred wrote:[color=blue]
      > 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<TC HAR> tstring; // string of TCHARs
      >
      > BOOL readFunction(ts tring *pret)[/color]

      Use bool instead of BOOL
      [color=blue]
      > {
      > tstring * ptstrpage = new tstring;[/color]
      Why use a pointer here? The following is enough
      tstring ptstrpage;
      [color=blue]
      > .
      > .
      > ptstrpage = loaded data[/color]

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

      return true;

      Memory leak: ptstrpage never deleted.
      [color=blue]
      > }
      >
      > callingfunc
      > {
      >
      > tstring * pData1,pData2;[/color]

      No memory allocated for pData1 and pData2!
      Again: why pointers where normal variables would do?
      [color=blue]
      >
      > if(!readFunctio n(pData1)
      > {
      > error processing
      > }
      >
      >
      > if(!readFunctio n(pData2)
      > {
      > error processing
      > }
      >
      > delete pData1,pData2;
      > }[/color]

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

      Regards, Stephan

      Comment

      • Fred

        #4
        Re: returning an std:string pointer as a parameter


        "Stephan Brönnimann" <broeni@hotmail .com> wrote in message
        news:1139504433 .568189.131570@ f14g2000cwb.goo glegroups.com.. .[color=blue]
        > Fred wrote:[color=green]
        > > 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[/color][/color]
        the[color=blue][color=green]
        > > function return.
        > >
        > > I have tried various indirections etc but nothing works.
        > >
        > > I want something like this psudo type code:
        > >
        > > typedef basic_string<TC HAR> tstring; // string of TCHARs
        > >
        > > BOOL readFunction(ts tring *pret)[/color]
        >
        > Use bool instead of BOOL
        >[color=green]
        > > {
        > > tstring * ptstrpage = new tstring;[/color]
        > Why use a pointer here? The following is enough
        > tstring ptstrpage;
        >[color=green]
        > > .
        > > .
        > > ptstrpage = loaded data[/color]
        >
        > Suspicous: who is owner of `loaded data'?
        > I guess it's type is TCHAR* for tstring*
        > => most probaly a memory leak.
        >[color=green]
        > > pret=ptstrpage;
        > > return(TRUE)[/color]
        >
        > return true;
        >
        > Memory leak: ptstrpage never deleted.
        >[color=green]
        > > }
        > >
        > > callingfunc
        > > {
        > >
        > > tstring * pData1,pData2;[/color]
        >
        > No memory allocated for pData1 and pData2!
        > Again: why pointers where normal variables would do?
        >[color=green]
        > >
        > > if(!readFunctio n(pData1)
        > > {
        > > error processing
        > > }
        > >
        > >
        > > if(!readFunctio n(pData2)
        > > {
        > > error processing
        > > }
        > >
        > > delete pData1,pData2;
        > > }[/color]
        >
        > Make the signature
        > bool readFunction(ts ting& dest);
        > and avoid the usage of pointers completely for the code you've posted.[/color]


        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+=lpR eadBuff;

        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 misunderstandin g - I just posted it like I did to show how I wanted
        the data returned through a pointer.

        Sorry about that.


        Comment

        • Fred

          #5
          Re: returning an std:string pointer as a parameter


          "Fred" <not@home.com > wrote in message
          news:lM-dnW-G7dqa4HbenZ2dnU VZ8qidnZ2d@pipe x.net...[color=blue]
          >
          > "Stephan Brönnimann" <broeni@hotmail .com> wrote in message
          > news:1139504433 .568189.131570@ f14g2000cwb.goo glegroups.com.. .[color=green]
          > > Fred wrote:[color=darkred]
          > > > 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[/color][/color][/color]
          as[color=blue]
          > the[color=green][color=darkred]
          > > > function return.
          > > >
          > > > I have tried various indirections etc but nothing works.
          > > >
          > > > I want something like this psudo type code:
          > > >
          > > > typedef basic_string<TC HAR> tstring; // string of TCHARs
          > > >
          > > > BOOL readFunction(ts tring *pret)[/color]
          > >
          > > Use bool instead of BOOL
          > >[color=darkred]
          > > > {
          > > > tstring * ptstrpage = new tstring;[/color]
          > > Why use a pointer here? The following is enough
          > > tstring ptstrpage;
          > >[color=darkred]
          > > > .
          > > > .
          > > > ptstrpage = loaded data[/color]
          > >
          > > Suspicous: who is owner of `loaded data'?
          > > I guess it's type is TCHAR* for tstring*
          > > => most probaly a memory leak.
          > >[color=darkred]
          > > > pret=ptstrpage;
          > > > return(TRUE)[/color]
          > >
          > > return true;
          > >
          > > Memory leak: ptstrpage never deleted.
          > >[color=darkred]
          > > > }
          > > >
          > > > callingfunc
          > > > {
          > > >
          > > > tstring * pData1,pData2;[/color]
          > >
          > > No memory allocated for pData1 and pData2![/color][/color]

          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.


          Comment

          • red floyd

            #6
            Re: returning an std:string pointer as a parameter

            Fred wrote:[color=blue]
            > 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][/color]

            Simple:

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

            int main()
            {
            std::string s;
            my_read(s);
            }

            Comment

            • Fred

              #7
              Re: returning an std:string pointer as a parameter


              [color=blue]
              > I want to padd the string created in the function back through a pointer.[/color]
              I[color=blue]
              > dont want a new one.[/color]

              padd = pass


              Comment

              • Stephan Brönnimann

                #8
                Re: returning an std:string pointer as a parameter

                Fred wrote:[color=blue]
                > "Stephan Brönnimann" <broeni@hotmail .com> wrote in message
                > news:1139504433 .568189.131570@ f14g2000cwb.goo glegroups.com.. .[color=green]
                > > Fred wrote:[color=darkred]
                > > > 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[/color][/color]
                > the[color=green][color=darkred]
                > > > function return.
                > > >
                > > > I have tried various indirections etc but nothing works.
                > > >
                > > > I want something like this psudo type code:
                > > >
                > > > typedef basic_string<TC HAR> tstring; // string of TCHARs
                > > >
                > > > BOOL readFunction(ts tring *pret)[/color]
                > >
                > > Use bool instead of BOOL
                > >[color=darkred]
                > > > {
                > > > tstring * ptstrpage = new tstring;[/color]
                > > Why use a pointer here? The following is enough
                > > tstring ptstrpage;
                > >[color=darkred]
                > > > .
                > > > .
                > > > ptstrpage = loaded data[/color]
                > >
                > > Suspicous: who is owner of `loaded data'?
                > > I guess it's type is TCHAR* for tstring*
                > > => most probaly a memory leak.
                > >[color=darkred]
                > > > pret=ptstrpage;
                > > > return(TRUE)[/color]
                > >
                > > return true;
                > >
                > > Memory leak: ptstrpage never deleted.
                > >[color=darkred]
                > > > }
                > > >
                > > > callingfunc
                > > > {
                > > >
                > > > tstring * pData1,pData2;[/color]
                > >
                > > No memory allocated for pData1 and pData2!
                > > Again: why pointers where normal variables would do?
                > >[color=darkred]
                > > >
                > > > if(!readFunctio n(pData1)
                > > > {
                > > > error processing
                > > > }
                > > >
                > > >
                > > > if(!readFunctio n(pData2)
                > > > {
                > > > error processing
                > > > }
                > > >
                > > > delete pData1,pData2;
                > > > }[/color]
                > >
                > > Make the signature
                > > bool readFunction(ts ting& dest);
                > > and avoid the usage of pointers completely for the code you've posted.[/color]
                >
                >
                > 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+=lpR eadBuff;
                >
                > 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 misunderstandin g - I just posted it like I did to show how I wanted
                > the data returned through a pointer.
                >
                > Sorry about that.[/color]

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

                Regards, Stephan

                Comment

                • Fred

                  #9
                  Re: returning an std:string pointer as a parameter


                  "Josh Mcfarlane" <darsant@gmail. com> wrote in message
                  news:1139503023 .517585.313840@ g43g2000cwa.goo glegroups.com.. .[color=blue]
                  > Fred wrote:[color=green]
                  > > BOOL readFunction(ts tring *pret)[/color]
                  >
                  > 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(ts tring*& pret)
                  >
                  > Josh McFarlane
                  >[/color]

                  Yes that seems to work

                  cheers one and all.


                  Comment

                  • Fred

                    #10
                    Re: returning an std:string pointer as a parameter


                    "Stephan Brönnimann" <broeni@hotmail .com> wrote in message
                    news:1139507057 .263051.20420@z 14g2000cwz.goog legroups.com...
                    Fred wrote:[color=blue]
                    > "Stephan Brönnimann" <broeni@hotmail .com> wrote in message
                    > news:1139504433 .568189.131570@ f14g2000cwb.goo glegroups.com.. .[color=green]
                    > > Fred wrote:[color=darkred]
                    > > > 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[/color][/color][/color]
                    as[color=blue]
                    > the[color=green][color=darkred]
                    > > > function return.
                    > > >
                    > > > I have tried various indirections etc but nothing works.
                    > > >
                    > > > I want something like this psudo type code:
                    > > >
                    > > > typedef basic_string<TC HAR> tstring; // string of TCHARs
                    > > >
                    > > > BOOL readFunction(ts tring *pret)[/color]
                    > >
                    > > Use bool instead of BOOL
                    > >[color=darkred]
                    > > > {
                    > > > tstring * ptstrpage = new tstring;[/color]
                    > > Why use a pointer here? The following is enough
                    > > tstring ptstrpage;
                    > >[color=darkred]
                    > > > .
                    > > > .
                    > > > ptstrpage = loaded data[/color]
                    > >
                    > > Suspicous: who is owner of `loaded data'?
                    > > I guess it's type is TCHAR* for tstring*
                    > > => most probaly a memory leak.
                    > >[color=darkred]
                    > > > pret=ptstrpage;
                    > > > return(TRUE)[/color]
                    > >
                    > > return true;
                    > >
                    > > Memory leak: ptstrpage never deleted.
                    > >[color=darkred]
                    > > > }
                    > > >
                    > > > callingfunc
                    > > > {
                    > > >
                    > > > tstring * pData1,pData2;[/color]
                    > >
                    > > No memory allocated for pData1 and pData2!
                    > > Again: why pointers where normal variables would do?
                    > >[color=darkred]
                    > > >
                    > > > if(!readFunctio n(pData1)
                    > > > {
                    > > > error processing
                    > > > }
                    > > >
                    > > >
                    > > > if(!readFunctio n(pData2)
                    > > > {
                    > > > error processing
                    > > > }
                    > > >
                    > > > delete pData1,pData2;
                    > > > }[/color]
                    > >
                    > > Make the signature
                    > > bool readFunction(ts ting& dest);
                    > > and avoid the usage of pointers completely for the code you've posted.[/color]
                    >
                    >
                    > 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[/color]
                    functions[color=blue]
                    > 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+=lpR eadBuff;
                    >
                    > 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 misunderstandin g - I just posted it like I did to show how I[/color]
                    wanted[color=blue]
                    > the data returned through a pointer.
                    >
                    > Sorry about that.[/color]
                    [color=blue]
                    >Please post compilable minimum code that exhibits your problem.
                    >Before that all advice can only be hasardous.[/color]
                    [color=blue]
                    >Regards, Stephan[/color]

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


                    Comment

                    • Gavin Deane

                      #11
                      Re: returning an std:string pointer as a parameter


                      Fred wrote:[color=blue]
                      > "Stephan Brönnimann" <broeni@hotmail .com> wrote in message[color=green]
                      > >Please post compilable minimum code that exhibits your problem.
                      > >Before that all advice can only be hasardous.[/color]
                      >[color=green]
                      > >Regards, Stephan[/color]
                      >
                      > As I said earlier my attempts wouldn't compile :-([/color]

                      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

                      Comment

                      • Marcus Kwok

                        #12
                        Re: returning an std:string pointer as a parameter

                        Stephan Br?nnimann <broeni@hotmail .com> wrote:[color=blue]
                        > Fred wrote:[color=green]
                        >> tstring * pData1,pData2;[/color]
                        >
                        > No memory allocated for pData1 and pData2![/color]

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

                        --
                        Marcus Kwok

                        Comment

                        Working...