473,563 Members | 2,767 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2011
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********@yah oo.com) (cb********@wor ldnet.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.lan g.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*******@aust in.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.lan g.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*******@aust in.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*******@aust in.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***********@p hysik.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_Keit h) 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
6794
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 works fine and do all what I need. But at the end of the execution, I can read "Segmentation Fault". The segmentation fault appear at the end of my...
6
2566
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 incoming string, copy it to a buffer, work on the buffer and copy the buffer back to the original. I tried but no sucess. The seg fault is shown...
7
3341
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"... ;-) but this time, it's not my code who "segment fault" but it's in the call of malloc/mallopt I've got:
3
11395
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 () from /lib/tls/libc.so.6 It's really strange; I just call malloc() like "tmp=malloc(size);" the system gives me Segmentation fault I want to...
5
2982
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 understood that a segmentation fault can occur whenever I declare a pointer and I leave it un-initialized. So I thought the problem here is with the (const...
27
3333
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! #include <stdlib.h> #include <stdio.h> typedef struct _node_t {
7
5861
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 testcases we experiance Segmentation fault from the python libraries. If i know how to handle the exception for Segmentation fault , it will help me...
3
5144
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 from 10g release2 PHP is configured with the following parameters './configure' '--prefix=/opt/oracle/php' '--with-apxs=/opt/oracle/apache/bin/apxs'...
1
1962
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) at ./src/main.cpp:149 149 router_mapfile << slave_array->name() << endl; The fault is located on this line ...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7642
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6255
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.