0

So I'm working on a little C program which is a little address book that automatically allocates memory when you add a new contact in it.

I'm using two typedef structures, the first one stores the info on the contact (name telephone etc.):

typedef struct
{
    char nom[TAILLE1];
    char tel[TAILLE2];
} CONTACT;

The seconde one contains a int with the number of contacts in the address book and an other one is the pointer to the other structure.

typedef struct
{
    int nb;
    CONTACT * contacts; // tableau
} LISTE_TABLEAU; 

I created a function to import contact from a TXT file (first line the name of the person, second line their telephone number and so on). I simplified it with only the basic

int lireDonneesTxt(LISTE_TABLEAU* tab)
{
    int i;
    tab->contacts = (CONTACT *)malloc(sizeof(CONTACT)*13); (13 because there are 13 contact for testing purposes)
    i = 0;

    while( !feof(entree) )
    {
        fgets(ligne, TAILLE1, entree);
        strcpy(tab->contacts[i].nom, ligne);

        fgets(ligne, TAILLE1, entree);
        strcpy(tab->contacts[i].tel, ligne);

        i++
    }
    return 1;
}

When I compile my code there isn't any issue, no warning what so ever.

But when I run my code everything works great until I try and print a name on the screen, then the executable file crashes.

My main function looks something like this:

int main(void)
{
    LISTE_TABLEAU *tabb;
    tabb->nb    = 0;

    lireDonneesTxt(&tabb);

    printf("%s", tabb->contacts[0].nom);

    return 0;
}

If I add the same printf that is in the main at the end of my lireDonneesTxt function it prints the name without any problem. I'm guessing that the data is not passed correctly to the structure.

I am now blocked and have no idea what to try to make this work !

Joris Blanc
  • 601
  • 1
  • 10
  • 24
  • If you're on Linux, use valgrind to troubleshoot your memory access problems. – John Zwinck Jun 07 '13 at 12:29
  • 1
    [Don't cast the return value of `malloc()` in C](http://stackoverflow.com/a/605858/28169). – unwind Jun 07 '13 at 12:31
  • 1
    `lireDonneesTxt()` - ah bon, vraiment? **No one ever** uses non-English identifiers in one's code... –  Jun 07 '13 at 12:31
  • @H2CO3 Obviously, "No one ever" is a bit of an overstatement. It may be highly disrecommended, and even uncommon, to use non-English identifiers, but it doesn't take much searching to find counter-examples to that statement... – twalberg Jun 07 '13 at 15:58
  • @twalberg I meant, "no sane programmer" :) –  Jun 07 '13 at 16:41

2 Answers2

3

Don't pass the address of the LISTE_TABLEAU pointer here:

lireDonneesTxt(&tabb);

just pass the pointer

lireDonneesTxt(tabb);

Also setting tabb->nb = 0; to unalocated memory is undefined behaviour.

0

The memory is not allocated for "tabb" & you are trying to access its member variable (tabb->nb) in main() function. It is illegal, thus the run time crash.

You can try the below code instead:

int main(void)
{
    LISTE_TABLEAU tabb;
    tabb.nb    = 0;

    lireDonneesTxt(&tabb);

    printf("%s", tabb.contacts[0].nom);

    return 0;
}
Subbu M
  • 194
  • 1
  • 5