473,398 Members | 2,212 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,398 software developers and data experts.

Debugging a Segmentation Fault in C++ STL code

Hi,

I have a C++ problem which uses STL containers and algorithm and it has
a segmentation fault under some condition.

Here is a back trace from gdb. Is it possible to tell what is wrong
from the trace? I know which method is crashing (MyHandler.cpp:133),
but I don't know why.

Thanks for any help.

#0 0x008173a3 in memmove () from /lib/libc.so.6
#1 0x05d6eace in std::__copy<true,
std::random_access_iterator_tag>::copy<A*> (__first=0xf762b070,
__last=0x10, __result=0x0) at stl_algobase.h:300
#2 0x05d6eafb in std::__copy_aux<A**, A**> (__first=0x89d4f90,
__last=0x89d4fa0, __result=0x0) at stl_algobase.h:317
#3 0x05d6eb43 in std::__copy_normal<true,
true>::copy_n<__gnu_cxx::__normal_iterator<A**, std::vector<A*,
std::allocator<A*> > >, __gnu_cxx::__normal_iterator<A**,
std::vector<A*, std::allocator<A*> > > > (__first={_M_current =
0x89d4f90}, __last={_M_current = 0x89d4fa0}, __result={_M_current =
0x0}) at stl_algobase.h:354
#4 0x05d6eb89 in std::copy<__gnu_cxx::__normal_iterator<A**,
std::vector<A*, std::allocator<A*> > >,
__gnu_cxx::__normal_iterator<A**, std::vector<A*, std::allocator<A*> >
(__first={_M_current = 0x89d4f90}, __last={_M_current = 0x89d4fa0}, __result={_M_current = 0x0}) at stl_algobase.h:387

#5 0x05d6fa9d in do_divide::operator() (this=0xbf91985c,
element=0x8ccea30) at MyHandler.cpp:133

Jan 22 '06 #1
7 2997

<si***************@gmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
Hi,

I have a C++ problem which uses STL containers and algorithm and it has
a segmentation fault under some condition.

Here is a back trace from gdb. Is it possible to tell what is wrong
from the trace? I know which method is crashing (MyHandler.cpp:133),
but I don't know why.
Without seeing your code, we can only guess. Try to assemble
a small, compilable example that still produces the undesired
behavior and post it here.

Having said that, I'll go ahead and take a guess:
#0 0x008173a3 in memmove () from /lib/libc.so.6
#1 0x05d6eace in std::__copy<true,
std::random_access_iterator_tag>::copy<A*> (__first=0xf762b070,
__last=0x10, __result=0x0) at stl_algobase.h:300
#2 0x05d6eafb in std::__copy_aux<A**, A**> (__first=0x89d4f90,
__last=0x89d4fa0, __result=0x0) at stl_algobase.h:317


The diagnostic text you've posted refers to 'std::copy()'.
Note that this function copies data from one storage location to
another, *but does not allocate any storage, you must do this
yourself*. E.g.:

std::vector<int> v1(10); // vector of ten zero-initialized integers
std::vector<int> v2; // *empty* vector of integers

std::copy(v1.begin(), v1.end(), v2.begin()); // ERROR, no place to write

If this is indeed your problem, look up 'insert_iterator',
'back_insert_iterator', 'back_inserter', etc. These constructs
will create storage for 'copy' to write to. Alternatively, look
at vector::resize(), which will add (or subtract) the necessary number
of elements to make the vector of the indicated size. (If it adds
elements, they will be default-initialized).

But again, this is only my best guess given the limited
information you provided.

-Mike
Jan 22 '06 #2
TB
si***************@gmail.com sade:
Hi,

I have a C++ problem which uses STL containers and algorithm and it has
a segmentation fault under some condition.

Here is a back trace from gdb. Is it possible to tell what is wrong
from the trace? I know which method is crashing (MyHandler.cpp:133),
but I don't know why.

Thanks for any help.

Let me reorder the error messages a bit:
#5 0x05d6fa9d in do_divide::operator() (this=0xbf91985c,
element=0x8ccea30) at MyHandler.cpp:133
In do_divide::operator() you're apparently using std::copy().
#4 0x05d6eb89 in std::copy<__gnu_cxx::__normal_iterator<A**,
std::vector<A*, std::allocator<A*> > >,
__gnu_cxx::__normal_iterator<A**, std::vector<A*, std::allocator<A*>
(__first={_M_current = 0x89d4f90}, __last={_M_current = 0x89d4fa0},
__result={_M_current = 0x0}) at stl_algobase.h:387


The quasi-prototype for std::copy() is:
OutputIterator copy(InputIterator __first, InputIterator __last,
OutputIterator __result)

Now, look at what value '__result' has in your error message:
__last={_M_current = 0x0}

I'd guess you're passing an invalid iterator (containing a null pointer
in this case) to std::copy() which further down in memmove() causes
your segfault.
#0 0x008173a3 in memmove () from /lib/libc.so.6
#1 0x05d6eace in std::__copy<true,
std::random_access_iterator_tag>::copy<A*> (__first=0xf762b070,
__last=0x10, __result=0x0) at stl_algobase.h:300
#2 0x05d6eafb in std::__copy_aux<A**, A**> (__first=0x89d4f90,
__last=0x89d4fa0, __result=0x0) at stl_algobase.h:317
#3 0x05d6eb43 in std::__copy_normal<true,
true>::copy_n<__gnu_cxx::__normal_iterator<A**, std::vector<A*,
std::allocator<A*> > >, __gnu_cxx::__normal_iterator<A**,
std::vector<A*, std::allocator<A*> > > > (__first={_M_current =
0x89d4f90}, __last={_M_current = 0x89d4fa0}, __result={_M_current =
0x0}) at stl_algobase.h:354


Just watch that nasty bug propagate down the call stack.

--
TB @ SWEDEN
Jan 22 '06 #3
TB
TB sade:
si***************@gmail.com sade:
Hi,

I have a C++ problem which uses STL containers and algorithm and it has
a segmentation fault under some condition.

Here is a back trace from gdb. Is it possible to tell what is wrong
from the trace? I know which method is crashing (MyHandler.cpp:133),
but I don't know why.

Thanks for any help.


Let me reorder the error messages a bit:
> #5 0x05d6fa9d in do_divide::operator() (this=0xbf91985c,
> element=0x8ccea30) at MyHandler.cpp:133


In do_divide::operator() you're apparently using std::copy().
> #4 0x05d6eb89 in std::copy<__gnu_cxx::__normal_iterator<A**,
> std::vector<A*, std::allocator<A*> > >,
> __gnu_cxx::__normal_iterator<A**, std::vector<A*, std::allocator<A*>
> > >

> (__first={_M_current = 0x89d4f90}, __last={_M_current = 0x89d4fa0},
> __result={_M_current = 0x0}) at stl_algobase.h:387


The quasi-prototype for std::copy() is:
OutputIterator copy(InputIterator __first, InputIterator __last,
OutputIterator __result)

Now, look at what value '__result' has in your error message:
__last={_M_current = 0x0}


It should of course read

__result={_M_current = 0x0}

not

__last={_M_current = 0x0}

--
TB @ SWEDEN
Jan 22 '06 #4
That is indeed my problem:

Expand|Select|Wrap|Line Numbers
  1. std::vector<int> v1(10); // vector of ten zero-initialized integers
  2. std::vector<int> v2;     // *empty* vector of integers
  3.  
  4. std::copy(v1.begin(), v1.end(), v2.begin());  // ERROR, no place to
  5. write
  6.  
How can I make sure v2 has enough size before calling 'copy'?

Jan 23 '06 #5

si***************@gmail.com wrote:
That is indeed my problem:

Expand|Select|Wrap|Line Numbers
  1.  std::vector<int> v1(10); // vector of ten zero-initialized integers
  2.  std::vector<int> v2;     // *empty* vector of integers
  3.  std::copy(v1.begin(), v1.end(), v2.begin());  // ERROR, no place to
  4.  write
  5.  

How can I make sure v2 has enough size before calling 'copy'?


Create it that big to start with

std::vector<int> v2(10);

or create it then call resize

std::vector<int> v2;
v2.resize(10);

Another option which has v2 grow as required (instead of setting it to
the right size to start with) is to pass a back insert iterator for v2
to std::copy.

Gavin Deane

Jan 23 '06 #6
si***************@gmail.com wrote:
That is indeed my problem:

Expand|Select|Wrap|Line Numbers
  1.  std::vector<int> v1(10); // vector of ten zero-initialized integers
  2.  std::vector<int> v2;     // *empty* vector of integers
  3.  std::copy(v1.begin(), v1.end(), v2.begin());  // ERROR, no place to
  4.  write
  5.  

How can I make sure v2 has enough size before calling 'copy'?
Mike Wahler already anticipated your question and answered it. Here's
his response again:
If this is indeed your problem, look up 'insert_iterator',
'back_insert_iterator', 'back_inserter', etc. These constructs
will create storage for 'copy' to write to. Alternatively, look
at vector::resize(), which will add (or subtract) the necessary number
of elements to make the vector of the indicated size. (If it adds
elements, they will be default-initialized).


Best regards,

Tom

Jan 23 '06 #7
TB
si***************@gmail.com sade:
That is indeed my problem:

Expand|Select|Wrap|Line Numbers
  1.  std::vector<int> v1(10); // vector of ten zero-initialized integers
  2.  std::vector<int> v2;     // *empty* vector of integers
  3.  std::copy(v1.begin(), v1.end(), v2.begin());  // ERROR, no place to
  4.  write
  5.  

How can I make sure v2 has enough size before calling 'copy'?


One pratical way to solve this is to use a back inserter:

#include <iterator>
std::copy(v1.begin(), v1.end(), std::back_inserter(v2));

The inserter will call push_back() on the container for
each new element, thus relieving you of making sure
the container is preallocated to a certain size.

Another way is to resize the vector to the same size
of the source container:

v2.resize(v1.size());

--
TB @ SWEDEN
Jan 23 '06 #8

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

Similar topics

16
by: laberth | last post by:
I've got a segmentation fault on a calloc and I don'tunderstand why? Here is what I use : typedef struct noeud { int val; struct noeud *fgauche; struct noeud *fdroit; } *arbre; //for those...
3
by: I_have_nothing | last post by:
Hi! I am new in C. I got a lots of "Segmentation Fault"s in my code. I guess One possibility is: if " int array_i; " is declard and the code trys to access "array_i", a Segmentation Fault will...
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...
6
by: I_have_nothing | last post by:
Hi! I am new in C. I try to use dynamical allocation fuction malloc( ) and realloc( ). I found something strange. After several calling realloc( ), the malloc( ) will give me a Segmentation...
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
7
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
6
by: DanielJohnson | last post by:
int main() { printf("\n Hello World"); main; return 0; } This program terminate just after one loop while the second program goes on infinitely untill segmentation fault (core dumped) on...
2
by: Zach | last post by:
I compiled a game client and it crashed (segmentation fault) resulting in a core file being generated. I'm trying to find out exactly what caused it to crash. Any ideas how I can do this with gdb?...
4
by: jemccarthy13 | last post by:
I am a very very low level beginner programmer, trying to teach myself C, and I could use some help with the following program for guessing a random number. Please, when helping, try to use low...
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
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?
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...
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
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,...
0
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...

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.