-1

I am writing an semestral work in C but I am unable to recognize the problem here. When I run the code in debug, it gets me a segmentation fault. I am not sure what's wrong. Previously I alocated memory to poleNazvu but instead of segmentation fault it was giving me "trying to free non heap object" so this is not a way. Please tell me how to deal with the segmentation fault. Its happening on the line where I strcpy(poleNazvu... ) I marked it in the code. ALOKACE_POLI = 200 ; ALOKACE_MALA=20; What it does is that it takes a "john 500" and levaStrana gets "john" while pravaStrana gets "500" So I am trying to copy the levaStrana to poleNazvu and pravaStrana to poleKreditu. At the end of this code snip is the segmentation fault.

void menuHighlights(void)                                               //menu se zebricky uzivatelu
{
    char vyber3, * poleNazvu[ALOKACE_POLI][ALOKACE_MALA],line[ALOKACE_LINE];
    char * prohodJmeno, * menic, * separator, * levaStrana, * pravaStrana;
    int poleKreditu[ALOKACE_POLI], count=0, countMax=0, prohodCislo, d;
    FILE *NACTENI;
    //int alokace1=0, alokace2=0;                                                       //vytvoreni promenne pro soubor


    fflush(stdin);                                                      //vyprazdneni bufferu
    system("cls");                                                      // vycisteni obrazovky
    printf("\nZebricek nejlepsich hracu:\n");                                               //tisk hlavicky
    fflush(stdin);                                                      //buffer
    NACTENI = fopen(SOUBOR_HRACU , "r");                                    //ulozeni otevreneho souboru do promenne
    if(NACTENI == NULL) {                                            //jestlize v promenne nic neni
        perror("Soubor nenalezen.");                                    //vypis error
        Sleep(HODNOTA_BIG_SLEEP);                                                   //nech uzivatel aby si to precetl
        exit(1);    //a pak ukonci seanci
    }

    while (fgets(line, sizeof line, NACTENI) != NULL) {                 //pokud radek nacteny ze souboru nebude prazdny
        fflush(stdin);                                                  //buffer
        menic=line;                                                     //anti-decay opatreni promenne line
        separator = " ";
        levaStrana = strtok(menic, separator);
        pravaStrana = strtok(NULL, "");
        //if ((poleNazvu[count][ALOKACE_MALA-1]=(char *)malloc(ALOKACE_PROHAZOVANI*sizeof(char)))==NULL) {
        //    printf("Nedostatek pameti. Ukoncuji.");
        //    exit(1);
        //}
        //alokace1=1;
        strcpy(poleNazvu[count][ALOKACE_MALA-1],levaStrana); //ITS HERE
        poleKreditu[count]=atoi(pravaStrana);
        if (countMax<count) {
            countMax=count;
        }
        count++;
    }
Lance
  • 203
  • 2
  • 15

1 Answers1

1

In the call

strcpy(poleNazvu[count][ALOKACE_MALA-1],levaStrana); //ITS HERE

the destination poleNazvu[count][ALOKACE_MALA-1] is an uninitialized pointer. It's value is indeterminate leading to undefined behavior.

Either allocate memory for the pointer before copying, or use strdup instead (which does allocation and copying in a single call), or make poleNazvu not be an array of array of pointers to char.


There are also some things that doesn't really make any sense, like why are you using poleNazvu[count][ALOKACE_MALA-1] as the destination? It will always copy the string to the same entry in the array arrays.

Perhaps you are supposed to make poleNazvu an array of arrays of char, not an array of arrays of pointers to char? Like

char poleNazvu[ALOKACE_POLI][ALOKACE_MALA];

Then you would just do

strcpy(poleNazvu[count], levaStrana);

Of course this requires the length of the string in levaStrana be less than ALOKACE_MALA characters. So perhaps use strncpy and explicit termination instead, like

strncpy(poleNazvu[count], levaStrana, ALOKACE_MALA - 2);
poleNazvu[count][ALOKACE_MALA - 1] = '\0';
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • When I remove the pointer in decladation it gives me "passing argument 1 of 'strcpy' makes pointer from integer without a cast" on line with strcpy – Lance Jun 15 '16 at 07:47
  • 1
    @Lance Please continue reading my (updated) answer. – Some programmer dude Jun 15 '16 at 07:51
  • The strdup is the solution. I need the two dimensional array because I am sorting the ladder later on and I am switching several values between the variables. Now do I need to free the variable poleNazvu somehow? – Lance Jun 15 '16 at 07:59
  • @Lance Yes, `strdup` calls `mallo` and you need to `free` that memory. – Some programmer dude Jun 15 '16 at 08:05
  • To update this. I used strcpy because strdup is "not damn standard function" When I allocated I used allocation according to strlen(of copied string) all warnings dissappeared and problem was solved. Thank you Joachim, you rock! – Lance Jun 17 '16 at 09:00