473,915 Members | 3,885 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<tru e,
std::random_acc ess_iterator_ta g>::copy<A*> (__first=0xf762 b070,
__last=0x10, __result=0x0) at stl_algobase.h: 300
#2 0x05d6eafb in std::__copy_aux <A**, A**> (__first=0x89d4 f90,
__last=0x89d4fa 0, __result=0x0) at stl_algobase.h: 317
#3 0x05d6eb43 in std::__copy_nor mal<true,
true>::copy_n<_ _gnu_cxx::__nor mal_iterator<A* *, std::vector<A*,
std::allocator< A*> > >, __gnu_cxx::__no rmal_iterator<A **,
std::vector<A*, std::allocator< A*> > > > (__first={_M_cu rrent =
0x89d4f90}, __last={_M_curr ent = 0x89d4fa0}, __result={_M_cu rrent =
0x0}) at stl_algobase.h: 354
#4 0x05d6eb89 in std::copy<__gnu _cxx::__normal_ iterator<A**,
std::vector<A*, std::allocator< A*> > >,
__gnu_cxx::__no rmal_iterator<A **, std::vector<A*, std::allocator< A*> >
(__first={_M_cu rrent = 0x89d4f90}, __last={_M_curr ent = 0x89d4fa0}, __result={_M_cu rrent = 0x0}) at stl_algobase.h: 387

#5 0x05d6fa9d in do_divide::oper ator() (this=0xbf91985 c,
element=0x8ccea 30) at MyHandler.cpp:1 33

Jan 22 '06 #1
7 3036

<si************ ***@gmail.com> wrote in message
news:11******** *************@g 44g2000cwa.goog legroups.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<tru e,
std::random_acc ess_iterator_ta g>::copy<A*> (__first=0xf762 b070,
__last=0x10, __result=0x0) at stl_algobase.h: 300
#2 0x05d6eafb in std::__copy_aux <A**, A**> (__first=0x89d4 f90,
__last=0x89d4fa 0, __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.be gin(), v1.end(), v2.begin()); // ERROR, no place to write

If this is indeed your problem, look up 'insert_iterato r',
'back_insert_it erator', '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::oper ator() (this=0xbf91985 c,
element=0x8ccea 30) at MyHandler.cpp:1 33
In do_divide::oper ator() you're apparently using std::copy().
#4 0x05d6eb89 in std::copy<__gnu _cxx::__normal_ iterator<A**,
std::vector<A*, std::allocator< A*> > >,
__gnu_cxx::__no rmal_iterator<A **, std::vector<A*, std::allocator< A*>
(__first={_M_cu rrent = 0x89d4f90}, __last={_M_curr ent = 0x89d4fa0},
__result={_M_cu rrent = 0x0}) at stl_algobase.h: 387


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

Now, look at what value '__result' has in your error message:
__last={_M_curr ent = 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<tru e,
std::random_acc ess_iterator_ta g>::copy<A*> (__first=0xf762 b070,
__last=0x10, __result=0x0) at stl_algobase.h: 300
#2 0x05d6eafb in std::__copy_aux <A**, A**> (__first=0x89d4 f90,
__last=0x89d4fa 0, __result=0x0) at stl_algobase.h: 317
#3 0x05d6eb43 in std::__copy_nor mal<true,
true>::copy_n<_ _gnu_cxx::__nor mal_iterator<A* *, std::vector<A*,
std::allocator< A*> > >, __gnu_cxx::__no rmal_iterator<A **,
std::vector<A*, std::allocator< A*> > > > (__first={_M_cu rrent =
0x89d4f90}, __last={_M_curr ent = 0x89d4fa0}, __result={_M_cu rrent =
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::oper ator() (this=0xbf91985 c,
> element=0x8ccea 30) at MyHandler.cpp:1 33


In do_divide::oper ator() you're apparently using std::copy().
> #4 0x05d6eb89 in std::copy<__gnu _cxx::__normal_ iterator<A**,
> std::vector<A*, std::allocator< A*> > >,
> __gnu_cxx::__no rmal_iterator<A **, std::vector<A*, std::allocator< A*>
> > >

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


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

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


It should of course read

__result={_M_cu rrent = 0x0}

not

__last={_M_curr ent = 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_iterato r',
'back_insert_it erator', '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.be gin(), v1.end(), std::back_inser ter(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.si ze());

--
TB @ SWEDEN
Jan 23 '06 #8

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

Similar topics

16
9019
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 who don't speak french arbre means tree.
3
2130
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 be returned by gcc compiler on linux. What are the other "major" reasons to have "Segmentation Fault"? I hope this is not a stupid question and someone is will to give me some exampls(part of code) with errors a newer might esasily make and
3
11479
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 () from /lib/tls/libc.so.6 It's really strange; I just call malloc() like "tmp=malloc(size);" the system gives me Segmentation fault I want to write a code to do like a dynamic array, and the code is as
6
4792
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 fault. If I just call realloc( ) once before calling malloc( ), it is OK. Why? I am trying to read some double-typed items from infile and save them
27
3392
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! #include <stdlib.h> #include <stdio.h> typedef struct _node_t {
7
5888
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 testcases we experiance Segmentation fault from the python libraries. If i know how to handle the exception for Segmentation fault , it will help me complete the run on any testcase , even if i experiance Seg Fault due to any one or many functions in...
3
5198
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 from 10g release2 PHP is configured with the following parameters './configure' '--prefix=/opt/oracle/php' '--with-apxs=/opt/oracle/apache/bin/apxs' '--with-config-file-path=/opt/oracle/apache/conf' '--enable-safe-mode' '--enable-session'...
6
5051
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 gcc. The only difference is that in first I only call "main" and in second call
2
3714
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? In the Makefile can I just add a "-g" flag to have the binary produced with debugging symbols? The source is written in ANSI C. This is what I have now: "CC = gcc" The client binary is 433680 and the core file produced when it crashed
4
3036
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 level functions and such. A Segmentation Fault occurs sometime between the running of lines 30-40 while debugging. #include <stdio.h>
0
9883
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
11359
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
9734
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
8102
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
7259
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
5944
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4779
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
4346
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.