Bonjour à tous!

Je pensais maitriser l'allocation dynamique de la mémoire mais je suis tombé sur un problème que je ne comprends pas.

Soit la structure suivante, avec ces deux méthodes associées:
fir_mov_avrg_t * new_fir_mov_avrg(const uint32_t size)
void destroy_fir_mov_avrg(fir_mov_avrg_t ** const pthis)

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
typedef struct
{
    float      * tab;
    uint32_t   size;
    uint32_t   wrptr;
    uint32_t   rdptr;
    float      n;
    float      acc;
    float      mean;
}fir_mov_avrg_t;
 
 
fir_mov_avrg_t * new_fir_mov_avrg(const uint32_t size)
{
    fir_mov_avrg_t * this = malloc(sizeof(this));
    if(this)
    {
        memset(this,0,sizeof(*this));
        if((this->tab = calloc(size,sizeof(*(this->tab)))))
        {
            this->size  = size;
        }
        else
        {
            destroy_fir_mov_avrg(&this);
        }
    }
    return this;
}/* end new_fir_mov_avrg*/
 
void destroy_fir_mov_avrg(fir_mov_avrg_t ** const pthis)
{
    if(pthis && *pthis)
    {
        fir_mov_avrg_t * this = *pthis;
        if(this->tab){free(this->tab);}
        free(this);
        *pthis = NULL;
    }
}/* end destroy_fir_mov_avrg*/
Lorsque j'appelle new_fir_mov_avrg(25) alors calloc échoue et renvoie NULL.

Mais lorsque je commente la ligne memset(this,0,sizeof(*this)); calloc renvoie un pointeur valide....

Je ne comprends pas pourquoi !