Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > returning an std:string pointer as a parameter

Reply
Thread Tools

returning an std:string pointer as a parameter

 
 
Fred
Guest
Posts: n/a
 
      02-09-2006
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;
}






 
Reply With Quote
 
 
 
 
Josh Mcfarlane
Guest
Posts: n/a
 
      02-09-2006
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

 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Stephan_Br=F6nnimann?=
Guest
Posts: n/a
 
      02-09-2006
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

 
Reply With Quote
 
Fred
Guest
Posts: n/a
 
      02-09-2006

"Stephan Brönnimann" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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.


 
Reply With Quote
 
Fred
Guest
Posts: n/a
 
      02-09-2006

"Fred" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "Stephan Brönnimann" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> > 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.


 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      02-09-2006
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);
}
 
Reply With Quote
 
Fred
Guest
Posts: n/a
 
      02-09-2006


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

I
> dont want a new one.


padd = pass


 
Reply With Quote
 
=?iso-8859-1?q?Stephan_Br=F6nnimann?=
Guest
Posts: n/a
 
      02-09-2006
Fred wrote:
> "Stephan Brönnimann" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> > 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

 
Reply With Quote
 
Fred
Guest
Posts: n/a
 
      02-09-2006

"Josh Mcfarlane" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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.


 
Reply With Quote
 
Fred
Guest
Posts: n/a
 
      02-09-2006

"Stephan Brönnimann" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
Fred wrote:
> "Stephan Brönnimann" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> > 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments