Re: segmentation fault - code attached
From: Artie Gold (artiegold_at_austin.rr.com)
Date: 10/04/04
- Next message: Wojtek Lerch: "Re: contiguity of arrays"
- Previous message: Wojtek Lerch: "Re: contiguity of arrays"
- In reply to: Artie Gold: "Re: segmentation fault - code attached"
- Next in thread: Narendran Kumaraguru Nathan: "Re: segmentation fault - code attached"
- Reply: Narendran Kumaraguru Nathan: "Re: segmentation fault - code attached"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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."
- Next message: Wojtek Lerch: "Re: contiguity of arrays"
- Previous message: Wojtek Lerch: "Re: contiguity of arrays"
- In reply to: Artie Gold: "Re: segmentation fault - code attached"
- Next in thread: Narendran Kumaraguru Nathan: "Re: segmentation fault - code attached"
- Reply: Narendran Kumaraguru Nathan: "Re: segmentation fault - code attached"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|