473,387 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

Oct 30 '05 #1
10 1787
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
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

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

<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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

45
by: Jamie Burns | last post by:
Hello, I realise that I just dont get this, but I cannot see the need for auto_ptr. As far as I have read, it means that if you create an object using an auto_ptr, instead of a raw pointer, then...
4
by: Wei-Chao Hsu | last post by:
The program always crashs where the memory is deallocated. Any one could help me? Thanks! #include <iostream> void main() { //Allocate int **a=new int* ; for(int i=0;i<10;++i)
10
by: gogogo_1001 | last post by:
Dear all, I don't understand why "delete" works well on destructing a object, but fails to destruct a vector of it. Any of your comment is highly appreciated! Following is the program...
0
by: acharyaks | last post by:
Hi life saver, I am using excel component for the development. The purpose is to connect to excel through the odbc connection string. Then through the connection extract data into a dataset and...
0
by: Carl Gilbert | last post by:
Hi I am trying to get a simple ASP.NET web application to work on my 1&1 (1and1) web space. I managed to get something working a while ago, with thanks to Karl Seguin, but for some unknown...
20
by: mariano.difelice | last post by:
Hi, I've a big memory problem with my application. First, an example: If I write: a = range(500*1024) I see that python process allocate approximately 80Mb of memory.
19
by: lawrence k | last post by:
How can I find out where my script is outputting to the screen for the first time? My error logs are full of stuff like this: PHP Warning: session_start(): Cannot send session cache...
2
by: karinmorena | last post by:
I'm having 4 errors, I'm very new at this and I would appreciate your input. The error I get is: Week5MortgageGUI.java:151:cannot find symbol symbol: method allInterest(double,double,double)...
5
by: Just_a_fan | last post by:
I tried to put an "on error" statement in a routine and got the message that I cannot user "on error" and a lamda or query expression in the same routine. Help does not list anything useful for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.