#include <time.h>
int perm_table[255];
float rand_vectors[255][3];
initialize_random_numbers()
{
int i;
float tmp[3], length;
for (i=0; i<255; i++)
{
perm_table[i] = rand() % 255;
tmp[0] = (float)(rand() % 1000);
tmp[1] = (float)(rand() % 1000);
tmp[2] = (float)(rand() % 1000);
printf("\n%i", i);
memcpy(rand_vectors[i], tmp, sizeof(float)*3);
}
}
Running this prints every number from 0 to 253 (on 255), before giving a Segmentation Fault.
Commenting out this line
perm_table[i] = rand() % 255;
removes the error.
I am completely beaten by this. Any help?
There is nothing wrong in this code for me.
The segfault probably occurs after the function returns and not because of this function.
The fact that the last number printed is
253is because the stream is not flushed afterprintf("\n%i", i);and as the segfault occurs after the function, the stream never got flushed.By default, when connected to a terminal,
stdoutis line-buffered. To flushstdouteither callfflush(stdout)or print a'\n'character.