#Seg fault

17 messages · Page 1 of 1 (latest)

left ginkgo
#

Hello, I am new to c, I have experience with java and I am now on a course at university doing C.

The program is supposed to work on Linux compiled with GCC.

I have written it through 5 days and I tried to go through it and change stuff but I'm still getting segmentation faults.

Here is the code:

https://pastebin.com/ZBxfX4mn

languid locustBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

#
Address Sanitizer

Memory errors in C and C++ are easy to make and they can be very hard to debug because they can manifest far from their source. Address sanitizer is a runtime checker that identifies memory errors at their source and makes debugging much simpler. Address sanitizer is available for gcc/clang on linux and msvc on windows. To use it simply pass -fsanitize=address to the compiler.

Note: Make sure to turn on debug symbols with -g for gcc/clang and -Zi for msvc.

ce Example

How to read sanitizer output

The first few lines tell you the problem, heap-use-after-free, due to performing a READ of size 4, at example.c line 7 (from the first line of the stack trace).

==1==ERROR: AddressSanitizer: heap-use-after-free on address ....
READ of size 4 at 0x602000000010 thread T0
    #0 0x40120f in main /app/example.c:7
    #1 0x7fda58629d8f  (...)
    #2 0x7fda58629e3f in __libc_start_main (...)
    #3 0x4010b4 in _start (...)

Additional information is also included such as where the allocation was performed and where the allocation was freed.

See Also
  • Other sanitizers exist and can be similarly helpful, including ubsan, threadsan, and memorysan.
tender veldt
#

tl;dr gcc -fsanitize=address -g ...

#

then run the program

#

I hope your code doesn't look the way you pasted it, as it is horrible to look at it ;p

left ginkgo
#

Nope, it was too long so I had to do it thru pastebin

tender veldt
#

but it looks horrible on pastebin

left ginkgo
#

Ah

tender veldt
#

no indentation

#

and instead large number of blank lines

tender veldt
left ginkgo
#

I'm sorry about that. Ok

tender veldt
#

when you run your code after that you will get information on how you ended up with a seg fault

#

another tip: always use -Wall -Wextra -Wpedantic -Werror with gcc, that will help you learn better

left ginkgo
#

Thank you very much, I found something that may be the problem. ^^.

#

!solved