 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
babis85@gmail.com Guest
|
Posted: Sun Oct 30, 2005 11:29 am Post subject: I cannot deallocate space. |
|
|
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...
|
|
| Back to top |
|
 |
TIT Guest
|
Posted: Sun Oct 30, 2005 11:54 am Post subject: Re: I cannot deallocate space. |
|
|
[email]babis85 (AT) gmail (DOT) com[/email] sade:
| Quote: | 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;
}
| Quote: | and do
mystack.push(tmp);
|
Do you allocate before or after the above statement?
| Quote: | 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
|
|
| Back to top |
|
 |
Valentin Samko Guest
|
Posted: Sun Oct 30, 2005 11:55 am Post subject: Re: I cannot deallocate space. |
|
|
[email]babis85 (AT) gmail (DOT) com[/email] wrote:
| Quote: | 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?
| Quote: |
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
|
|
| Back to top |
|
 |
gary Guest
|
Posted: Sun Oct 30, 2005 11:57 am Post subject: Re: I cannot deallocate space. |
|
|
On 30 Oct 2005 03:29:22 -0800
[email]babis85 (AT) gmail (DOT) com[/email] wrote:
| Quote: | 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? |
| Quote: | 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.
| Quote: | mystack.pop();
After this sentence, do "delete tmp". |
| Quote: | 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...
|
|
|
| Back to top |
|
 |
Jim Langston Guest
|
Posted: Mon Oct 31, 2005 12:48 am Post subject: Re: I cannot deallocate space. |
|
|
<babis85 (AT) gmail (DOT) com> wrote
| Quote: | 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;
}
| Quote: |
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...
|
|
|
| Back to top |
|
 |
Old Wolf Guest
|
Posted: Mon Oct 31, 2005 3:30 am Post subject: Re: I cannot deallocate space. |
|
|
[email]babis85 (AT) gmail (DOT) com[/email] wrote:
| Quote: | 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.
|
|
| Back to top |
|
 |
TIT Guest
|
Posted: Mon Oct 31, 2005 5:25 am Post subject: Re: I cannot deallocate space. |
|
|
Old Wolf sade:
| Quote: | babis85 (AT) gmail (DOT) 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
|
|
| Back to top |
|
 |
TIT Guest
|
Posted: Mon Oct 31, 2005 5:30 am Post subject: Re: I cannot deallocate space. |
|
|
Jim Langston sade:
| Quote: | babis85 (AT) gmail (DOT) com> wrote in message
news:1130671762.158221.188790 (AT) o13g2000cwo (DOT) googlegroups.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
|
|
| Back to top |
|
 |
TIT Guest
|
Posted: Mon Oct 31, 2005 5:56 am Post subject: Re: I cannot deallocate space. |
|
|
TIT sade:
| Quote: | Jim Langston sade:
[email]babis85 (AT) gmail (DOT) com[/email]> wrote in message
news:1130671762.158221.188790 (AT) o13g2000cwo (DOT) googlegroups.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
|
|
| Back to top |
|
 |
savagesmc Guest
|
Posted: Mon Oct 31, 2005 6:00 pm Post subject: Re: I cannot deallocate space. |
|
|
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
|
|
| Back to top |
|
 |
Old Wolf Guest
|
Posted: Mon Oct 31, 2005 8:12 pm Post subject: Re: I cannot deallocate space. |
|
|
TIT wrote:
| Quote: | Old Wolf sade:
[email]babis85 (AT) gmail (DOT) com[/email] 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;
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|