我无法释放空间。 [英] I cannot deallocate space.

查看:82
本文介绍了我无法释放空间。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有一个问题,我实际上无法理解。

我已经定义了一个类,让我们调用它,X和我有一个变量X * tmp,

我用new分配空间。然后我使用

stack< X *> mystack;





mystack.push(tmp);


然后,我这么做是
tmp = mystack.top();

if(tmp!= NULL)

删除tmp;

mystack.pop();


我得到分段错误。


即使我这样做


if(mystack.top()!= NULL)

删除mystack.top();

mystack.pop();


i得到相同的消息。


最后如何解除tmp的空间?

谢谢。 ..

Hello, i have a problem, which i cannot actually understand.
I have defined a class, let''s call it, X and i have a variable X* tmp,
whom i allocate space with new.Then i use
stack<X* > mystack;

and do

mystack.push(tmp);

And then, i do

tmp = mystack.top();
if (tmp != NULL)
delete tmp;
mystack.pop();

and i get segmentation fault.

Even if i do

if (mystack.top() != NULL)
delete mystack.top();
mystack.pop();

i get the same message.

How can, finally, deallocate the tmp''s space ?
Thanks...

推荐答案

ba *** **@gmail.com 写道:
你好,我有一个问题,我实际上无法理解。
我已经定义了一个类,让我们称之为, X和我有一个变量X * tmp,
我用new分配空间。然后我使用
堆栈< X *> mystack;


mystack.push(tmp);


什么是tmp?你确定,你没有两次推同一个指针吗?

然后,我做了t / = tt = mystack.top();
if(tmp! = NULL)
删除tmp;
mystack.pop();

我得到分段错误。

即使我这样做

if(mystack.top()!= NULL)
删除mystack.top();
mystack.pop();

我收到相同的消息。
Hello, i have a problem, which i cannot actually understand.
I have defined a class, let''s call it, X and i have a variable X* tmp,
whom i allocate space with new.Then i use
stack<X* > mystack;
and do

mystack.push(tmp);
What''s tmp? Are you sure, you are not pushing the same pointer twice?

And then, i do

tmp = mystack.top();
if (tmp != NULL)
delete tmp;
mystack.pop();

and i get segmentation fault.

Even if i do

if (mystack.top() != NULL)
delete mystack.top();
mystack.pop();

i get the same message.




您可以发布编译并生成分段错误的代码吗?


-

Valentin Samko - http://www.valentinsamko.com


ba*****@gmail.com sade:
你好,我有一个问题,我实际上无法理解。
我已经定义了一个类,让我们称它为X,我有一个变量X * tmp,
谁我用new分配空间。然后我使用
stack< X *> mystack;


尽管你说的是,所提供的代码缺少课程和

的实际分配。


如果你忽略了潜在的例外情况,那就完美无缺:


#include< stack>

class X {};

int main (){

std :: stack< X *> mystack;

X * tmp =新X;

mystack.push(tmp);

tmp = mystack.top();

mystack.pop();

删除tmp;

返回0;

}

并且做了

mystack.push(tmp);


你在上述声明之前或之后分配了吗?

然后,我这样做了/ tt = mystack.top();
if(tmp!= NULL)
删除tmp;
mystack.pop();

我得到分段错误。
Hello, i have a problem, which i cannot actually understand.
I have defined a class, let''s call it, X and i have a variable X* tmp,
whom i allocate space with new.Then i use
stack<X* > mystack;

Despite what you say, the presented code lacks the class and
the actual allocation.

This works flawless if you ignore potential exceptions:

#include <stack>
class X {};
int main() {
std::stack<X*> mystack;
X * tmp = new X;
mystack.push(tmp);
tmp = mystack.top();
mystack.pop();
delete tmp;
return 0;
}
and do

mystack.push(tmp);

Do you allocate before or after the above statement?
And then, i do

tmp = mystack.top();
if (tmp != NULL)
delete tmp;
mystack.pop();

and i get segmentation fault.




很可能是因为你已经推送了除了分配指针之外的其他东西,或已经删除它,或者没有按任何东西推动

。并注意你可以这样做:


X * tmp = 0;

删除tmp;


复制并粘贴你想在这里运行的代码。


TIT



Most likely because you have pushed something other than the
allocated pointer, or already deleted it, or haven''t pushed
anything at all. And notice that you can do this:

X * tmp = 0;
delete tmp;

Copy and paste the code that you''re trying to run here.

TIT




2005年10月30日03:29:22 -0800
ba*****@gmail.com 写道:

On 30 Oct 2005 03:29:22 -0800
ba*****@gmail.com wrote:
你好,我有一个问题,我实际上无法理解。
我已经定义了一个类,让我们称它为X,我有一个变量X * tmp,
我用new分配空间。然后我使用
stack< X *> mystack;



mystack.push(tmp);

tmp由新运营商分配?

然后,我做了t / = tt = mystack.top();
if(tmp!= NULL)
删除tmp;


top()不等于pop()。使用top()时,顶部元素不会从堆栈中删除。但即便如此,在我的测试中也没有错误。

mystack.pop();

在这句话之后,请执行删除tmp。

我得到分段错误。

即使我这样做(/stack.top()!= NULL)
删除mystack.top() ;
mystack.pop();

我收到同样的消息。

最终如何释放tmp的空间?
谢谢......
Hello, i have a problem, which i cannot actually understand.
I have defined a class, let''s call it, X and i have a variable X* tmp,
whom i allocate space with new.Then i use
stack<X* > mystack;

and do

mystack.push(tmp);
tmp is allocated by new operator?
And then, i do

tmp = mystack.top();
if (tmp != NULL)
delete tmp;
top() is not equal to pop(). When using top(), the top element is not deleted from the stack. But even so, in my test there is no error.
mystack.pop();
After this sentence, do "delete tmp".
and i get segmentation fault.

Even if i do

if (mystack.top() != NULL)
delete mystack.top();
mystack.pop();

i get the same message.

How can, finally, deallocate the tmp''s space ?
Thanks...



这篇关于我无法释放空间。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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