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.
62 messages ยท Page 1 of 1 (latest)
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.
Could you try to compile and run with address sanitizer enabled? That should tell you what line the segfault is occurring on
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
-gfor gcc/clang and-Zifor msvc.
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.
whats sanitzer
This post should explain what it is
do i need to install it?
Should be with gcc on linux
Should still work? Not sure
Just try adding -fsanitize=address to your compile flags
do i run it like gdb?
Once you compile it, just run the program like normal. When you hit the segfault, address sanitizer will emit all the debug info
Yeah, that should be all you need. Mac must not have native support for it
ye and i tried running it on lunix then got a binary error
what?
Can you share the full error message?
You're probably trying to run the binary built for mac, so it doesn't know what to do
ye is there a way to fix it
Are you passing -fsanitize=address to the command that links all the object files as well?
i havent yet
Ah, yeah. Try adding $(CFLAGS) to the end of your link command
Run your clean target and rebuild
oh ye now it runs then seg fault hits
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
Ah, you did it through gdb. That's fine as well
First thing I'd check is your indices there
What is grid?
I.e. what datatype is it?
It's likely that you're indexing out of the bounds of grid, so check the values you're indexing it by
Cause I think you want to do grid[newX][newX] == '$' instead of ... == "$"
char ** grid
Also I'm not too sure if [newX][newX] is what you want rather than [newX][newY] or smth like that
Yeah, then you need to do ... == '$'
Otherwise you're comparing a char against a char *
Which obviously doesn't work
Didn't spot the double quotes lol. I was focusing on the segfault. Your screenshot shows you're compiling with warnings. Don't ignore those
this one sorry
You want to compare against a character (i.e. char), not against a string (i.e. char *)
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
oh have u guys made a snake game before
nope
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
oh wait just understtod what u said lmao
But I did once download a snake game, fully written in pure native Windows batch ๐
@calm pawn
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.
!solve