另一个动态记忆疑问 [英] Another dynamic memory doubt

查看:49
本文介绍了另一个动态记忆疑问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友:


我有以下代码,


char * MyClass :: MyMethod()

{

char * ptr = new char [6]; //用于NULL终止符的空格

ptr =" hello";

return ptr;

delete [] ptr;

}


我要做的是返回ptr,但之后删除[]。在上面的

代码中,我相信删除[]行不会在

返回语句后执行。


如何我应该这样做吗?


谢谢。

Hello friends:

I have the following code,

char* MyClass::MyMethod()
{
char* ptr = new char[6]; // space for NULL terminator
ptr = "hello";
return ptr;
delete[] ptr;
}

What I''m trying to do is return ptr, but delete[] it afterwards. In the
code above, I believe the delete[] line will not be executed following
the return statement.

How should I achieve this?

Thanks.

推荐答案

2008年4月26日9:46,pradeep写道:
On 26 Apr 2008 at 9:46, pradeep wrote:

我有以下代码,
I have the following code,



您的代码中存在一些问题和误解。 ..

There are several problems and misunderstandings in your code...


char * MyClass :: MyMethod()
char* MyClass::MyMethod()



在C ++中,它通常是最好#include< string然后使用

std :: string而不是C风格的字符串。如果需要,你可以使用c_str()方法返回一个好的旧的

char *。

In C++, it''s usually better to #include <stringand then use
std::string instead of C-style strings. You can always get a good old
char * back if necessary by using the c_str() method.


{

char * ptr = new char [6]; //用于NULL终止符的空格

ptr =" hello" ;;
{
char* ptr = new char[6]; // space for NULL terminator
ptr = "hello";



这不会复制字符串hello进入数组你只需要分配
,而是让ptr指向一个字符串文字的开头,

将存储在你对象的数据段中文件。


这样做的结果是你已经泄漏了之前在线上分配的内存,并试图删除ptr会导致问题。

This does not copy the string "hello" into the array you''ve just
allocated, but rather makes ptr point to the start of a string literal,
which will be stored in the data segment of your object file.

A consequence of this is that you have leaked the memory you allocated
on the line before, and trying to delete ptr will cause problems.


return ptr;

delete [] ptr;

}


我要做的是返回ptr,但之后删除[]。在上面的

代码中,我相信删除[]行不会在

返回语句后执行。
return ptr;
delete[] ptr;
}

What I''m trying to do is return ptr, but delete[] it afterwards. In the
code above, I believe the delete[] line will not be executed following
the return statement.



当然不是!返回语句会向上移动一个堆栈帧,并在

中将程序计数器设置为调用

函数时的位置。

Of course not! A return statement moves you up a stack frame, and in
particular sets the program counter to where it was when you called the
function.


我应该如何实现这一目标?
How should I achieve this?



在你的函数的文档中,解释它分配一些

内存并且调用者负责删除ptr。


或者根本不分配内存,但只需要


char * MyClass :: MyMethod()

{

return" hello";

}

In the documentation for your function, explain that it allocates some
memory and that the caller is responsible for deleting ptr.

Or don''t allocate memory at all, but just have

char* MyClass::MyMethod()
{
return "hello";
}


pradeep写道:
pradeep wrote:

朋友们:


我有以下代码,


char * MyClass :: MyMethod()
Hello friends:

I have the following code,

char* MyClass::MyMethod()



这是C ++。有一个专门的C ++小组叫做comp.lang.c ++。

试试吧。


< snip>

This is C++. There is a dedicated group for C++ called comp.lang.c++.
Try that.

<snip>


pradeep写道:
pradeep wrote:

你好朋友:


我有以下代码,


char * MyClass :: MyMethod()
Hello friends:

I have the following code,

char* MyClass::MyMethod()



^^^^^^^^此行是语法错误在C.

^^^^^^^^ This line is a syntax error in C.


{

char * ptr = new char [6]; //用于NULL终止符的空格
{
char* ptr = new char[6]; // space for NULL terminator



^^^^^^^^此行是C语法错误。

^^^^^^^^ This line is a syntax error in C.


ptr =" hello" ;;
ptr = "hello";



^^^^^^^^如果前面的非C行为char [6]和

分配了空间ptr的第一个元素的地址,然后

这行创建了一个内存泄漏并且丢失了地址

char [6]数组。

^^^^^^^^ If the preceding non-C line allocated space for a char[6] and
assigned the address of the first element to ptr, then
this line creates a memory leak and loses the address of
the char[6] array.


返回ptr;

delete [] ptr;
return ptr;
delete[] ptr;



^^^^^^^^这一行是C中的语法错误,即使它不是,

之前的回报让这条线无法到达。

^^^^^^^^ This line is a syntax error in C, and even if it weren''t,
the return in the line before makes this line unreachable.


}


我要做的就是返回ptr,但之后删除[]。
}

What I''m trying to do is return ptr, but delete[] it afterwards.



用愚蠢的语言你是非常滥用,你需要单独删除数组
。但是,由于您已经分配了匿名字符hello的

addess。到了ptr,这几乎可以保证在你的脸上爆炸。在您回去阅读

教科书并阅读新闻组的常见问题解答后,请使用该新闻组。您使用C ++语言的语言很可能是误用的,其新闻组是< news:comp.lang.c ++> ;.


在上面的

In the foolish language you are grossly misusing, you will need to
delete the array separately. However, since you have assigned the
addess of the anonymous char literal "hello" to ptr, that is almost
guaranteed to blow up in your face. After you go back and read your
textbook and read the FAQ for the newsgroup for the language you are
using, ask in that newsgroup. It is very likely that the language you
are misusing in C++, and its newsgroup is <news:comp.lang.c++>.

In the


代码中,我相信在返回语句

之后将不会执行delete []行。


我应该如何实现这一目标?
code above, I believe the delete[] line will not be executed following
the return statement.

How should I achieve this?



通过不随机编码并祈祷你输入的内容是合法的。把

用于实际学习语言。

By not coding randomly and praying that whatever you type is legal. Put
some effort into actually learning the language.


这篇关于另一个动态记忆疑问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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