473,385 Members | 1,707 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,385 software developers and data experts.

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

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
Jul 22 '05 #1
11 2459

"Sims" <si*********@hotmail.com> wrote in message
news:bu************@ID-162430.news.uni-berlin.de...
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 <cstring> for this

// 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.
Jul 22 '05 #2

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
Jul 22 '05 #3

"Sims" <si*********@hotmail.com> wrote in message
news:bu************@ID-162430.news.uni-berlin.de...

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.
Jul 22 '05 #4

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.

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

Jul 22 '05 #5

"Sims" <si*********@hotmail.com> wrote in message
news:bu************@ID-162430.news.uni-berlin.de...

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.


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


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

Jul 22 '05 #6
>
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

Jul 22 '05 #7
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.
Jul 22 '05 #8
"Alex Tibbles" <al**********@yahoo.co.uk> wrote in message news:<40***********************@mercury.nildram.ne t>...
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> :-)
Jul 22 '05 #9

"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:bu************@ID-221354.news.uni-berlin.de...
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
Jul 22 '05 #10

"Sims" <si*********@hotmail.com> wrote in message
news:bu************@ID-162430.news.uni-berlin.de...
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.
Jul 22 '05 #11

"Frank Puck" <ne***********@sbcglobal.net> wrote in message
news:B4******************@newssvr25.news.prodigy.c om...

"Sims" <si*********@hotmail.com> wrote in message
news:bu************@ID-162430.news.uni-berlin.de...
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
Jul 22 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: James Vanns | last post by:
I want to be able to print out (and read in) characters with accents (for example French and Italian text). So far I have this: std::locale lang (getenv ("LANG")); which seems to set the...
9
by: Jim Langston | last post by:
#include <string> int main () { std::string MyString = "Testing"; MyString = " " + MyString; } This works in Microsoft Visual C++ .net 2003
2
by: anelma via .NET 247 | last post by:
Following code works fine, when compiled with VS 6.0, but not anymore when compiled in .NET. What's wrong here, I can't see it by myself? arrString content will be garbage with .net compilation, but...
6
by: SteelSide | last post by:
Ive searched and searched,but havent been able to get it to work. ----------------- #include <iostream> using namespace std; std::string tempHostNameStr = "s1e2.hidden.thingy.org"; char...
5
by: Tom Smith | last post by:
I hardly dare ask this given the furore in another thread over strings and const... My problem is this. I am assured that casting away the constness of the return value of std::string::c_str() is...
5
by: Stefan Naewe | last post by:
A simple test: <----------------------------> #include <string> #include <iostream> int main() { std::string h("Hello "); *h.rbegin() = '\0';
11
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" //...
29
by: aarthi28 | last post by:
Hi, I have written this code, and at the end, I am trying to write a vector of strings into a text file. However, my program is nor compiling, and it gives me the following error when I try to...
5
by: Olaf | last post by:
Hi, I wrap a legacy C library, e.g. the signature is void set_error_buffer(char* buf); where the buf length should be of length of 512 (it's defined). Now I want to wrap it with...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.