#Seg Fault

62 messages ยท Page 1 of 1 (latest)

terse sparrowBOT
#

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.

tulip summit
#

Could you try to compile and run with address sanitizer enabled? That should tell you what line the segfault is occurring on

terse sparrowBOT
#
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.
calm pawn
#

whats sanitzer

tulip summit
#

This post should explain what it is

calm pawn
#

do i need to install it?

tulip summit
#

Should be with gcc on linux

calm pawn
#

im on mac

#

is that an issue

tulip summit
#

Should still work? Not sure

#

Just try adding -fsanitize=address to your compile flags

calm pawn
#

do i run it like gdb?

tulip summit
#

Once you compile it, just run the program like normal. When you hit the segfault, address sanitizer will emit all the debug info

calm pawn
#

oh okok

#

i just tried it doesnt work

#

like that right?

tulip summit
#

Yeah, that should be all you need. Mac must not have native support for it

calm pawn
#

ye and i tried running it on lunix then got a binary error

robust ibex
#

Can you share the full error message?

calm pawn
#

ye i can

#

one moment

tulip summit
#

You're probably trying to run the binary built for mac, so it doesn't know what to do

calm pawn
#

ye is there a way to fix it

tulip summit
#

Are you passing -fsanitize=address to the command that links all the object files as well?

calm pawn
#

i havent yet

tulip summit
#

Ah, yeah. Try adding $(CFLAGS) to the end of your link command

calm pawn
tulip summit
#

Run your clean target and rebuild

calm pawn
#

oh ye now it runs then seg fault hits

tulip summit
#

Yep. If you skim through the error you should see a summary line, which will tell you the file and line the segfault occurred on

calm pawn
#

i got the gdb

tulip summit
#

Ah, you did it through gdb. That's fine as well

#

First thing I'd check is your indices there

calm pawn
#

oh ok is it saying that the $ is out of bounds

#

or something

robust ibex
#

I.e. what datatype is it?

tulip summit
#

It's likely that you're indexing out of the bounds of grid, so check the values you're indexing it by

robust ibex
#

Cause I think you want to do grid[newX][newX] == '$' instead of ... == "$"

calm pawn
#

char ** grid

robust ibex
#

Also I'm not too sure if [newX][newX] is what you want rather than [newX][newY] or smth like that

robust ibex
#

Otherwise you're comparing a char against a char *

#

Which obviously doesn't work

tulip summit
robust ibex
#

You want to compare against a character (i.e. char), not against a string (i.e. char *)

robust ibex
# calm pawn this one sorry

Well, rn you're using the same variable/value for both indices.
This may be correct, but could also be incorrect, but depends on what you want to do

calm pawn
#

oh have u guys made a snake game before

robust ibex
#

nope

tulip summit
#

No, but usually you index a grid by an x and y, not x and x

#

Depends on what you're doing of course. This is just guessing lol

calm pawn
#

oh wait just understtod what u said lmao

robust ibex
#

But I did once download a snake game, fully written in pure native Windows batch ๐Ÿ’

calm pawn
#

ye NewX and NewY

#

oh thanks ill look into it

#

and see how it works

terse sparrowBOT
#

@calm pawn

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.

calm pawn
#

!solve