473,218 Members | 1,476 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,218 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 2978

<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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.