returning an std:string pointer as a parameter

F

Fred

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;
}
 
J

Josh Mcfarlane

Fred said:
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
 
?

=?iso-8859-1?q?Stephan_Br=F6nnimann?=

Fred said:
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
 
F

Fred

Stephan Brönnimann said:
Use bool instead of BOOL

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


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


return true;

Memory leak: ptstrpage never deleted.


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


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.
 
F

Fred

Fred said:

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.
 
R

red floyd

Fred said:
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);
}
 
?

=?iso-8859-1?q?Stephan_Br=F6nnimann?=

Fred said:
Stephan Brönnimann said:
Use bool instead of BOOL

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


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


return true;

Memory leak: ptstrpage never deleted.


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


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
 
F

Fred

Josh Mcfarlane said:
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.
 
F

Fred

Fred said:
as
the

Use bool instead of BOOL

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


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


return true;

Memory leak: ptstrpage never deleted.


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


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 :-(
 
G

Gavin Deane

Fred said:
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
 
M

Marcus Kwok

Stephan Br?nnimann said:
No memory allocated for pData1 and pData2!

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top