std :: string和char *,我做错了什么? [英] std::string and char*, what am i doing wrong?

查看:69
本文介绍了std :: string和char *,我做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个看起来像......的函数。


void函数(std :: string& oldval)

{

int iSome_size = xxx; //得到一些大小

char * tmp = NULL;

tmp = new char [iSome_size + 1];


/ /获取一些文本到tmp


//然后将数据复制到std :: string(...)

oldval = tmp;

删除[] tmp;

tmp = NULL;

}


//但值传递给oldval是错误的,oldval被分配了一些

垃圾。

//我错过了什么?


非常感谢


Sims

解决方案



" Sims" < SI ********* @ hotmail.com>在留言中写道

news:bu ************ @ ID-162430.news.uni-berlin.de ...

你好,

我有一个看起来像......的函数。

void函数(std :: string& oldval)
{
int iSome_size = xxx; //得到一些大小
char * tmp = NULL;
tmp = new char [iSome_size + 1];

//将一些文本输入tmp


你是怎么得到一些文字的?进入临时?尝试使用这样的机制:


std :: strcpy(tmp," Good");

//你将需要inlcude<为c_string>为此
//然后将数据复制到std :: string(...)
oldval = tmp;
delete [] tmp;
tmp = NULL;
}
//但是传递给oldval的值是错误的,oldval被分配了一些垃圾。
//我错过了什么?

非常感谢

模拟人生




问候,

Sumit。



我的功能看起来像......

void Function(std :: string& oldval)
{
int iSome_size = xxx; //得到一些大小
char * tmp = NULL;
tmp = new char [iSome_size + 1];

//将一些文本输入tmp



你是怎么得到一些文字的?进入临时?尝试使用这样的机制:




好​​吧,我正在使用Window API,


RegQueryValueEx(...并且返回的值是corect。

当我跟踪代码时,我可以看到值很好。

但是一旦我尝试将它复制到我的std :: string它不起作用。


Sims




" Sims" < SI ********* @ hotmail.com>在消息中写道

news:bu ************ @ ID-162430.news.uni-berlin.de ...

< blockquote class =post_quotes>


我有一个看起来像......的函数。

void函数(std :: string& oldval)
{
int iSome_size = xxx; //得到一些大小
char * tmp = NULL;
tmp = new char [iSome_size + 1];

//将一些文本输入tmp



你是怎么得到一些文字的?进入临时?尝试使用这样的机制:



好吧,我正在使用Window API,RegQueryValueEx(...),返回的值是corect。
当我跟踪代码时,我可以看到值很好。
但是当我尝试将它复制到我的std :: string时它不起作用。




如何发布整个代码。

可能是你可以用虚拟功能分离Windows API然后尝试

显示代码。

有人可以帮助你。


-Sharad

PS:尝试在调试器下运行代码。


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

解决方案


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



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



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


这篇关于std :: string和char *,我做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆