lundi 17 avril 2017

Getting seg fault when assigning random float value in a loop from rand()

I cannot figure out why my code works only one way and it is driving me crazy because it just should work. I want to fill my allocated memory with floats. The thing is that this code does not work:

for(j=0; j<*length; j++){
             values[j] = (rand()%200)/MAGIC_CONSTANT;   
    }

But this one does without any problem:

for(j=0; j<*length; j++){
             help = (rand()%200)/MAGIC_CONSTANT;
             values[j] = help;
}

MAGIC_CONSTANT = 73.

The thing is it only works when I first assign it to my help variable and then to the memory and whenever I assign it right away it gives me segmentation fault. What is wrong with that rand() function?

Thanks!




Aucun commentaire:

Enregistrer un commentaire