Re: segmentation fault - code attached

From: Artie Gold (artiegold_at_austin.rr.com)
Date: 10/04/04


Date: Sun, 03 Oct 2004 22:18:50 -0500

Artie Gold wrote:
> Narendran Kumaraguru Nathan wrote:
>
>> Hi all,
>> I am fairly experianced in C. I am writing a program in which I'm
>> getting a segmentation fault. The problem is that it is getting the
>> segmentation fault when executing calloc. I tried debugging it, with
>> no use. I tried to figure out the common reasons (1) Using a memory
>> location which is not allocated. (2) Using memory which is freed. (3)
>> Function use before declaration.
>> I didn't use any other debuggers or tools other than ddd. My
>> platform is : RH linux 2.4.20-8 & gcc -v ouputs the following
>>
>> Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/specs
>> Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
>> --infodir=/usr/share/info --enable-shared --enable-threads=posix
>> --disable-checking --with-system-zlib --enable-__cxa_atexit
>> --host=i386-redhat-linux
>> Thread model: posix
>> gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
>>
>> I don't understand what to do. I couldn't attach the program I am
>> developing, hence have put that in geocities with a sample input. The
>> files are under
>> http://www.geocities.com/narenkumaraguru/vcdedit/
>> The files are
>> 1. main.c.c
>> 2. vcd_read.c.c
>> 3. vcd_read.h.c
>> 4. Makefile.c
>> 5. cntr.vcd.c
>>
>> Since I couldn't upload files with different names, I postfixed .c
>> to all of them. Once downloaded please correct the names by removing
>> the extra .c in them.
>>
>> Procedure to run it is
>> 1. make
>> 2. ./vcd_read
>>
>> I'm aint a C wizard. The program is to parse a file format called
>> the VCD. It is used in VLSI for debugging and waveform dump in
>> particular. I was writing a library for the format.
>>
>> Thanks,
>> Naren.
>
>
> Note the following (in vcd_Read.c):
>
> case var:
> if (!LastScope) { /* $scope should have preceeded */
> fprintf(stderr, "Error %d: detected $var before $scope!\n",
> line_no);
> exit(1);
> }
> TempVar = (struct Var *)
> calloc( 1, sizeof(struct Var *) );
>
> ITYM:
> calloc( 1, sizeof(struct Var) );
>
> TempVar->msb_index = TempVar->lsb_index = -1;
> if (!LastVar) {
> LastScope->var = TempVar;
> } else {
> LastVar->next = TempVar;
> }
>
>
> The cast is unnecessary (you may see previous discussions in
> news:comp.lang.c for details) -- but the problem is that you've only
> allocated enough space for a pointer to `struct Var' not an instance of
> `struct Var'. This is why the recommended form of use members of the
> malloc/calloc family is, for example:
>
> ptr = calloc(1, sizeof *ptr);
>

Oops, forgot to mention:

You didn't get a segfault right there -- but you did corrupt your free
store, causing a segfault on a future allocation.

HTH,
--ag

-- 
Artie Gold -- Austin, Texas
"If you don't think it matters, you're not paying attention."


Relevant Pages

  • Re: segmentation fault - code attached
    ... > segmentation fault when executing calloc. ... It is used in VLSI for debugging and waveform dump in ... I was writing a library for the format. ... allocated enough space for a pointer to `struct Var' not an instance of ...
    (comp.lang.c)
  • Re: Want function or class that uses reflection to iterate over properties in custom object
    ... use a "real" IDE with debugging support. ... > Why don't you use the debugger instead of writing debug code. ... I build and extend a lot of custom objects, ... I've learned that you can do this using reflection. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Gallery Pagination Result Problem
    ... I use Altova XML Spy Home Edition when writing simple XML scripts, and it has the added benefit of validating the XML and pointing out errors against the DTD. ... Is there such a free program for writing PHP that has a good debugging mode or validation? ... And if I'm not using a debugger, I've found strategically placed echo statements do wonders for debugging. ...
    (comp.lang.php)
  • Re: ANSI C compliance
    ... > Writing a significant chunk of embedded code. ... > workstation and debugging it there because there were far better tools ... I used some stubs for platform specific functionality. ... C++ Faq: http://www.parashift.com/c++-faq-lite ...
    (comp.lang.c)