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... 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 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
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...
<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... 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.
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
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
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
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
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; This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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)
|
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...
|
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...
|
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...
|
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.
|
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...
|
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)...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |