472,955 Members | 2,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,955 software developers and data experts.

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 3979
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Espen Ruud Schultz | last post by:
Lets say I have a char pointer and an std::string. Is it possible to get a pointer to the std::string's "content" so that the char pointer can point to the same text? And vice versa; can I give...
24
by: Julie | last post by:
I'm re-evaluating the way that I convert from a std::string to char *. (Requirement: the source is a std::string, the usable contents are char *) Here is what I've come up with: #include...
3
by: Paul Kirby | last post by:
Hello All I am trying to create a function that returns a string like the following: // defined in headerfile // void test_func(char *retstr); // -------------------- // // c/cpp File //
2
by: Susan Baker | last post by:
Hi, I have declared a class MyClass in a header file MyClass.h I have then gone onto define the class in MyClass.cpp. This is (roughly) what the definition (.cpp) file looks like: #include...
5
by: Alfonso Morra | last post by:
Hi, What is the recomended way of returning an STL container (e.g. std::string, std::vector etc fom a function? Is it by simply returning a local variable? (I doubt it) std::string...
12
by: jl_post | last post by:
Dear C++ community, I have a question regarding the size of C++ std::strings. Basically, I compiled the following code under two different compilers: std::string someString = "Hello, world!";...
8
by: Jason Heyes | last post by:
If s is a std::string, does &s refer to the contiguous block of characters representing s?
6
by: Erik | last post by:
Hello, For many years ago I implemented my own string buffer class, which works fine except assignments - it copies the char* buffer instead of the pointer. Therefore in function calls I pass...
10
by: mr_sorcerer | last post by:
Hi! I just found something interesting. I mean what do you think about this: char *p = 0; std::string str = p; Why std::string doesn't check null pointers?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.