473,395 Members | 1,471 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,395 software developers and data experts.

Seg fault when delete a pointer to vector<int>

I have a class 'Statistics' which has a private attribute
' vector<int>* _x;'

And in the destructor of the Statistics, I have this code to free the
memory:

Statistics::~Statistics()
{

if (_x) {
delete _x; // Seg fault here...
}
}

But I get a Seg. Fault when I execute the program.

Can you please tell me what did I do wrong?

Thank you.
(gdb) bt
#0 0x00248eaa in __gnu_cxx::__pool<true>::_M_reclaim_block () from
/usr/lib/libstdc++.so.6
#1 0x0250da84 in __gnu_cxx::__mt_alloc<int,
__gnu_cxx::__common_pool_policy<__gnu_cxx::__pool, true> >::deallocate
(this=0x9107474, __p=0xdadadada, __n=152471256) at mt_allocator.h:746
#2 0x0250daac in std::_Vector_base<int, std::allocator<int>
::_M_deallocate (this=0x9107474, __p=0x91686d8, __n=0) at

stl_vector.h:123
#3 0x0250dacd in ~_Vector_base (this=0x91686d8) at stl_vector.h:109
#4 0x0250db0b in ~vector (this=0x9107474) at stl_vector.h:273
#5 0x0250caf2 in ~Statistics (this=0xbfd85ad8) at
Statistics.cpp:54

Jan 30 '06 #1
10 2876
Piotr wrote:
I have a class 'Statistics' which has a private attribute
' vector<int>* _x;'

And in the destructor of the Statistics, I have this code to free the
memory:

Statistics::~Statistics()
{

if (_x) {
delete _x; // Seg fault here...
}
}

But I get a Seg. Fault when I execute the program.

Can you please tell me what did I do wrong?
Looks like _x isn't valid at the point where you try to delete it.
Would need to see the rest of the code dealing with _x to know how that
happened. Are you setting it? Attempting conversions? Deleting it
twice?
(gdb) bt
#0 0x00248eaa in __gnu_cxx::__pool<true>::_M_reclaim_block () from
/usr/lib/libstdc++.so.6
#1 0x0250da84 in __gnu_cxx::__mt_alloc<int,
__gnu_cxx::__common_pool_policy<__gnu_cxx::__pool, true> >::deallocate
(this=0x9107474, __p=0xdadadada, __n=152471256) at mt_allocator.h:746


Note the suspicious hex address. __p is likely to be the value of _x,
by my guess. Use your debugger to trace what's going on.

Luke

Jan 30 '06 #2
Thank you. Can you please explain what you meant by 'Attempting
conversions?'

And I have set it somewhere, otherwise, the code won't execute, right?

Statistics::~Statistics()
{

if (_x) {
delete _x; // Seg fault here...
}

}

Jan 30 '06 #3
Can you please tell me what is the right way to write the destructor if
i have a private attribute
of ' vector<int>* _x;' in my class?

class Statistic {
private:
vector<int>* _x;
};

Jan 30 '06 #4
Piotr wrote:
Thank you. Can you please explain what you meant by 'Attempting
conversions?'
Mucking about with typecasts, conversion operators, single-argument
constructors, operator=, etc. Basically just stuff that would make it
easier to mess up some fancy pointer hocus-pocus.
And I have set it somewhere, otherwise, the code won't execute, right?
Uh, no. If you never initialize _x, your program will (hopefully) seg
fault because you are attempting to delete memory pointed to by an
uninitialized address.
Statistics::~Statistics()
{

if (_x) {
delete _x; // Seg fault here...
}

}


What do you mean "if(_x)"? Don't you know whether it's null? Is it
possible that _x became null at some point? Is it really necessary for
there to be two destruction paths for _x and its referent?

If you want more help, you should post, I say again, the rest of the
code dealing with _x.

Luke

Luke

Jan 30 '06 #5
Piotr wrote:
Thank you. Can you please explain what you meant by 'Attempting
conversions?'
Argh... i wrote up a whole reply and lost it to the bit bucket... sigh.
Anyway, conversions. I mean mucking about with explicit casts, or any
of the various forms of implicit conversions.
And I have set it somewhere, otherwise, the code won't execute, right?
Uh, no. If you haven't initialized _x, its contents are undefined,
which means it points to a random location of memory. What do you
suppose happens when you try to delete a pointer like that?
Statistics::~Statistics()
{

if (_x) {
delete _x; // Seg fault here...
}

}


Yup.

What's with the "if(_x)", by the way? Can x_ become null somehow? If
so, I'd reconsider your design not to have two destruction paths for
x_.

Luke

Jan 30 '06 #6
Damn, I guess I'm just having a Bad Usenet Day.

Jan 30 '06 #7
Piotr wrote:
Can you please tell me what is the right way to write the destructor if
i have a private attribute
of ' vector<int>* _x;' in my class?

class Statistic {
private:
vector<int>* _x;
};


You are asking for "the right way"? There is no such thing. It depends. The
most important question here is:

?? Can a pointee be shared among different objects of this class ??

a) If so, you will either need to be *very* careful or use a smart pointer
instead of a raw pointer. The reason is that you will want to delete
the pointer only if there are no other references to the same vector<int>
object left (rule: the last pointer pointing there has to kill it).

b) If not, you could and should just have a vector<int> member. In
this case, the default destructor of Statistic would take care of
the proper destruction of the member.

Note: Even if you did not intend the pointee to be shared, you may have
implemented the copy constructor or assignment operator in a way that
shares the pointee. The default copy constructor and assignment operator,
for instance, will happily copy the pointer so that afterwards you have two
Statistic objects whose _x members point to the same vector<int>.

Rule of thumb: avoid pointer members. They just cause a headache. If you
need them some reason, consider using something like tr1::shared_ptr
(formerly known as boost::shared_ptr).
Best

Kai-Uwe Bux
Jan 30 '06 #8
Piotr wrote:
I have a class 'Statistics' which has a private attribute
' vector<int>* _x;'

And in the destructor of the Statistics, I have this code to free the
memory:

Statistics::~Statistics()
{

if (_x) {
delete _x; // Seg fault here...
}
}

But I get a Seg. Fault when I execute the program.

Can you please tell me what did I do wrong?


First of all, the 'if' part is unnecessary -- delete
works fine for NULL pointers.

Second of all -- I'd bet that either
a) you don't allocate _x with new,
b) you had already deleted _x earlier,
c) you have corrupted _x.

Try inserting some 'cerr << "I am here" << endl;'
constructs into sensitive places, like the destructor
or the place where you actually new your _x.

HTH,
- J.
Jan 30 '06 #9
On 29 Jan 2006 22:08:13 -0800 in comp.lang.c++, "Piotr"
<ra************@gmail.com> wrote,
Can you please tell me what is the right way to write the destructor if
i have a private attribute
of ' vector<int>* _x;' in my class?


The right way? The right way is to change it to
vector<int> x;

Then let the vector destructor handle it.

Jan 30 '06 #10
In article <11**********************@o13g2000cwo.googlegroups .com>,
"Piotr" <ra************@gmail.com> wrote:
I have a class 'Statistics' which has a private attribute
' vector<int>* _x;'

And in the destructor of the Statistics, I have this code to free the
memory:

Statistics::~Statistics()
{

if (_x) {
delete _x; // Seg fault here...
}
}

But I get a Seg. Fault when I execute the program.

Can you please tell me what did I do wrong?

Thank you.
(gdb) bt
#0 0x00248eaa in __gnu_cxx::__pool<true>::_M_reclaim_block () from
/usr/lib/libstdc++.so.6
#1 0x0250da84 in __gnu_cxx::__mt_alloc<int,
__gnu_cxx::__common_pool_policy<__gnu_cxx::__pool, true> >::deallocate
(this=0x9107474, __p=0xdadadada, __n=152471256) at mt_allocator.h:746
#2 0x0250daac in std::_Vector_base<int, std::allocator<int>
::_M_deallocate (this=0x9107474, __p=0x91686d8, __n=0) at

stl_vector.h:123
#3 0x0250dacd in ~_Vector_base (this=0x91686d8) at stl_vector.h:109
#4 0x0250db0b in ~vector (this=0x9107474) at stl_vector.h:273
#5 0x0250caf2 in ~Statistics (this=0xbfd85ad8) at
Statistics.cpp:54


My guess is that at some point in your code, you assign one Statistics
object to another. Here are a few things to try:

First:

Make sure _x is default initialized properly.

Statistics::Statistics():_x(0) { } or
Statistics::Statistics():_x(new vector<int>) { }

Second:

Make sure you aren't passing the pointer to other objects accidentally:

class Statistics {
// don't implement the two functions below
Statistics( const Statistics& );
Statistics& operator=( const Statistics& );
// other privates
public:
// public stuff
};

I have a feeling that when you put the two function definitions above
in, your code won't compile anymore. That tells you that you had
multiple Statistics objects sharing the same vector<int> object, the
first one to go out of scope deleted the vector<int> but the second one
doesn't know that so it tries to delete it again.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Jan 30 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andreas Krueger | last post by:
Hi! I am fed up with vector<int> iv; iv.push_back(42); iv.push_back(9); iv.push_back(11); ... and would rather use a function "fillVector": vector<int> iv = fillVector(42,9,11); like...
3
by: Erik Borgstr?m | last post by:
Hi, Yes, I have followed some of the discussion on vector<vector<int> >, but perhaps I'm just blind. I want to use a vector<vector<int> > of outer size n and inner size m. Are these correct ways...
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
5
by: pmatos | last post by:
Hi all, I have a vector of vector of ints, I could use C approach by using int but I think C++ vector<vector<int> > would be easier to manage. So I have a function which creates and initializes...
4
by: arnuld | last post by:
i wrote a programme to create a vector of 5 elements (0 to 4), here is the code & output: #include <iostream> #include <vector> int main() { std::vector<intivec; // dynamically create a...
2
by: danielhdez14142 | last post by:
Some time ago, I had a segment of code like vector<vector<int example; f(example); and inside f, I defined vector<int>'s and used push_back to get them inside example. I got a segmentation...
0
by: citystud | last post by:
does C# support vector< vector<int> > ?if not how can i use vector?
10
by: arnuld | last post by:
It is quite an ugly hack but it is all I am able to come up with for now :-( and it does the requires work. I want to improve the program, I know you people have much better ideas ;-) /* C++...
10
by: arnuld | last post by:
WANTED: /* C++ Primer - 4/e * * Exercise: 9.26 * STATEMENT * Using the following definition of ia, copy ia into a vector and into a list. Use the single iterator form of erase to...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.