By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,515 Members | 1,848 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,515 IT Pros & Developers. It's quick & easy.

I cannot deallocate space.

P: n/a
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...

Oct 30 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
ba*****@gmail.com wrote:
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.


Can you post the code which compiles and generates a segmentation fault?

--

Valentin Samko - http://www.valentinsamko.com
Oct 30 '05 #2

P: n/a
TIT
ba*****@gmail.com sade:
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.


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
Oct 30 '05 #3

P: n/a

On 30 Oct 2005 03:29:22 -0800
ba*****@gmail.com wrote:
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...

Oct 30 '05 #4

P: n/a

<ba*****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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();
Here you are popping something you already deleted. Pop it first then
delete.

tmp = mystack.top();
if (tmp != NULL)
{
mystack.pop();
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...

Oct 31 '05 #5

P: n/a
ba*****@gmail.com wrote:
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.


You have to do the pop() before you do the delete, eg:

tmp = mystack.pop();
delete tmp;

If you still get errors after making this change, then you
have an error elsewhere in your code. See if you can post
a compilable program that demonstrates the error.

Oct 31 '05 #6

P: n/a
TIT
Old Wolf sade:
ba*****@gmail.com wrote:
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.

You have to do the pop() before you do the delete, eg:

tmp = mystack.pop();
delete tmp;


Assuming he's using std::stack<> then that won't even compile
since std::stack<>::pop() returns void.

TIT
Oct 31 '05 #7

P: n/a
TIT
Jim Langston sade:
<ba*****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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();

Here you are popping something you already deleted. Pop it first then
delete.

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


It doesn't matter if he pops before or after he deletes the top.

TIT
Oct 31 '05 #8

P: n/a
TIT
TIT sade:
Jim Langston sade:
<ba*****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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();


Here you are popping something you already deleted. Pop it first then
delete.

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


It doesn't matter if he pops before or after he deletes the top.


Sorry, that should read:
It doesn't matter if he pops before of after he deletes the already
aquired top value.

TIT
Oct 31 '05 #9

P: n/a
You can't do the NULL check on mystack.top(). What you want to do is
check to see if mystack is empty or not before calling top to get a
pointer to delete. Something like:

while (!mystack.empty())
{
delete mystack.top();
mystack.pop();
}

This will work assuming that you actually inserted valid pointers into
the container.
Hope that helps.

Steve

Oct 31 '05 #10

P: n/a
TIT wrote:
Old Wolf sade:
ba*****@gmail.com wrote:

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

and i get segmentation fault.


You have to do the pop() before you do the delete, eg:

tmp = mystack.pop();
delete tmp;


Assuming he's using std::stack<> then that won't even compile
since std::stack<>::pop() returns void.


Let me try again:

tmp = mystack.top();
mystack.pop();
delete tmp;

Oct 31 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.