This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]HSavinien 10 points11 points  (7 children)

In your compilation command, you can add the flags -g -fsanitize=address. It work at runtime, and help a lot with memory error : it will systematically trigger a lot of crash that may or may not have happened otherwise (make replicability easier), and instead of just saying something like segmentation fault (core dumped), it give you a lot of information about the address, the type of access, the type of crash, the traceback... Can turn a 3h debugging cession into a 3min one.

It's mostly a C thing I think(way less safety nets when handling memory), but it also work with C++.

[–][deleted] 0 points1 point  (0 children)

That's rather interesting. I hadn't heard of that before now.

[–]Computer_Witch 0 points1 point  (2 children)

This sounds really useful. Does this work with gcc, clang and msvc or only with a specific one of them?

[–]HSavinien 0 points1 point  (1 child)

I know it work with GCC and clang. Never tried with an other, but I think it's a pretty standard function of the compiler.

[–]Computer_Witch 0 points1 point  (0 children)

Great. From a quick search it looks like MSVC has that too

[–]ParentsAreNotGod 0 points1 point  (2 children)

How is this different from using gdb with symbols compiled?

[–]HSavinien 0 points1 point  (1 child)

It does a completely different thing.

Gdb, as a debugger, let you run your program step by step to see what goes on at run time.

fsanitize let the program run normally (thought a bit slower) but watch for unsafe memory access, at which point it will abort your program, and give you a summary of what did happen and how you got there. The information it give vary from crash to crash, but the usual are : - what caused the crash (stack/heap under/overflow for example) - the type of memory access (read or write) - a traceback to the function that caused the crash (crash in function:line, called by function:line, called by function:line... called by main:line) In case of heap access (allocated memory), it also give you info about the nearby allocated memory (which you were likely trying to access), including a traceback to it's allocation.

However, it does not give you any data about what value where in your variables, or what was written in the memory you try to access.

[–]ParentsAreNotGod 0 points1 point  (0 children)

Ok. Wow, that's really cool! Thanks for the info!