Re: Newbie needing some help with segmentation fault
- From: Hendrik Maryns <gtw37bn02@xxxxxxxxxxxxxx>
- Date: Thu, 06 Dec 2007 11:24:46 +0100
James Kuyper schreef:
Hendrik Maryns wrote:
Martin Ambuhl schreef:...
Hendrik Maryns wrote:
mgTreeNode *loadTree(char *file)Did this fopen succeed? If it did not (and you don't check) the
{
hcreate(MAX_NODES);
char symbol[MAX_SYMBOL];
char name[MAX_NODENAME];
char left[MAX_NODENAME];
char right[MAX_NODENAME];
ENTRY node;
char* cache[MAX_NODES][3];
size_t nodeCounter=0;
size_t i;
FILE *fp = fopen(file, "r");
following fscanf dereferences a null pointer.
This indeed was the problem: I gave the wrong arguments to the debug
program.
How do I properly check whether the open succeeded? Is
if (fp == 0) exit(-2);
enough?
There are only three things you can portably pass to exit():
EXIT_SUCCESS, EXIT_FAILURE (both #defined in <stdlib.h> or 0. Note: not
all programs need to be portable, but you should write non-portable code
only by reason of a deliberate decision, not as a matter of habit.
I totally agree. However, I use the exit value in the calling program
(in Java). And there, 1 has the special meaning: ACCEPT. So I would
not want to return 1, to which EXIT_FAILURE is defined. I guess I will
make it a little bit more non-portable for now, until I read up on JNI
and can avoid using the exit value by calling the relevant functions
directly from my Java program. But I’ll definitely remember this.
There's no guarantee that EXIT_SUCCESS is 0, nor is there any guarantee
that EXIT_SUCCESS is not 0. It can be arguably be inferred that
EXIT_FAILURE must be different from both 0 and EXIT_SUCCESS.
I normally would add a perror(file) call before the exit() call, for
diagnostic purposes, but only if it's acceptable for your program to
send something to stderr.
I would also recommend avoiding direct calls to exit(). In most cases a
subroutine should report a problem to it's caller, allowing the caller
to perform any cleanup that may be needed.
Good point. I’m so used to Java’s automatic garbage collection that I
forgot about this. But even there it is better to throw an exception or
something.
However, if the process is totally dead, won’t the OS reclaim the memory?
Thanks, H.
--
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Attachment:
signature.asc
Description: OpenPGP digital signature
- Follow-Ups:
- Re: Newbie needing some help with segmentation fault
- From: CBFalconer
- Re: Newbie needing some help with segmentation fault
- From: James Kuyper
- Re: Newbie needing some help with segmentation fault
- References:
- Newbie needing some help with segmentation fault
- From: Hendrik Maryns
- Re: Newbie needing some help with segmentation fault
- From: Martin Ambuhl
- Re: Newbie needing some help with segmentation fault
- From: Hendrik Maryns
- Re: Newbie needing some help with segmentation fault
- From: James Kuyper
- Newbie needing some help with segmentation fault
- Prev by Date: mingw socket programming
- Next by Date: Re: c++ calling c functions
- Previous by thread: Re: Newbie needing some help with segmentation fault
- Next by thread: Re: Newbie needing some help with segmentation fault
- Index(es):
Relevant Pages
|