[prog] C++ - linked list and segmentation fault

Almut Behrens almut-behrens at gmx.net
Wed Apr 9 00:11:32 EST 2003


On Tue, Apr 08, 2003 at 01:10:56PM -0400, Jennifer Davis wrote:
> 
> I was wondering if I could get some advice on a program I am trying to
> complete for class.  It is a killer as I am new to linked lists.  I write
> the program and it compiles.  However, when it runs, it segfaults.  I'm
> sure it happens when I am creating the list.  All help would be very much
> appreciated.

for debugging memory-related problems I'd warmheartedly recommend a tool
called "valgrind". As it's relatively new, it might not be known widely.
There's a whole bunch of similar tools like Electric Fence etc., but
valgrind is particularly easy to use -- despite the complexity under
the hood. It's already saved me from hours of hair-pulling, when having
to deal with segfaulting programs or libs...

Running it on your program (as you posted it here), you'd get something
like:

$ g++ -g -o linkedlist-test linkedlist-test.cpp 
$ valgrind linkedlist-test
==1585== Memcheck, a.k.a. Valgrind, a memory error detector for x86-linux.
==1585== Copyright (C) 2002, and GNU GPL'd, by Julian Seward.
==1585== Using valgrind-1.9.5, a program instrumentation system for x86-linux.
==1585== Copyright (C) 2000-2002, and GNU GPL'd, by Julian Seward.
==1585== Estimated CPU clock rate is 1314 MHz
==1585== For more details, rerun with: -v
==1585== 
==1585== Conditional jump or move depends on uninitialised value(s)
==1585==    at 0x8048ADB: createFile(INVENTORY_ITEM *) (linkedlist-test.cpp:42)
==1585==    by 0x80489A1: main (linkedlist-test.cpp:20)
==1585==    by 0x402ADC6E: __libc_start_main (in /lib/libc.so.6)
==1585==    by 0x80488C0: (within /home/ab/test/linkedlist-test)
==1585== 
==1585== Use of uninitialised value of size 4
==1585==    at 0x4016A9B4: strcmp (vg_clientfuncs.c:474)
==1585==    by 0x8048AF8: createFile(INVENTORY_ITEM *) (linkedlist-test.cpp:42)
==1585==    by 0x80489A1: main (linkedlist-test.cpp:20)
==1585==    by 0x402ADC6E: __libc_start_main (in /lib/libc.so.6)
==1585== 
==1585== Conditional jump or move depends on uninitialised value(s)
==1585==    at 0x4016A9B8: strcmp (vg_clientfuncs.c:475)
==1585==    by 0x8048AF8: createFile(INVENTORY_ITEM *) (linkedlist-test.cpp:42)
==1585==    by 0x80489A1: main (linkedlist-test.cpp:20)
==1585==    by 0x402ADC6E: __libc_start_main (in /lib/libc.so.6)
==1585== 
==1585== Conditional jump or move depends on uninitialised value(s)
==1585==    at 0x4016A9C0: strcmp (vg_clientfuncs.c:479)
==1585==    by 0x8048AF8: createFile(INVENTORY_ITEM *) (linkedlist-test.cpp:42)
==1585==    by 0x80489A1: main (linkedlist-test.cpp:20)
==1585==    by 0x402ADC6E: __libc_start_main (in /lib/libc.so.6)
==1585== 
==1585== Use of uninitialised value of size 4
==1585==    at 0x8048B0C: createFile(INVENTORY_ITEM *) (linkedlist-test.cpp:43)
==1585==    by 0x80489A1: main (linkedlist-test.cpp:20)
==1585==    by 0x402ADC6E: __libc_start_main (in /lib/libc.so.6)
==1585==    by 0x80488C0: (within /home/ab/test/linkedlist-test)
==1585== 
==1585== Invalid write of size 4
==1585==    at 0x8048B22: createFile(INVENTORY_ITEM *) (linkedlist-test.cpp:44)
==1585==    by 0x80489A1: main (linkedlist-test.cpp:20)
==1585==    by 0x402ADC6E: __libc_start_main (in /lib/libc.so.6)
==1585==    by 0x80488C0: (within /home/ab/test/linkedlist-test)
==1585==    Address 0x0 is not stack'd, malloc'd or free'd
Segmentation fault


This tells you more or less directly that problems are occuring in
lines 42-44 of your code. With these hints, taking a closer look at the
source, you'd probably have seen that it must be the variable p which
is pointing to an uninitialised memory location...

Interested? -- then go and check out the following links:

http://developer.kde.org/~sewardj/
http://developer.kde.org/~sewardj/docs-1.9.5/manual.html

I'm not sure whether there are any prebuilt binary packages, so you
might have to build it from source. I just tried it with the most
recent sources -- worked out of the box for me.

Cheers,
Almut


More information about the Programming mailing list