Erreur bizarre : tableau 1D + ouverture fichier.
Bonjour,
J'ai deux problèmes :
1- la taille alouée de mon tableau 1D est limitée à 162 éléments, dés que je veux allouer un tableau 1D de plus de 162 éléments ça me donne :
Code:
1 2
| Process terminated with status 1 (0 minutes, 1 seconds)
0 errors, 0 warnings |
voici mon code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| #include <stdio.h>
#include <stdlib.h>
typedef enum Array_err_ {
ARRAY_OK,
ARRAY_ERR_NULL_ARG,
ARRAY_ERR_SIZE,
ARRAY_ERR_MEMORY,
ARRAY_ERR_UNKNOWN
} Array_err_e;
//*** Fonction pour faire l'allocation dynamique de mémoire pour un tableau 1D ***
static int array1dfloat_new(float **array, int a)
{
int err = ARRAY_OK;
if (array != NULL)
{
if (a > 0)
{
float *self = NULL;
self = malloc( sizeof *self);
if (self != NULL)
{
int i;
/* Initialisation de tous les éléments à 0 */
for (i = 0; i < a ; ++i)
{
self[i] = 0.0;
}
*array = self;
}
else
{
err = ARRAY_ERR_MEMORY;
}
}
else
{
err = ARRAY_ERR_SIZE;
}
}
else
{
err = ARRAY_ERR_NULL_ARG;
}
return err;
}
// *** Fonction pour libérer la mémoire allouée
static void array1dfloat_free(float **array)
{
if (array != NULL)
{
float *self = *array;
free(self);
*array = NULL;
}
}
int main(void)
{
int err = EXIT_SUCCESS;
int arrer = ARRAY_OK;
float *data = NULL; // tableau 1D
arrer = array1dfloat_new(&data,163); // si 162 à la place de 163 ça marche !!!???
if (arrer != ARRAY_OK)
{
fprintf(stderr, "Erreur d'allocation! data \n");
err = EXIT_FAILURE;
}
array1dfloat_free(&data);
return err;
} |
2- 2éme problème est quand j'ajoute une simple ouverture et fermeture d'un fichier à un code qui marche il ne marche plus et le compilateur me donne le meme message d'echec precedent !!! pourquoi ??
voici mon code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| int main(void)
{
int err = EXIT_SUCCESS;
int arrer = ARRAY_OK;
float *data = NULL; // tableau 1D
FILE *f1;
arrer = array1dfloat_new(&data,100);// je réspecte la limite 163.
if (arrer != ARRAY_OK)
{
fprintf(stderr, "Erreur d'allocation! data \n");
err = EXIT_FAILURE;
}
f1=fopen("source.dat","w"); // sans l'ouverture et fermeture du fichier le code marche bien dés que j'ajoute le travail avec le fichier çà ne marche plus ?
if (f1== NULL)
{
printf("Impossible d'ouvrire le fichier source.dat");
exit (1);
}
fclose(f1);
array1dfloat_free(&data);
return err;
} |
merci de m'aider a comprendre où est le problème??