Search code examples
cstringsegmentation-faultdynamic-memory-allocationstrcpy

Strcpy just crashes the second time I use in the same context


This code waits for a string entered by the user, then the program should sort it immediately in an pointer array.

The problem is in the case cop<0. I don't know what's the problem with strcpy() . If anyone could help, I'll be thankful.

Here is the code :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int lecture(char* t[],int len_t); // reads the strings
void ecriture(char* [],int len_t);  // prints the strings


int main(int argc, char *argv[], char *envp[])
{
    char* cTab[20]={NULL};
    int iNbl;

    iNbl=lecture(cTab,20);  // returns number of strings 
    puts(""); 
    ecriture(cTab,iNbl); // prints the strings 
    return 0;
}

int lecture(char* t[],int len_t)
{
    char cChaine[80]; // place to make the string in
    puts("the String is  : ");int i=0; 
    while(strcmp(gets(cChaine),"end")) // if cChaine==end then it quits
{
    int verif=0; // verification to know if the string took its place or not
    t[i]=(char *)malloc(strlen(cChaine)+1);
    if(i==0)
    {
        strcpy(t[i],cChaine); puts("case i=0"); //puts is just a verification 
    }
    else
    {
        int j=0, cop=0;
        for(j=0;j<i-1;j++)
        {
            cop=strcmp(cChaine,t[j]);

            if(cop>0)
            {
                continue;/* after that I will use "verif" to decide if I add the string in the end or in the beginning */
            }
            if(cop==0)
            {
                puts("Beginning cop=0 !");
                int compteur=0;char* tTmp[20];
                for(compteur=j+1;compteur<i;compteur++)
                {
                     strcpy(tTmp[compteur],t[compteur]); //works here as expected;
                }

                strcpy(t[j+1],cChaine);  //here to o_o it's works as I wanted
                compteur=0;

                for(compteur=j+1;compteur<i;compteur++)
                {
                    strcpy(t[compteur+1],tTmp[compteur]); //works as expected
                }
                puts("End of cop=0");
                verif=1;  //to not add the string in the end of t[] cuz it's already added
            }

            if(cop<0)
            {
                puts("Beginning of case : cop <0 !");
                int compteur=0;char* tTmp[20];

                for(compteur=j;compteur<i;compteur++)
                {
                    strcpy(tTmp[compteur],t[compteur]); //it crashes here I don't know why
                }
                strcpy(t[j],cChaine); // crash .. 
                compteur=0;
                for(compteur=j;compteur<i;compteur++)
                {
                    strcpy(t[compteur+1],tTmp[compteur]); //crash ... 

                }
            }
            verif=1;
            break;
        }
        printf("Fin \n");
    }
    if(verif==0)  //the use of verif is here
    {
        puts("verif =0 :p ");
        strcpy(t[i],cChaine);  // here is the last case of the array
    }
    i++;
}
    return i;
}

void ecriture(char* t[],int len_t)
{
    int i=0;
    puts("Lecture ... : ");
    for(i=0;i<len_t;i++)
    {
        puts(t[i]);
    }
}

Solution

  • The problem here, as I can see, is with

    char* tTmp[20];
    

    you never allocated memory to tTmp[n] before using like

     strcpy(tTmp[compteur],t[compteur]);
    

    using uninitialized memory leads to undefined behaviour.

    Note: Don't be fooled by the strange behavior of UB. your cop == 0 case is equally erroneous as cop < 0 case.

    That said,

    1. Never use gets(), use fgets() instead.
    2. int main(int argc, char *argv[], char *envp[]) is not proper. the recommended signature is int main(int argc, char *argv[])