Skip to content

How to not UB/segfault with mat4s fields in dyn-alloc'd struct arrays? #447

Answered by mghcoder
metaleap asked this question in Q&A
Discussion options

You must be logged in to vote

What is blowing up your alignment is the realloc() call in add

It seems random probably because sometimes realloc() is returning 32 byte aligned addresses and sometimes addresses aligned to less than 32 bytes (then the memory sanitizer throws an exception).

The C standard has no aligned_realloc(), Windows has _aligned_realloc(). As for other OSs, there might be some malloc like library that offers aligned_realloc(). When sticking with the standard library, you could do what you already do, aligned_alloc() + memcpy(); or request extra bytes to realloc(), align the address and memmove():

type struct list {
    void *ptr;
    node_t *nodes;
    size_t nodes_offset;
} list_t;
list_t list;
lis…

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
3 replies
@mghcoder
Comment options

@metaleap
Comment options

@mghcoder
Comment options

Answer selected by metaleap
Comment options

You must be logged in to vote
1 reply
@metaleap
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants