#Segmentation Fault with Array of Structs

12 messages · Page 1 of 1 (latest)

celest tokenBOT
#

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.

peak sedge
#

And i don't know why i need to use lines[count].str instead of lines[count]->str

hushed beacon
#

You have not given str any pointer value.

foggy mural
#

!asan

celest tokenBOT
# foggy mural !asan

@peak sedge

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.
hushed beacon
#

-> is used to access a member of an object pointed to by a pointer; p->m is the same as (*p).m.

peak sedge
#

Ok, now is "working", i don't get a segfault, but now i have other problem

while (sup != NULL) {
    lines[count].str = (char *)malloc(sizeof(char) * sup->value->len);
    memcpy(lines[count].str, sup->value->str, sup->value->len);
    count += 1;
    sup = sup->next;
}
#

i'm just looping and printing the values

for (int i; i < lista->len; i++)
    printf("%s\n", lines[i].str);
hushed beacon
#

Try debugging.

peak sedge
#

well, i'm having problems with this too

#

ok, i find the problem, the debugger is compiling only the main file, not the headers, and i cant find then to edit this