MacOS C Inconsistent behavior

Your code looks like a harder/wrong way to to implement a flexible array member. Just using one correctly, like this:

typedef struct _dVec{
    uint32_t len;
    float _val[];
}_dVec;

//Create vector mem
Vector vecinit(int lenght){
    
    //Allocate and init vector
    Vector _vec = malloc(sizeof(_dVec) + lenght * sizeof(float));
    _vec->len = lenght;
    
    return _vec;
}

Should solve all your problems. If you’re really set on doing it the hard way, just fix allocation:

Vector _vec = malloc(sizeof(_dVec) + sizeof(float) * lenght);

and your pointer arithmetic:

_vec->_val = (float *)(_vec + 1);

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top