简体   繁体   English

连接两个字符串的分段错误

[英]Segmentation fault with concatenation of two strings

I'm trying to build alphanumeric strings consisting of 3 initial letters and 3 final numbers and save them in a .txt file.我正在尝试构建由 3 个首字母和 3 个最终数字组成的字母数字字符串,并将它们保存在.txt文件中。 I wrote this:我写了这个:

int i = 0,          
    j = 0;     
char name_cpu[8],
     array_numbers_final[8],    
     array_letters[27] = "ABCDEFGHIJKLMNOPQRSTUWXYVZ",
     array_numbers[10] = "123456789";


/* Generator data */
  for(i = 0; i < number_cpu; i++)
  { 
      for(j = 0; j < 3; j++){
      name_cpu[j] = array_letters[rand() % (sizeof(array_letters)-1)];
      array_numbers_final[j] = array_numbers[rand() % (sizeof(array_numbers)-1)];
    }

    strcat(name_cpu, array_numbers_final);
    fprintf(list_cpu, "%s \n", name_cpu);
  }

The problem is that at the first external for loop it correctly prints a string of the form "AAA000".问题在于,在第一个外部for循环中,它正确打印了一个“AAA000”形式的字符串。 At the second for loop it goes in segmentation fault.在第二个 for 循环中,它进入分段错误。 Can anyone tell me what and where I am doing wrong?谁能告诉我我做错了什么以及在哪里做错了?

EDIT: A minimal reproducible example is:编辑:一个最小的可重现示例是:

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

int main (void){

  FILE *list_cpu = NULL;
  int i = 0,
  number_cpu = 3,    
  j = 0;     
  char name_cpu[8] = {0},
  array_numbers_final[8] = {0},    
  array_letters[27] = "ABCDEFGHIJKLMNOPQRSTUWXYVZ",
  array_numbers[10] = "123456789";
        
  list_cpu = fopen("list_cpu.txt", "w");  
        
        
  /* Generator data */
  for(i = 0; i < number_cpu; i++)
  { 
    for(j = 0; j < 3; j++){
      name_cpu[j] = array_letters[rand() % (sizeof(array_letters)-1)];
      array_numbers_final[j] = array_numbers[rand() % (sizeof(array_numbers)-1)];
  }
   strcat(name_cpu, array_numbers_final);
   fprintf(list_cpu, "%s \n", name_cpu);
  }
  fclose(list_cpu);

return(0);
}

If number_cpu is equal to 1 it works.如果number_cpu等于 1 它可以工作。 But if it is higher then 1, the program goes into segmentation fault.但如果它高于 1,则程序进入分段错误。

answer to new edits to OP回答对 OP 的新编辑

Edits to OP include initialization of variables addressing some instances of undefined behavior in original code, however buffer overflow problem remains in following section:对 OP 的编辑包括初始化变量,以解决原始代码中未定义行为的一些实例,但缓冲区溢出问题仍然存在于以下部分:

/* Generator data */
  for(i = 0; i < number_cpu; i++)
  { 
    for(j = 0; j < 3; j++){
      name_cpu[j] = array_letters[rand() % (sizeof(array_letters)-1)];
      array_numbers_final[j] = array_numbers[rand() % (sizeof(array_numbers)-1)];
  }
   strcat(name_cpu, array_numbers_final);
   fprintf(list_cpu, "%s \n", name_cpu);
  }
  fclose(list_cpu);

Where name_cpu is defined with 8 char: char name_cpu[8] = {0}其中name_cpu用 8 个字符定义: char name_cpu[8] = {0}

"If number_cpu is equal to 1 it works. But if it is higher then 1, the program goes into segmentation fault."... “如果 number_cpu 等于 1 它可以工作。但如果它高于 1,则程序进入分段错误。”...

After first iteration of outer loop, including one strcat of name_cpu and list_cpu , the char name_cpu is populated with 6 new characters and \0 in remaining locations.在外循环的第一次迭代之后,包括一个strcat name_cpulist_cpuchar name_cpu填充有 6 个新字符和\0在剩余位置。 Eg:例如:

//example
|S|J|P|Y|E|P|\0|\0|  //

The error occurs at end of 2nd iteration of outer loop at the strcat() statement.该错误发生在strcat()语句的外部循环的第二次迭代结束时。 The inner loop re-populated the first three positions of name_cpu , eg:内部循环重新填充了name_cpu的前三个位置,例如:

//first three positions changed, the rest remains
|C|B|A|Y|E|P|\0|\0|  // 
 ^ ^ ^ updated values

Error occurs when the new value for array_numbers_final :array_numbers_final的新值发生错误:

|G|H|O|\0|\0|\0|\0|\0|

is attempted to be concatenated to name_cpu , resulting in:试图连接到name_cpu ,导致:

|C|B|A|Y|E|P|G|H|O|  //attempt to write to memory not owned
                ^end of buffer

Resulting in buffer overflow error, and likely the segmentation fault error condition.导致缓冲区溢出错误,并可能出现分段错误错误情况。 Again, as before, design variables to meet the potential needs of of the program.同样,和以前一样,设计变量以满足程序的潜在需求。 In this instance the following will work:在这种情况下,以下将起作用:

 char name_cpu[100] = {0};
 char array_numbers_final[100] = {0};     

answer to original post :回复原帖
The problem is here:问题在这里:

strcat(name_cpu, array_numbers_final);//undefined behavior.

First, name_cpu[8] is uninitialized at time of declaration, nor is it initialized at any time before used.首先, name_cpu[8]在声明时是未初始化的,也不是在使用前的任何时候初始化的。 Because it is not guaranteed what the contents are, it is not necessarily a string.因为不能保证内容是什么,所以不一定是字符串。 Using this variable as is in any string function can invoke undefined behavior在任何字符串中使用此变量 function 可以调用未定义的行为

Furthermore,(even if name_cpu is a valid string) when array_numbers_final is concatenated to name_cpu , the buffer will overflow.此外,(即使name_cpu是有效字符串)当array_numbers_final连接到name_cpu时,缓冲区将溢出。 During my test run I saw this:在我的测试运行中,我看到了这个:

在此处输入图像描述

The fix is to start off with initialized buffers of sufficient size for intended purpose.解决方法是从用于预期目的的足够大小的初始化缓冲区开始。 eg:例如:

 char name_cpu[100] = {0}
 array_numbers_final[100] = {0},    
 array_letters[27] = "ABCDEFGHIJKLMNOPQRSTUWXYVZ",
 array_numbers[10] = "123456789";

Code example below is adaptation using your provided example, with suggested edits.下面的代码示例是使用您提供的示例进行改编,并带有建议的编辑。 Read in-line comments:阅读在线评论:

int main(void)
{
    int number_cpu = 3;//added - previously not defined in OP
    int i = 0,          
    j = 0;     
    char name_cpu[100] = {0},//resized and initialize
     array_numbers_final[100] = {0},//resized and initialize    
     array_letters[27] = "ABCDEFGHIJKLMNOPQRSTUWXYVZ",
     array_numbers[10] = "123456789";


/* Generator data */
      for(i = 0; i < number_cpu; i++)
      { 
          for(j = 0; j < 3; j++){
          name_cpu[j] = array_letters[rand() % (sizeof(array_letters)-1)];
          array_numbers_final[j] = array_numbers[rand() % (sizeof(array_numbers)-1)];
        }

        strcat(name_cpu, array_numbers_final);
        fprintf(stdout, "%s \n", name_cpu);//to stdout
        //        ^ changed here
      }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM