Tableau + valgrind

Zellas -  
daronmaster Messages postés 326 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour,

J'ai malloc un tableau, mais quand j'essaye mon programme sous valgrind j'ai une erreur, si quelqu'un sait comment l'enlever se serait sympa...

#include "mysh.h"

void cpy_envp(char **envp, t_struct *t)
{
int i;
int a;

i = 0;
a = 0;
while (envp[a] != NULL)
a++;
t->my_envp = malloc(sizeof(t->my_envp) * (a + 2));
a = 0;
while (envp[a] != NULL)
{
t->my_envp[a] = strdup(envp[a]);
a++;
}
t->my_envp[a + 1] = NULL;
}

int main()
{
t_struct *t;
extern char **environ;
int j;

j = 0;
t = malloc(sizeof(*t));
cpy_envp(environ, t);
while (t->my_envp[j])
{
free(t->my_envp[j]);
j++;
}
free(t);
return (0);
}

voila le message d'erreur :

==16885== Conditional jump or move depends on uninitialised value(s)
==16885== at 0x80486BA: main (main.c:43)
==16885==
==16885== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
==16885== malloc/free: in use at exit: 156 bytes in 1 blocks.
==16885== malloc/free: 39 allocs, 38 frees, 1984 bytes allocated.
==16885== For counts of detected errors, rerun with: -v
==16885== searching for pointers to 1 not-freed blocks.
==16885== checked 1506448 bytes.
Configuration: FreeBSD
Firefox 2.0.0.6

1 réponse

  1. daronmaster Messages postés 326 Date d'inscription   Statut Membre Dernière intervention   44
     
    ce que tu codes est vraiment chelou ...

    si ton prog consiste à copier des chaines alors voila ce que j'aurai fait :

    void cpy_envp(char **envp, t_struct *t) 
    { 
    
        t->my_envp = (char *)malloc(sizeof( char ) * (strlen(*envp)+1) );
    
        strcpy(t->my_envp,*envp);
    
    } 
    
    
    int main() 
    { 
        t_struct *t; 
        extern char **environ; 
     
        t = malloc(sizeof(t_struct )); 
    
        t->my_envp = NULL;
        cpy_envp(environ, t); 
    
        if (t->my_envp != NULL ) free( t->my_envp);
        
        free(t); 
        
        return 0; 
    } 
    
    


    essaie voir
    0