2

Sorry, I'm new to c programming

As the title says, The code runs perfectly till the end of main where it returns 0. It then gives a seg fault with no reason why. Some answers said that maybe I wasnt freeing all that I malloced but I did. So I tried using gdb to figure out why. This was the first time I've ever used it.

This is the output:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7644f1d in ?? () from /lib/x86_64-linux-gnu/libc.so.6
(gdb) bt
#0  0x00007ffff7644f1d in ?? () from /lib/x86_64-linux-gnu/libc.so.6
#1  0x00007ffff76450aa in ?? () from /lib/x86_64-linux-gnu/libc.so.6
#2  0x00007ffff760365b in ?? () from /lib/x86_64-linux-gnu/libc.so.6
#3  0x00007ffff76036f5 in exit () from /lib/x86_64-linux-gnu/libc.so.6
#4  0x00007ffff75eaecc in __libc_start_main ()
   from /lib/x86_64-linux-gnu/libc.so.6
#5  0x0000000000400bc9 in _start ()

My main:

int main(int argc, char *argv[]) {

    if(argv[1] == NULL)
    {
        printf("Please enter the path to the map generating file as an argument.\n");
        exit(0);
    }

    run(getName(), argv[1]);
    return 0;
}

My program is a ncurses program, which i can (I believe I am) succesfully create the screen and then close it. I've checked that all the malloced variables have been freed as well.

Run is in a diffrent c file where I draw the ncurses board.

Any help would be appreciated.

Exikle
  • 1,155
  • 2
  • 16
  • 40
  • 1
    You have undefined behavior when no argument is passed to the program. – emlai Mar 04 '15 at 07:45
  • 1
    @zenith sorry what do you mean? isnt that the null checker? or am i thinking of something else? – Exikle Mar 04 '15 at 07:46
  • May be off topic,but what is argument types of function `run`? – Vagish Mar 04 '15 at 07:46
  • 2
    `argv[1] == NULL` should be `argc < 2` – usr1234567 Mar 04 '15 at 07:47
  • 4
    It is possible that your `run()` method is corrupting the stack. Try to reproduce the problem with a simpler version of `run()`. – merlin2011 Mar 04 '15 at 07:49
  • @Exikle Sorry my mistake, [`argv[argc]` *is* actually a null pointer](http://stackoverflow.com/questions/16418932/is-argvargc-equal-to-null-pointer). You learn something new everyday... – emlai Mar 04 '15 at 07:54
  • 2
    Try to run it in valgrind – Ôrel Mar 04 '15 at 09:14
  • Given gdb output is not sufficient. May be you should use `list` and `run` commands on gdb to find on which line you are receiving Seg Fault –  Mar 04 '15 at 10:22

1 Answers1

0

It's not sure that gdb will give you the exact location when the corruption really happens. (the backtrace suggests it's a stack-related one)

For this kind of errors, the best tool is Valgrind, run your app with it.

(In my experience memory corruption errors could be tracked down and eliminated within minutes with Valgrind)

lorenzodarkside
  • 136
  • 2
  • 4