【问题标题】:C program working on debug mode and not working on release modeC 程序在调试模式下工作而不在发布模式下工作
【发布时间】:2018-06-07 21:23:43
【问题描述】:

我正在编写一个程序,该程序应处理存储在文件中的一些数据,当我在代码块内以调试模式运行它时它运行完美,但每当我尝试使其以不同方式运行时崩溃(通过单击 exe 或尝试运行发布模式)我尝试了一切,我认为不应该有越界访问或未初始化的指针这只是代码的一个抽象,因为它在函数 readCSV 上停止,csv 文件包含 53 行并且计数行是能够在发布时正确计算它们,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);

}

【问题讨论】:

  • 标题在尖叫未定义的行为!!! 无论如何,这段代码是不完整的。而且格式很差。
  • 是的,我知道代码不完整,但代码的其他部分没有执行,因为它在 readCSV 上停止,我也知道有一些未定义的行为,但我知道是什么原因造成的以及如何修复它
  • for (i=0;(!feof(file));i++) 请参阅“为什么while(!feof(fp)) 总是错误的”。 stackoverflow.com/questions/5431941/…
  • 认真的吗?我们怎么知道MAX 是什么?
  • @ChristianGibbons 错误地指出 while ( !feof() ) 的使用是错误的几乎与使用错误使用 while ( !feof() ) 的代码发布问题一样常见。在这种情况下,if ( fgets( ... ) != NULL ) 会生成正常工作的代码。

标签: c pointers debugging crash release


【解决方案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) 在定义上始终为 1,并且您不必在 C 中强制转换 void *

另外,see strdup()

【讨论】:

  • 非常感谢您的回答!我不敢相信我以前没有注意到,我认为你救了我的大学生活!
猜你喜欢
  • 2018-07-31
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
相关资源
最近更新 更多