468,241 Members | 1,571 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,241 developers. It's quick & easy.

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


Feb 9 '06 #1
11 3820
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

Feb 9 '06 #2
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

Feb 9 '06 #3

"Stephan Brönnimann" <br****@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr 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.
Feb 9 '06 #4

"Fred" <no*@home.com> wrote in message
news:lM******************************@pipex.net...

"Stephan Brönnimann" <br****@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr 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.
Feb 9 '06 #5
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);
}
Feb 9 '06 #6

I want to padd the string created in the function back through a pointer. I dont want a new one.


padd = pass
Feb 9 '06 #7
Fred wrote:
"Stephan Brönnimann" <br****@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr 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

Feb 9 '06 #8

"Josh Mcfarlane" <da*****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr 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.
Feb 9 '06 #9

"Stephan Brönnimann" <br****@hotmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Fred wrote:
"Stephan Brönnimann" <br****@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr 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 :-(
Feb 9 '06 #10

Fred wrote:
"Stephan Brönnimann" <br****@hotmail.com> 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

Feb 9 '06 #11
Stephan Br?nnimann <br****@hotmail.com> 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
Feb 9 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by Espen Ruud Schultz | last post: by
3 posts views Thread by Paul Kirby | last post: by
5 posts views Thread by Alfonso Morra | last post: by
12 posts views Thread by jl_post | last post: by
8 posts views Thread by Jason Heyes | last post: by
6 posts views Thread by Erik | last post: by
10 posts views Thread by mr_sorcerer | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.