I have some problem with realloc()
:
int main(int argc, char* argv[])
{
int* amis;
int saisie, cpt = 1;
while(saisie != -1) {
printf("Entrer les notes -1 pour quitter :");
scanf("%d", &saisie);
if (cpt == 1) {
amis = malloc(sizeof(int));
if(amis == NULL) {
printf("amis == NULL");
exit(0);
}
}
if(saisie != -1) {
amis = realloc(amis, sizeof (int) + sizeof (amis));
if(amis == NULL) {
printf("amis == NULL cpt= %d", cpt);
exit(0);
}
amis[cpt] = saisie;
printf("size = %d, saisie = %d, tab = %d \n", cpt * sizeof(int), saisie, amis[cpt]);
cpt++;
}
}
printf("%d",1==0);
afficherTab(amis,cpt);
printf("END\n");
free(amis);
return 0;
}
Why does realloc()
cause an error when I use sizeof(int) * cpt
instead of sizeof(amis) + sizeof(int)
?
free(amis)
also doesn't work in that case.