473,466 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

seg fault and I don't know where

Hello,

I am making a program that encrypts and compresses plain text and indexes
them by date.

Needless to say, I have many alloc(), realloc(), calloc(), and free() calls.
There are also double pointers all over the place.

The program works fine except for one tiny detail. Every time I try to
extract a file, the program terminates with a segmentation fault. I used
the debugger and discovered that the segmentation fault did not occur
because of an out of bounds error or anything. The segmentation fault
occurs when the program exits.

I thought that the problem was that I did not free() some block of memory
that I had previousally allocated. I checked and made sure that I had
free()d all the memory.

Could someone possibly offer an explanition as to why this keeps happening?

Thanks,
Nov 13 '05 #1
8 2130
James Leddy wrote:
Hello,

I am making a program that encrypts and compresses plain text and indexes
them by date.

Needless to say, I have many alloc(), realloc(), calloc(), and free() calls.
There are also double pointers all over the place.

The program works fine except for one tiny detail. Every time I try to
extract a file, the program terminates with a segmentation fault. I used
the debugger and discovered that the segmentation fault did not occur
because of an out of bounds error or anything. The segmentation fault
occurs when the program exits.
Ah, but most likely it *is* an `out of bounds' situation -- just not
one that causes a crash when you exceeded the bounds of a buffer,
but at the point where whatever you overwrote was used.

I thought that the problem was that I did not free() some block of memory
that I had previousally allocated. I checked and made sure that I had
free()d all the memory.
Failure to free() allocated memory may be a Bad Thing, but will not
cause a program to crash.

Could someone possibly offer an explanition as to why this keeps happening?

Show us the smallest compilable snippet that exhibits the problem
and someone, no doubt, will.

HTH,
--ag
--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

Nov 13 '05 #2
>I thought that the problem was that I did not free() some block of memory
that I had previousally allocated. I checked and made sure that I had
free()d all the memory.


It is unlikely that a segmentation fault would be caused by failure
to free memory you allocated. (Standards wizards: I think a
segfault for failure to free memory would not be allowed, as this
failure doesn't invoke undefined behavior. Opinions?)

It is quite possible that a segmentation fault would be caused
by free()ing something you DIDN'T allocate (with malloc & friends).
So could stomping on the malloc() arena caused by using more
memory than you actually allocated.

Gordon L. Burditt
Nov 13 '05 #3
James Leddy wrote:
Could someone possibly offer an explanition as to why this keeps happening?
You are derefrencing the NULL pointer on line 42.
Thanks,


No prob.

--
Noah Roberts
- "If you are not outraged, you are not paying attention."

Nov 13 '05 #4

"James Leddy" <jl*****@binghamton.edu> wrote in message
news:3f********@bingnews.binghamton.edu...
Hello,

I am making a program that encrypts and compresses plain text and indexes
them by date.

Needless to say, I have many alloc(), realloc(), calloc(), and free() calls. There are also double pointers all over the place.

The program works fine except for one tiny detail. Every time I try to
extract a file, the program terminates with a segmentation fault. I used
the debugger and discovered that the segmentation fault did not occur
because of an out of bounds error or anything. The segmentation fault
occurs when the program exits.

I thought that the problem was that I did not free() some block of memory
that I had previousally allocated. I checked and made sure that I had
free()d all the memory.

Could someone possibly offer an explanition as to why this keeps happening?
Thanks,


As an educated guess, I would say that you are accessing one of your arrays
out-of-bounds, and when you are coming to free it then you get your
segfault.
If you cant find it, then I would suggest getting a good memory wrapper for
your environment and testing thoroughly.
HTH
Allan
Nov 13 '05 #5
Gordon Burditt wrote:
I thought that the problem was that I did not free()
some block of memory that I had previousally allocated.
I checked and made sure that I had free()d all the memory.
It is unlikely that a segmentation fault would be caused by failure
to free memory you allocated. (Standards wizards: I think a
segfault for failure to free memory would not be allowed, as this
failure doesn't invoke undefined behavior. Opinions?)


Yes, but ...
the failure to free, especially if in a loop,
may cause a memory shortage.
A memory shortage could cause malloc and friends to fail.
If malloc or friends fail,
and the code is written in such a way which always assumes success,
then all of that taken together, could lead to UB.
It is quite possible that a segmentation fault would be caused
by free()ing something you DIDN'T allocate (with malloc & friends).
So could stomping on the malloc() arena caused by using more
memory than you actually allocated.


That could do it too.

--
pete
Nov 13 '05 #6
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote:

"James Leddy" <jl*****@binghamton.edu> wrote in message
news:3f********@bingnews.binghamton.edu...

<snip>
I thought that the problem was that I did not free() some block of memory
that I had previousally allocated. I checked and made sure that I had
free()d all the memory.

Could someone possibly offer an explanition as to why this keeps

happening?

As an educated guess, I would say that you are accessing one of your arrays
out-of-bounds, and when you are coming to free it then you get your
segfault.


Any attempt to free() an _array_ always leads to undefined behaviour.

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #7
James Leddy wrote:

[...] Every time I try to
extract a file, the program terminates with a segmentation fault. I used
the debugger and discovered that the segmentation fault did not occur
because of an out of bounds error or anything. The segmentation fault
occurs when the program exits. [...]


In addition to the possibilities others have mentioned,
the fact that the crash occurs upon exit suggests two more
things you might want to check for:

- If you have used atexit() to register any functions
to be run when the program terminates, make sure
any data that those functions use is still "live."
If one of the registered functions refers to memory
already freed, or uses an already-closed FILE* stream,
or uses a pointer to an automatic variable in a
function that's already returned, pretty much
anything might happen.

- If you have used setvbuf() to designate an I/O
buffer of your own for some FILE* stream, make sure
(as above) that the buffer still exists until the
stream gets closed, either explicitly by your use
of fclose() or automatically as part of program
termination.

--
Er*********@sun.com
Nov 13 '05 #8
James Leddy <jl*****@binghamton.edu> writes:
Hello,

I am making a program that encrypts and compresses plain text and indexes
them by date.

Needless to say, I have many alloc(), realloc(), calloc(), and free() calls.
There are also double pointers all over the place.
There is no function alloc() in the Standard C library. Did you
mean malloc()?
The program works fine except for one tiny detail. Every time I try to
extract a file, the program terminates with a segmentation fault. I used
the debugger and discovered that the segmentation fault did not occur
because of an out of bounds error or anything. The segmentation fault
occurs when the program exits.

I thought that the problem was that I did not free() some block of memory
that I had previousally allocated. I checked and made sure that I had
free()d all the memory.

Could someone possibly offer an explanition as to why this keeps happening?


This sort of thing typically happens when free() was called with
a block of memory that was never allocated with malloc() or
calloc(), or when memory you did not have a right to touch was
accessed. This can be a *very* tricky situation to track down:
there are tools available to help detect illegal memory access,
such as Electric Fence (which is for UNIX systems only, I
believe).

HTH,
Micah
Nov 13 '05 #9

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

Similar topics

10
by: Vishal Grover | last post by:
Hello Everyone, I am seeing a certain behaviour which I find strange, and am curious to get an explanation to it. I have the following program. #include <iostream> #include <cstdlib> using...
9
by: fudmore | last post by:
Hello Everybody. I have a Segmentation fault problem. The code section at the bottom keeps throwing a Segmentation fault when it enters the IF block for the second time. const int...
9
by: Maksim Kasimov | last post by:
Hello, my programm sometime gives "Segmentation fault" message (no matter how long the programm had run (1 day or 2 weeks). And there is nothing in log-files that can points the problem. My...
10
by: Peter Dragun | last post by:
I am generally new to programming under Unix, but know how to code under Windows. I have to create a simple program, that takes the following information from a file using redirection: 4 4 4 4 4...
0
by: Paiman Allage | last post by:
Hello all, I hope you can help me. I have developed an ".NET" application and started to deploy the application. However, I am haveing a problem, where when I compile the program on my laptop,...
3
by: Kiran | last post by:
Hello All, In my program, I have a main thread which is the GUI (wxPython) and then a thread which goes and reads data from a socket. The reason this is in a different thread is because the data...
10
by: Linny | last post by:
Hi All, I am pasting a piece of code which executes fine on 32 bit system but fails with a segmentation fault when compiled 64 bit compiler.I am using a HP-UX C compiler on PA-RISC system. This...
8
by: Bryan | last post by:
Hello all. I'm fairly new to c++. I've written several programs using std::vectors, and they've always worked just fine. Until today. The following is a snippet of my code (sorry, can't...
85
by: Bill Cunningham | last post by:
I have put together this program that seems to do what I want without error checking added yet. If I just run the program it produces a segmentation fault. If I add the proper value say 43.56 it's...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
1
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...
0
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,...
0
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...
0
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 ...

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.