C程序在调试模式下工作而不在发布模式下工作

时间:2018-06-07 21:23:44

标签: c pointers debugging crash release

我正在编写一个应该处理存储在文件中的一些数据的程序,当我在代码块内部以调试模式运行它时它运行得很好但是每当我尝试使它以不同的方式运行时崩溃(通过点击exe或尝试运行发布模式)我尝试了每次操作,我认为不应该是超出访问范围或未初始化的指针这只是代码中的摘要,因为它在函数readCSV上停止,csv文件包含53行和print行也可以在发布时正确计算它们,printfs很难理解它在哪里停止执行我希望有一些我无法发现的错误,提前感谢帮助

typedef struct
{
 char* codice;
 char* nome;
 char* album;
 char* artista;
 int track;
 Durata durata;
 Genere genere;
}Brano;






   int countRowsCSV(const char* filename)
   {    FILE* file=fopen(filename,"r");
        char line[200];
        int i=0;
        if(file==NULL)
        return -1;
        while(!feof(file))
        {if(fgets(line,sizeof(line),file)!=NULL)
          i++;
        }
        fclose(file);
        return i;
   }



  char* getString(char* line)
 {
   printf("%s",line);
   char* a=NULL;
   a=(char*)malloc(sizeof(char)*strlen(line));
   strcpy(a,line);
   return a;
 }

 void readCSV(Brano* catalogo)
{
FILE* file=fopen("brani.csv","r");
char line[200]="";

int i;
if(file==NULL)
return ;

for (i=0;(!feof(file));i++)
{
    if(fgets(line,sizeof(line),file)!=NULL)
    {
        if(line[strlen(line)-1]!='\0')//I remove the final \n
        line[strlen(line)-1]='\0';
        printf("%s",line);
        catalogo[i].codice=NULL;
        catalogo[i].codice=getString(strtok(line,";"));
        printf("%s\n",catalogo[i].codice);
        catalogo[i].nome=NULL;
        catalogo[i].nome=getString(strtok(NULL,";"));
        printf("%s\n",catalogo[i].nome);
        catalogo[i].album=NULL;
        catalogo[i].album=getString(strtok(NULL,";")); //the program often crashes here with i=0
        printf("%s\n",catalogo[i].album);
        catalogo[i].artista=NULL;
        catalogo[i].artista=getString(strtok(NULL,";"));
        printf("%s\n",catalogo[i].artista);
        catalogo[i].track=atoi(strtok(NULL,";"));
        printf("%d\n",catalogo[i].track);
        catalogo[i].durata.minuti=atoi(strtok(NULL,";"));
        catalogo[i].durata.secondi=atoi(strtok(NULL,";"));
        catalogo[i].genere=stringToGenere(strtok(NULL,";"));
    }
}
fclose(file);

}

int main(void) {
int scelta=0,num_utenti=0,size_catalogo=countRows("brani.csv");
int size_publicP=0;

if(size_catalogo<0)
{ MessageBox(NULL,"Failed to open \"brani.csv\" ","ERROR MESSAGE",MB_ICONERROR|MB_ICONSTOP);
  return -1;
}
Brano* catalogo=NULL;
catalogo=(Brano*)malloc(sizeof(Brano)*size_catalogo);
Brano** pCatalogo=NULL;

Playlist** publicPlaylists=NULL;
Utente* loggedUser=NULL;
pCatalogo=(Brano**)malloc(sizeof(Brano*)*size_catalogo);
Utente* utenti=NULL;
readCSV(catalogo);

}

1 个答案:

答案 0 :(得分:1)

您的getstring()错了。它没有考虑终止NUL字符。

  char* getString(char* line)
 {
   printf("%s",line);
   char* a=NULL;
   a=(char*)malloc(sizeof(char)*strlen(line));
   strcpy(a,line);
   return a;
 }

应该是

 char *getString(char *line)
 {
     printf("%s",line);
     char* a=malloc(1 + strlen(line));
     strcpy(a,line);
     return a;
 }

请注意1 + strlen()来说明终止NUL字符。

另请注意,根据定义,sizeof(char)始终是一个,并且您不必在C中投射void *

另外,see strdup()

相关问题