473,320 Members | 2,088 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,320 software developers and data experts.

segmentation fault - code attached

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.
Nov 14 '05 #1
9 2001
On Sun, 2004-10-03 at 19:52 -0700, 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


If you're getting segfaults in a *alloc call, you're most likely doing
some core fandango somewhere. That can be one of the hardest errors to
debug, since your fault might well be in a completely different part of
the source, which may have been executed long before the actual fault.

<OT>
However, since you're running Linux on i386, I'd recommend that you
fetch Valgrind, which is _great_ for debugging such issues. It's an i386
emulator that catches suspicious access to suspicious memory locations.
</OT>

Fredrik Tolf
Nov 14 '05 #2
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);

HTH,
--ag
--
Artie Gold -- Austin, Texas

"If you don't think it matters, you're not paying attention."
Nov 14 '05 #3
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."
Nov 14 '05 #4
Artie Gold wrote:
.... snip ...
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
`struct Var'. This is why the recommended form of use members of
the malloc/calloc family is, for example:

ptr = calloc(1, sizeof *ptr);


The use of calloc to assign space for pointers is worthless. There
is no guarantee that "all bytes zero" represents a NULL pointer.
So the overhead of calloc is wasted, use malloc and explicitly set
it to NULL.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #5
CBFalconer wrote:
Artie Gold wrote:

... snip ...
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
`struct Var'. This is why the recommended form of use members of
the malloc/calloc family is, for example:

ptr = calloc(1, sizeof *ptr);

The use of calloc to assign space for pointers is worthless. There


The OP had -- mistakenly.
is no guarantee that "all bytes zero" represents a NULL pointer.
So the overhead of calloc is wasted, use malloc and explicitly set
it to NULL.


Agreed. The structs the OP was using, IIRC, consisted of integral fields
-- and I wasn't looking to get into the malloc/calloc discussion.

Cheers,
--ag

--
Artie Gold -- Austin, Texas

"If you don't think it matters, you're not paying attention."
Nov 14 '05 #6
Do you guys suggest me that I sould never use the code
if(!ptr) { }
and I should explicitly equate it to NULL? as in
if(ptr == NULL) {}
???
Thanks,
Naren.
Artie Gold <ar*******@austin.rr.com> wrote in message news:<2s*************@uni-berlin.de>...
CBFalconer wrote:
Artie Gold wrote:

... snip ...
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
`struct Var'. This is why the recommended form of use members of
the malloc/calloc family is, for example:

ptr = calloc(1, sizeof *ptr);

The use of calloc to assign space for pointers is worthless. There


The OP had -- mistakenly.
is no guarantee that "all bytes zero" represents a NULL pointer.
So the overhead of calloc is wasted, use malloc and explicitly set
it to NULL.


Agreed. The structs the OP was using, IIRC, consisted of integral fields
-- and I wasn't looking to get into the malloc/calloc discussion.

Cheers,
--ag

Nov 14 '05 #7
Thanks a lot Art,
Sometimes silly bugs like these elude my eyes :). Anyway I've never
used valgrind and on this occassion, you have prevented me from using
it - thanks for all those who helped ....
Regards,
Naren.
Artie Gold <ar*******@austin.rr.com> wrote in message news:<2s*************@uni-berlin.de>...
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

Nov 14 '05 #8
Narendran Kumaraguru Nathan <na*************@yahoo.co.uk> wrote:

Please don't top-post.
Artie Gold <ar*******@austin.rr.com> wrote in message news:<2s*************@uni-berlin.de>...
CBFalconer wrote:
> Artie Gold wrote:
>>`struct Var'. This is why the recommended form of use members of
>>the malloc/calloc family is, for example:
>>
>> ptr = calloc(1, sizeof *ptr);
>
>
> The use of calloc to assign space for pointers is worthless. There
The OP had -- mistakenly.
> is no guarantee that "all bytes zero" represents a NULL pointer.
> So the overhead of calloc is wasted, use malloc and explicitly set
> it to NULL.
>


Agreed. The structs the OP was using, IIRC, consisted of integral fields
-- and I wasn't looking to get into the malloc/calloc discussion.

Do you guys suggest me that I sould never use the code
if(!ptr) { }
and I should explicitly equate it to NULL? as in
if(ptr == NULL) {}
???


No. There is some additional compiler magic involved. Even when the
bit representation of a NULL pointer isn't all bits 0 a comparison
with NULL (or just 0) must always result in the expected result, i.e.
the compiler, when it sees that you compare a pointer to 0 must
do the comparison as if the pointer would be all bits 0. It will
also set the pointer to whatever represents a NULL pointer on your
machine when you assign NULL (or 0) to the pointer - but, as it has
been pointed out, that isn't guaranteed to be a value with all bits
set to 0 and thus calloc() can't do the right thing since it doesn't
know that the memory you allocate is going to be used for a pointer.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #9
na*************@yahoo.co.uk (Narendran Kumaraguru Nathan) writes:
Do you guys suggest me that I sould never use the code
if(!ptr) { }
and I should explicitly equate it to NULL? as in
if(ptr == NULL) {}
???
Thanks,
Naren.


Please don't top-post. New material should follow quoted material,
and quoted material should usually be trimmed. See most of the
articles in this newsgroup for examples.

Assuming that you have an include directive for one of the headers
that defines the NULL macro, and assuming that ptr is a pointer
object, "if (!ptr) ..." and "if (ptr == NULL)" are exactly equivalent.
The "!" operator returns true if and only if its operand compares
equal to zero; for a pointer, comparison to zero is comparison to a
null pointer. See section 5 of the C FAQ, at
<http://www.eskimo.com/~scs/C-faq/top.html>. (Read the whole thing
while you're there.)

Which one you should use is a matter of style. Personally, I prefer
"ptr == NULL" because it's more explicit, but there are valid reasons
for preferring "!ptr". If you're going to be working with other
people's code (or with your own after you've written it), you need to
understand both forms.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #10

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

Similar topics

2
by: sivignon | last post by:
Hi, I'm writing a php script which deals with 3 ORACLE databases. This script is launch by a script shell on an linux machine like this : /../php/bin/php ./MySript.php (PHP 4.3.3) My script...
6
by: AMT2K5 | last post by:
Hello guys. I have a function, cleanSpace . I was told from another source that the problem is, is that the function is acting on constant string literal. To fix this I was told to take the...
7
by: Alexandre | last post by:
Hello, Maybe it's a little OT, but the fact is that I don't necessarly want to know "how to correct?", but "why it happens?" I have a program who "segment fault" (ok, that's "normal"... ;-)...
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...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
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...
1
by: Greatrebel | last post by:
Hi all, I wrote a SystemC program which was compiled fine with g++, but as run, I got a segmentation fault: Program received signal SIGSEGV, Segmentation fault. 0x0804a720 in mpsoc (n_slave=2)...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.