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

Need helpin figuing out Segmentation fault when calling size() of STL vector

Hi,

I have a segmentation fault in line 66 of GroupResult.h and I can't
figure out why that causes any problem, I appreciate if anyone can
help.

line 66 of Result.h:

66 size_t size() const { return _bdl.size(); }

where _bdl is a private attribute of

class Result
{
private:
int _type;
vector<BlockData*> _bdl;

};

I step thru the debugger, _bdl is not null. So I can't understand why
calling '_bdl.size()' returns a Segmentation fault.

Any help is appreciated.
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1208583648 (LWP 9292)]
0x03edeffb in std::vector<BlockData*, std::allocator<BlockData*>
::begin (this=0x9) at /usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/stl_vector.h:343 /usr/include/c++/4.0.2/bits/stl_vector.h:343:12255:beg:0x3edeffb
(gdb) bt
#0 0x03edeffb in std::vector<BlockData*, std::allocator<BlockData*>::begin (this=0x9) at /usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/stl_vector.h:343 #1 0x03edf08d in std::vector<BlockData*, std::allocator<BlockData*>::size (this=0x9) at /usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/stl_vector.h:403

#2 0x03edf0ca in GroupResult::size (this=0x1) at Result.h:66
#3 0x03f11e98 in PageBreaker::handleOverLap (this=0xbf97a63c) at
PageBreaker.cpp:274

Mar 19 '06 #1
5 6720
In article <11**********************@i39g2000cwa.googlegroups .com>,
"si***************@gmail.com" <si***************@gmail.com> wrote:
Hi,

I have a segmentation fault in line 66 of GroupResult.h and I can't
figure out why that causes any problem, I appreciate if anyone can
help.

line 66 of Result.h:

66 size_t size() const { return _bdl.size(); }

where _bdl is a private attribute of

class Result
{
private:
int _type;
vector<BlockData*> _bdl;

};

I step thru the debugger, _bdl is not null. So I can't understand why
calling '_bdl.size()' returns a Segmentation fault.

Any help is appreciated.


Somebody, somewhere has stepped on your Result prior to you printing out
size().

I like to give my class an invariants() member:

class Result
{
public:
bool invariants() const;
private:
int _type;
vector<BlockData*> _bdl;

};

In invariants() I check all of the things must always be true for the
type. If they are all true I return true, else I return false. You can
then sprinkle your code with exceptions or asserts until you find out
where your invariants are being violated.

Example:
void foo()
{
int stuff[4];
Result r;
assert(r.invariants());
// do things with r
// ...
assert(r.invariants());
// do things with stuff
for (int i = 0; i <= 4; ++i)
stuff[i] = i;
assert(r.invariants());
return r;
}

-Howard
Mar 19 '06 #2
si***************@gmail.com wrote:
where _bdl is a private attribute of

class Result
{
private:
int _type;
vector<BlockData*> _bdl;

};


I recommend against using variable names that begin with an underscore,
because it makes your code none-portable.
IAW C++ standard, variable names that begin with an underscore are
reserved for the implementation.
If you need to use an underscore, use trailing underscores instead.
vector<BlockData*> _bdl; //This is NOT portable
vector<BlockData*> bdl_; //This is portable

I also recommend you use smart pointers instead of raw pointers in your
vector.
Consider using boost::shared_ptr, or the following more efficient smart
pointer:
http://axter.com/smartptr

By using the above smart pointer, you automate memory management, and
you get automatic deep copy cloning of your pointee.

Your current segment fault error may be related to your code's attempt
to do manual memory management.
It might be performming early deletion, or duplicate deletions. If you
use a smart pointer, you can avoid such logic errors.

Mar 20 '06 #3
si***************@gmail.com wrote:
Hi,

I have a segmentation fault in line 66 of GroupResult.h and I can't
figure out why that causes any problem, I appreciate if anyone can
help.

line 66 of Result.h:

66 size_t size() const { return _bdl.size(); }

where _bdl is a private attribute of

class Result
{
private:
int _type;
vector<BlockData*> _bdl;

};

I step thru the debugger, _bdl is not null.
It can't be. It's not a pointer.
So I can't understand why calling '_bdl.size()' returns a Segmentation
fault.

Any help is appreciated.
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1208583648 (LWP 9292)]
0x03edeffb in std::vector<BlockData*, std::allocator<BlockData*>
::begin (this=0x9) at
::/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/stl_vector.h:343 /usr/include/c++/4.0.2/bits/stl_vector.h:343:12255:beg:0x3edeffb
(gdb) bt
#0 0x03edeffb in std::vector<BlockData*, std::allocator<BlockData*>
::begin (this=0x9) at
::/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/stl_vector.h:343

#1 0x03edf08d in std::vector<BlockData*, std::allocator<BlockData*>
::size (this=0x9) at
::/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/stl_vector.h:403

#2 0x03edf0ca in GroupResult::size (this=0x1) at Result.h:66

^^^^^^^^
This looks fishy. Also the "this=0x9" parts before. It seems those objects
are all part of an object you are trying to access through a null pointer.

#3 0x03f11e98 in PageBreaker::handleOverLap (this=0xbf97a63c) at
PageBreaker.cpp:274


Mar 20 '06 #4

"Axter" <go****@axter.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...
si***************@gmail.com wrote:
where _bdl is a private attribute of

class Result
{
private:
int _type;
vector<BlockData*> _bdl;

};


I recommend against using variable names that begin with an underscore,
because it makes your code none-portable.
IAW C++ standard, variable names that begin with an underscore are
reserved for the implementation.
If you need to use an underscore, use trailing underscores instead.
vector<BlockData*> _bdl; //This is NOT portable
vector<BlockData*> bdl_; //This is portable


Not so. If the leading undescore is followed by a capital letter or another
underscore, then you're correct... it's "reserved for the implementation",
meaning you shouldn't use it yourself. Otherwise, it's legal according to
the standard to precede the variable name with a single underscore.

-Howard

Mar 20 '06 #5

<si***************@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
Hi,

I have a segmentation fault in line 66 of GroupResult.h and I can't
figure out why that causes any problem, I appreciate if anyone can
help.

line 66 of Result.h:

66 size_t size() const { return _bdl.size(); }

where _bdl is a private attribute of

class Result
{
private:
int _type;
vector<BlockData*> _bdl;

};

I step thru the debugger, _bdl is not null.
It can't be NULL, since it's not a pointer.
So I can't understand why
calling '_bdl.size()' returns a Segmentation fault.

Any help is appreciated.
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1208583648 (LWP 9292)]
0x03edeffb in std::vector<BlockData*, std::allocator<BlockData*>
::begin (this=0x9) at
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/stl_vector.h:343

/usr/include/c++/4.0.2/bits/stl_vector.h:343:12255:beg:0x3edeffb
(gdb) bt
#0 0x03edeffb in std::vector<BlockData*, std::allocator<BlockData*>
::begin (this=0x9) at
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/stl_vector.h:343

#1 0x03edf08d in std::vector<BlockData*, std::allocator<BlockData*>
::size (this=0x9) at
/usr/lib/gcc/i386-redhat-linux/4.0.2/../../../../include/c++/4.0.2/bits/stl_vector.h:403

#2 0x03edf0ca in GroupResult::size (this=0x1) at Result.h:66
#3 0x03f11e98 in PageBreaker::handleOverLap (this=0xbf97a63c) at
PageBreaker.cpp:274


Looks to me like the Result object itself is not valid. You also seem to be
mixing up Result and GroupResult here somewhere. Without more code, it's
impossible to tell what's actually going on, but the values shown above for
"this" in both Result and GroupResult definitely look wrong. Check the
actual instance(s) of the variable(s) containing _bdl, and I suspect you'll
find the problem.

-Howard

Mar 20 '06 #6

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

Similar topics

3
by: VB | last post by:
Hi, here File.cpp and File.h: File.cpp: ---------------------- #pragma warning (disable: 4786)
3
by: mblome | last post by:
Hi everybody! I came across a very strange problem with stl vectors during developement of a mesh creation program. As the program is quite large I can only post small parts of it. Basically I...
1
by: sandwich_eater | last post by:
I get a segmentation fault in my program when calling a function "TestFn" that has been passed as a pointer into another function. The following excerpt should give enough information as to what I...
3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
7
by: utab | last post by:
Dear all, I tried sth like this, compiles but segmentation fault error. In my reasoning field_values holds a vector<double> but when I tried, I understood that it is not the case :-). ...
4
by: Greg | last post by:
The code where the error occurs is below in svdfit. This is a difficult one becuse changing the reporting (cout) can move the error. dblvec and dblmat are std::vectors of doubles. I am using the...
6
by: Wes | last post by:
I'm running FreeBSD 6.1 RELEASE #2. The program is writting in C++. The idea of the program is to open one file as input, read bytes from it, do some bitwise operations on the bytes, and then...
1
by: jwlkr | last post by:
Hi, I am trying to sort a vector of a user defined type: a class which represents points in cartesian coordinates. The vector of points needs to be sorted according to the value of the...
8
by: Bryan | last post by:
Hello all. I'm fairly new to c++. I've written several programs using std::vectors, and they've always worked just fine. Until today. The following is a snippet of my code (sorry, can't...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.