473,748 Members | 7,571 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::~St atistics()
{

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::__po ol<true>::_M_re claim_block () from
/usr/lib/libstdc++.so.6
#1 0x0250da84 in __gnu_cxx::__mt _alloc<int,
__gnu_cxx::__co mmon_pool_polic y<__gnu_cxx::__ pool, true> >::deallocate
(this=0x9107474 , __p=0xdadadada, __n=152471256) at mt_allocator.h: 746
#2 0x0250daac in std::_Vector_ba se<int, std::allocator< int>
::_M_deallocat e (this=0x9107474 , __p=0x91686d8, __n=0) at

stl_vector.h:12 3
#3 0x0250dacd in ~_Vector_base (this=0x91686d8 ) at stl_vector.h:10 9
#4 0x0250db0b in ~vector (this=0x9107474 ) at stl_vector.h:27 3
#5 0x0250caf2 in ~Statistics (this=0xbfd85ad 8) at
Statistics.cpp: 54

Jan 30 '06 #1
10 2902
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::~St atistics()
{

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::__po ol<true>::_M_re claim_block () from
/usr/lib/libstdc++.so.6
#1 0x0250da84 in __gnu_cxx::__mt _alloc<int,
__gnu_cxx::__co mmon_pool_polic y<__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::~St atistics()
{

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::~St atistics()
{

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::~St atistics()
{

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_p tr).
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::~St atistics()
{

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

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

Similar topics

3
5813
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 in Mathematica(tm): someList={42,9,11};
3
2806
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 to achieve this? 1. vector<vector<int> > v(n); for(int i=0;i<n;i++){ v = vector<int>(m);
20
17831
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; std::vector<double>::size_type size = src.size(); dest.reserve(size); for (std::vector<int>::size_type i = 0;
5
10576
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 the vector with the values I need (I know these values before hand). - What's the best way to initialize the vector<vector<int> >? Can I initilize it by enumerating its values? - If I do: v = new vector<vector<int> >(3) for example, is it
4
5705
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 vector
2
3321
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 fault which I resolved by doing vector<vector<int example; example.push_back(vector<int>());
0
1138
by: citystud | last post by:
does C# support vector< vector<int> > ?if not how can i use vector?
10
5020
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++ Primer - 4/e * * exercise 9.20 * STATEMENT: * Write a program to to compare whether a vector<intcontains the
10
6073
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 remove the elements with odd values from your list * and the even values from your vector.
0
8830
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9544
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8243
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.