Re: Newbie needing some help with segmentation fault



James Kuyper schreef:
Hendrik Maryns wrote:
Martin Ambuhl schreef:
Hendrik Maryns wrote:
...
mgTreeNode *loadTree(char *file)
{
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");
Did this fopen succeed? If it did not (and you don't check) the
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



Relevant Pages

  • Re: [PATCH] x86: fix improper .init-type section references
    ... You might've done this because the caller of ... But modpost will still complain about calling __exit ... So I'd guess all the various notifier callback functions in your patch ...
    (Linux-Kernel)
  • Re: Newbie needing some help with segmentation fault
    ... ENTRY node; ... There's no guarantee that EXIT_SUCCESS is 0, nor is there any guarantee that EXIT_SUCCESS is not 0. ... I normally would add a perrorcall before the exit() call, for diagnostic purposes, but only if it's acceptable for your program to send something to stderr. ... In most cases a subroutine should report a problem to it's caller, allowing the caller to perform any cleanup that may be needed. ...
    (comp.lang.c)
  • Re: Should every procedure/function have an On Error... trap terminated with an On Error GoTo 0?
    ... Next" in one routine, VB would not auto clear the error upon exit, ... No error is generated in the caller, ...
    (microsoft.public.vb.general.discussion)
  • Style question (Was: Using a pipe as a stack)
    ... This brings up a style question: mgetline() calls exit(). ... function such as this one is a Bad Idea -- that, instead, worker ... caller of mgetline's caller's caller, ...
    (comp.unix.programmer)
  • Re: Question on SSH hang on exit when running Java background process
    ... Since i have to send signals to the process, eg, Send SIGINT to tell the process to shutdown, ... That will tie the background process to PID 1, freeing up the current shell to exit whenever you want, whether the java program is done or not. ... My problem is that my SSH client hang on "exit" if I run a java program ...
    (SSH)