Help with segfault - Programming On Unix
Users browsing this thread: 3 Guest(s)
|
|||
So I'm barely getting into C and wanted to start with something that goes step by step, fairly recent, and then going to run through K&R2. Chose Zed A. Shaw's Learn C the Hard Way. Exercise 16 ran perfect for me. Until I rebuilt my laptop with Debian on it. I was running Solus. Now when I try to run the following code, it segfaults right after completing the printf statement on line 51. I tried running it through gdb and I get an error saying strlen.S No such file or directory, so I'm not sure what I'm missing to be able to debug this small program. Weird thing is, if I run it through valgrind, it does NOT segfault. It also does not segfault on my work Fedora install. This is verbatim code for exercise 16 from the book.
Is there a perfectly valid reason this code would segfault, or is my machine misconfigured? Code: #include <stdio.h> |
|||
|
|||
running the code through my machine, I get this warning
Code: 51: warning: format '%p' expects type 'void *', but argument 2 has type 'struct Person *' Maybe that's your issue? The strlen warning is weird, as you have no calls to strlen in your code. Hopefully someone else will be able to help you better. Here is a good list of c resources that you might appreciate: http://www.iso-9899.info/wiki/Main_Page |
|||
|
|||
Your issue is not a direct call to strlen(3), it an internal function of the libc called strlen.S which seems to fail. This is the kind of things that happen when you pass a NULL pointer to a function in string.h (eg. strdup(3).
I ran it on my computer and don't get any warning at compilation, and couldn't get it to segfault though. We'll need the full trace of your runs here. |
|||
|
|||
i wasn't under the impression a NULL pointer was being passed... how do i provide a full trace?
|
|||
|
|||
strace ./yourprog
valgrind ./yourprog gdb ./yourprog Then send us the WHOLE output. A segfault occurs when some function tries to access a forbidden memory area. This is 90% of the time due to a NULL pointer being passed. In your case, I can't see were this could happen though. |
|||
|
|||
I can't get it to segfault either. Running it with valgrind is also fine.
|
|||
|
|||
Code: $ strace ./ex16 |
|||
|
|||
That's really odd... Try to add this at the top of your Person_print() function:
Code: assert(who != NULL); |
|||
|
|||
It compiled and still segfaults. I'm going to assume my machine is missing something somehow. I installed the `build-essential` meta-package as well as some extras. If I can't get it to work on Debian, I'm probably going to switch to another distro... I'm starting to run out of those.
|
|||