std::string and char*, what am i doing wrong?

S

Sims

Hi,

I have a function that looks something like ....

void Function( std::string &oldval )
{
int iSome_size = xxx; // get some size
char *tmp = NULL;
tmp = new char[iSome_size+1];

// get some text into the tmp

// and then copy the data to the std::string(...)
oldval = tmp;
delete [] tmp;
tmp = NULL;
}

// but the value passed to oldval is wrong, oldval gets allocated some
garbage.
// what am i missing?

Many thanks

Sims
 
S

Sumit Rajan

Sims said:
Hi,

I have a function that looks something like ....

void Function( std::string &oldval )
{
int iSome_size = xxx; // get some size
char *tmp = NULL;
tmp = new char[iSome_size+1];

// get some text into the tmp

How did you "get some text" into temp? Try to use a mechanism like this:

std::strcpy(tmp,"Good");
// you will need to inlcude said:
// and then copy the data to the std::string(...)
oldval = tmp;
delete [] tmp;
tmp = NULL;
}

// but the value passed to oldval is wrong, oldval gets allocated some
garbage.
// what am i missing?

Many thanks

Sims

Regards,
Sumit.
 
S

Sims

I have a function that looks something like ....

void Function( std::string &oldval )
{
int iSome_size = xxx; // get some size
char *tmp = NULL;
tmp = new char[iSome_size+1];

// get some text into the tmp

How did you "get some text" into temp? Try to use a mechanism like this:

Well, i am using a Window API,

RegQueryValueEx(...) and the value returned is corect.
When i am tracing the code i can see that the value is good.
But as soon as i try to copy it to my std::string it does not work.

Sims
 
S

Sharad Kala

Sims said:
I have a function that looks something like ....

void Function( std::string &oldval )
{
int iSome_size = xxx; // get some size
char *tmp = NULL;
tmp = new char[iSome_size+1];

// get some text into the tmp

How did you "get some text" into temp? Try to use a mechanism like this:

Well, i am using a Window API,

RegQueryValueEx(...) and the value returned is corect.
When i am tracing the code i can see that the value is good.
But as soon as i try to copy it to my std::string it does not work.

How about posting the whole code.
May be you can separate out the Windows API with a dummy function and then try
showing the code.
Someone would be then able to help you.

-Sharad
P.S : Try running the code under a debugger.
 
S

Sims

How about posting the whole code.
May be you can separate out the Windows API with a dummy function and then try
showing the code.
Someone would be then able to help you.

It is a window API so i am affraid it would be OT.
All i do is read a registery value. I then copy the data to a std::string
-Sharad
P.S : Try running the code under a debugger.

That is what i did and that is where i noticed the problem.

if i do,

std::string test = "Hello";

I can see in the debugger that test="Hello".

But if i do

char * tmp = new char[10+1];
strcpy( tmp, "Tested" );
test = tmp;

the debugger shows
test == "0x)()!I())@#" // <== Garbage
but is correct and test.c_str() == "Tested";

I am no longer sure if it is a problem, (i am not used to .NET so i don't
know if the values displayed are right).

Sims
 
S

Sharad Kala

Sims said:
It is a window API so i am affraid it would be OT.
All i do is read a registery value. I then copy the data to a std::string


That is what i did and that is where i noticed the problem.

if i do,

std::string test = "Hello";

I can see in the debugger that test="Hello".

Try this out -
After using the Windows API you must be getting the data to be copied into a
buffer.
Print that out before copying to the string.
I think most probably you are not using the API correctly.
If you are unsure as to what the debugger is showing , use simple print
statements and verify that the data is indeed valid.


..
 
S

Sims

Try this out -
After using the Windows API you must be getting the data to be copied into a
buffer.
Print that out before copying to the string.
I think most probably you are not using the API correctly.
If you are unsure as to what the debugger is showing , use simple print
statements and verify that the data is indeed valid.

No, it is printing it out ok. The value is fine, (i can compare it with the
reg value).
From what i can see it is the debugger that is not displaying it right.

if i do

std::string a = "Hello"; // displays ok
std::string b = tmp; // only displays b.c_str() ok, (when tmp is a
char*).

I still have a nagging feeling that i am not doing something right thought.
I cannot see the MS boys overlooking that case.

Sims
 
A

Alex Tibbles

perhaps your debugger is not interpreting the string properly and just
printing its address (the stuff starting 0x...). gdb does this for
std::string cos it doesn't know that a std::string is a string.
 
N

Niklas Borson

Alex Tibbles said:
perhaps your debugger is not interpreting the string properly and just
printing its address (the stuff starting 0x...). gdb does this for
std::string cos it doesn't know that a std::string is a string.

I suspect that's the problem here. Your code seems correct, and
the value of the std::string is actually correct, but it looks
wrong in your debugger.

I've experienced this myself in Visual Studio 7, but it was fixed
in 7.1 if I recall correctly. The issue is that beginning in v 7,
the implementation of std::basic_string (from Dinkumware) used the
small string optimization. If you look at the source, you'll see
the following union:

union _Bxty
{ // storage for small buffer or pointer to larger one
_Elem _Buf[_BUF_SIZE];
_Elem *_Ptr;
} _Bx;

A short string is stored in _Buf, in which case _Ptr is garbage.
The debugger took a while to catch up with this. So in 7.0 (but
not IIRC in 7.1) the debugger only knew about _Ptr. So for a
small string, the debugger displayed the contents of a "random"
location.

<sop_to_OT_police>
If you think about it, this is kind of a tricky thing for a
debugger to get right so you may see similar behavior on other
implementations that use this technique.
</sop_to_OT_police> :)
 
F

Frank Puck

Sharad Kala said:
May be you can separate out the Windows API with a dummy function and then try
showing the code.


this would certainly help to avoid offending some people which do
programming without an underlying os -- come real problems require an
underlying OS
 
F

Frank Puck

void Function( std::string &oldval )
{
int iSome_size = xxx; // get some size
char *tmp = NULL;
tmp = new char[iSome_size+1];

// get some text into the tmp

// and then copy the data to the std::string(...)
oldval = tmp;
delete [] tmp;
tmp = NULL;
}

First using a writeable reference is kind of code obfuscation -- some people
may disagree here.
Why don't you use a fixed size buffer -- this would avoid all the problems
with allocating some buffer.

void Function(std::string *pOldValue)
{
char ac[1024];

....

*pOldValue = ac;
}

If you really need a dynamic buffer, use


std::vector<char> sBuffer(someSize);

This avoids that you have to manually destroy the buffer.
It is also exception safe
-- remember that between new and delete somebody may throw an exception.
 
V

VANNA CHHUM

Frank Puck said:
void Function( std::string &oldval )
{
int iSome_size = xxx; // get some size
char *tmp = NULL;
tmp = new char[iSome_size+1];

// get some text into the tmp

// and then copy the data to the std::string(...)
oldval = tmp;
delete [] tmp;
tmp = NULL;
}

Why not just put the data directly into oldval? std::string is very clean
relative to raw char*'s. std::string will dynamically resize for you, so
you don't need to worry about buffer sizes (don't use std::vector<char> as
someone else suggested).

std::cin >> oldVal; // one possibility from standard input
oldVal = *(someCFunctionThatReturnsCharPointer( ));

Most likely, you can get rid of this Function altogether and simply use a
std::string at the call site. It looks like you are worried about making
conversions between char* and std::string - you don't need to worry about
such things.

Good Luck,
Shane
 

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

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,383
Latest member
EzraGiffor

Latest Threads

Top