How to fill an entire vector with malloc (sizeof (int)) with some value

2

I have a problem, I have no idea how to fill this vector with some value, for example, I want to fill it all with -1 (start it all with -1). The problem here is that I'm not sure exactly what the size of my vector is.

    typedef struct argumentosThread {   
        int dim, queens, posicoes, total;
        int *posicCertas;               
        int head;                       
    }ArgumentosThread;

In the middle of the code I initialize it, I put the sizeof (int) because I do not know what the size of the vector will be, it can be very small or VERY large.

    argumentos->posicCertas = malloc(sizeof(int));
    
asked by anonymous 04.10.2018 / 03:26

1 answer

0

When you allocate this vector using malloc, also store its size in the structure ...

And make a for loop to fill as desired.

argumentos->posicCertas = (int*)malloc(sizeof(int)*n_pos);
argumentos->n_pos = n_pos;

Then just make one by filling in the vector.

It's also interesting to do a casting of the malloc return.

    
04.10.2018 / 14:32