Thread: Char** problem

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    9

    Question Char** problem

    I am programming a TP with c , and I have this function that bugs and I do not know how to fix it; I have been trying for the last five days, and I still can't find where the problem is;so please help me fix it.
    Code:
    char **EtatsAccess(Automate *automate)
    {
        char **TabEtatAccess= malloc(sizeof(**TabEtatAccess));
                    int nbEtatAccess=0;int i=0;//i c'est l'element en cours
                    TabEtatAccess[nbEtatAccess]=automate->EtatInit;
                    //char *chaine=automate->EtatInit;
                    nbEtatAccess++;
                            if (automate->tab_transition == NULL)
                                {
                                    exit(EXIT_FAILURE);
                                }
                                while(i<nbEtatAccess)
                                {
                                  Instruction *actuel = automate->tab_transition->premier;
                                    while (actuel != NULL)
                                    {
                                        if(strcmp(TabEtatAccess[i],actuel->etatInit.nom)==0)
                                        {
                                            //vérifier si cet elem n'existe pas
                                            BOOLEAN exist=0;
                                            int i1=0;
                                            for(i1=0;i1<nbEtatAccess;i1++)
                                            {
                                                if (strcmp(TabEtatAccess[i1],actuel->etatFin.nom)==0)
                                                {
                                                    exist=1;
                                                    break;
                                                }
                                            }
                                            //Ajouter cet element
                                            if(!exist)
                                            {
                                             TabEtatAccess[nbEtatAccess]=actuel->etatFin.nom;
                                             actuel->etatFin.esAccess=1;
                                             nbEtatAccess++;
                                            }
                                        }
                                        actuel = actuel->suivant;
                                    }
                                    i++;
                                }
    
    
                                AffichEtatAccess(TabEtatAccess,nbEtatAccess);
                                automate->nbEtatAccess=nbEtatAccess;
                                return TabEtatAccess;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I suggest posting enough code so it can be compiled.
    I am think the info about the type Automate is at least needed.
    Edit: Info on type Instruction will likely also be needed.


    You do know that you are NOT likely allocating enough size in the malloc call?
    Note: This is just a guess since your code is NOT really clear on what you are trying to allocate to me.

    Tim S.
    Last edited by stahta01; 10-28-2014 at 08:59 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Well first of all, learning how to indent code properly would significantly aid in clarity (both for you and us).

    Code:
    char **EtatsAccess(Automate * automate)
    {
      char **TabEtatAccess = malloc(sizeof(**TabEtatAccess));
      int nbEtatAccess = 0;
      int i = 0;                    //i c'est l'element en cours
      TabEtatAccess[nbEtatAccess] = automate->EtatInit;
      //char *chaine=automate->EtatInit;
      nbEtatAccess++;
      if (automate->tab_transition == NULL) {
        exit(EXIT_FAILURE);
      }
      while (i < nbEtatAccess) {
        Instruction *actuel = automate->tab_transition->premier;
        while (actuel != NULL) {
          if (strcmp(TabEtatAccess[i], actuel->etatInit.nom) == 0) {
            //vérifier si cet elem n'existe pas
            BOOLEAN exist = 0;
            int i1 = 0;
            for (i1 = 0; i1 < nbEtatAccess; i1++) {
              if (strcmp(TabEtatAccess[i1], actuel->etatFin.nom) == 0) {
                exist = 1;
                break;
              }
            }
            //Ajouter cet element
            if (!exist) {
              TabEtatAccess[nbEtatAccess] = actuel->etatFin.nom;
              actuel->etatFin.esAccess = 1;
              nbEtatAccess++;
            }
          }
          actuel = actuel->suivant;
        }
        i++;
      }
    
    
      AffichEtatAccess(TabEtatAccess, nbEtatAccess);
      automate->nbEtatAccess = nbEtatAccess;
      return TabEtatAccess;
    }
    Next, you need to learn how to use malloc properly.
    > char **TabEtatAccess = malloc(sizeof(**TabEtatAccess));
    Every * in the declaration needs it's own independent malloc call. You can't allocate a ** variable in a single malloc call.

    So for example,
    Code:
    char **TabEtatAccess;
    TabEtatAccess = malloc( numberOfRows * sizeof(*TabEtatAccess) );
    for ( i = 0 ; i < numberOfRows ; i++ )
        TabEtatAccess[i] = malloc( numberOfCols * sizeof(*TabEtatAccess[i]) );
    
    // This is the malloc equivalent of
    // char TabEtatAccess[numberOfRows][numberOfCols]
    Also notice the use of sizeof in this code. Using the form p = malloc( sizeof(*p) ); saves you from having to check back at the declaration to get the type right.

    > TabEtatAccess[nbEtatAccess] = actuel->etatFin.nom;
    You might want to think hard about what you're doing here.
    It looks like you're going to end up with two pointers in two different data structures pointing at the SAME bit of memory.
    If one of those data structures is deallocated, the other could end up with garbage pointers.

    For safety, you should really make a copy of the string using strcpy() - remembering to make sure you have enough memory allocated.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2014
    Posts
    9
    Hi, thank you so much for taking time and helping me with my tp, and Im sorry for posting my code that way, but now I learned how to indent code correctely thanks to your advice.
    Besides, you explained well how to use malloc, so I corrected all the memory allocations in my TP, and now everything works, thank you again.
    Code:
    char **EtatsAccess(Automate *automate)
    {
        char **TabEtatAccess;
        TabEtatAccess= malloc(20*sizeof(*TabEtatAccess));
        int nbEtatAccess=0;int i=0;               //i c'est l'element en cours
        ///**Initialisation**///
        TabEtatAccess[nbEtatAccess]=malloc(10*sizeof(*TabEtatAccess[nbEtatAccess]));
        strcpy(TabEtatAccess[nbEtatAccess],automate->EtatInit);
        nbEtatAccess++;
        if (automate->tab_transition == NULL){
         exit(EXIT_FAILURE);
        }
        while(i<nbEtatAccess){
            Instruction *actuel = automate->tab_transition->premier;
            while (actuel != NULL){
                if(strcmp(TabEtatAccess[i],actuel->etatInit.nom)==0){
                   //vérifier si cet elem n'existe pas
                    BOOLEAN exist=0;
                    int i1=0;
                    for(i1=0;i1<nbEtatAccess;i1++)
                    {
                        if (strcmp(TabEtatAccess[i1],actuel->etatFin.nom)==0){
                            exist=1;
                            break;
                        }
                    }
                    //Ajouter cet element
                    if(!exist){
                        TabEtatAccess[nbEtatAccess]=malloc(10*sizeof(*TabEtatAccess[nbEtatAccess]));
                        strcpy(TabEtatAccess[nbEtatAccess],actuel->etatFin.nom);
                        actuel->etatFin.esAccess=1;
                        nbEtatAccess++;
                    }
                }
            actuel = actuel->suivant;
            }
        i++;
        }
        AffichEtatAccess(TabEtatAccess,nbEtatAccess);
        automate->nbEtatAccess=nbEtatAccess;
        return TabEtatAccess;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 07-24-2012, 10:41 AM
  2. Basic char */const char* problem
    By wisuzu in forum C++ Programming
    Replies: 7
    Last Post: 12-02-2011, 11:56 AM
  3. Replies: 9
    Last Post: 08-09-2011, 12:41 PM
  4. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  5. Problem with char and char*
    By chris1985 in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 11:44 AM