Strcpy在我第二次使用相同的上下文时崩溃了

时间:2015-05-19 15:18:11

标签: c string segmentation-fault dynamic-memory-allocation strcpy

此代码等待用户输入的字符串,然后程序应立即在指针数组中对其进行排序。

问题在于cop<0。我不知道strcpy()有什么问题。如果有人能提供帮助,我会感激不尽。

以下是代码:

#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]);
    }
}

1 个答案:

答案 0 :(得分:2)

我可以看到,问题在于

char* tTmp[20];

在使用

之前,你从未向tTmp[n]分配内存
 strcpy(tTmp[compteur],t[compteur]);

使用未初始化的内存会导致undefined behaviour

注意:不要被UB的奇怪行为所迷惑。您的cop == 0案例与cop < 0案件同样错误。

那就是说,

  1. 切勿使用gets(),而是使用fgets()
  2. int main(int argc, char *argv[], char *envp[])不合适。推荐的签名是int main(int argc, char *argv[])