J'ai un problème concernant malloc(). C'est étrange. Mon code est le suivant. J'utilise un générateur aléatoire pour générer des éléments pour un tableau. Le tableau est ouvert par malloc(). Si la taille du tableau est inférieure à 8192, c'est OK. Si la taille est supérieure à 8192, cela indique une erreur de segment.C++ erreur de segmentation de malloc
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);
}
er ... si facile de répondre ... – chnet
Avez-vous besoin spécifiquement d'utiliser des rangées de style C? –
Juste une question d'expérience. Après un certain temps, vous développez un réflexe pour attendre sizeof() chaque fois que vous voyez malloc(). – Dima