c++ malloc segment fault
- by chnet
I have a problem about malloc(). It is wired.
My code is in the following.
I use random generator to generate elements for an array.
The array is opened by malloc().
If the array size is smaller than 8192, it is OK. If the size is larger than 8192, it shows segment fault.
void random_generator(int num, int * array) {
srand((unsigned)time(0));
int random_integer;
for(int index=0; index< num; index++){
random_integer = (rand()%10000)+1;
*(array+index) = random_integer;
cout << index << endl;
}
}
int main() {
int array_size = 10000;
int *input_array;
input_array = (int*) malloc((array_size));
random_generator(8192, input_array); // if the number is larger than 8192, segment fault
free(input_array);
}