473,395 Members | 2,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 3992
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?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.