简体   繁体   English

带字符串数组的分段错误

[英]Segmentation Fault with string array

I'm experiencing some problems with an array of strings that seem to be invading a reserved space of memory. 我遇到了一些字符串数组问题,这些字符串似乎正在侵犯内存的保留空间。 The code is too large to post here, so I'll post the important part below: 代码太大,无法在此处发布,因此我将在下面发布重要部分:

int main (  ){

 int i = 0, j = 0, k = 0, count = 0, numLinhas = 0, l = 0;
 char string[100][100];
 char line [17];
 char str[4];
 char str1[5];
 char str2[4];
 char str3[4];

 FILE *p;
 p = fopen("text.txt", "r");
 while(fgets(line, sizeof line, p)!=NULL){
  printf("%s", line);
  strncpy(string[i], line, 17);
  i++;
  numLinhas++; 
  }  
  fclose(p);
  char *temp[numLinhas]; 

After that it goes into a loop in which stores in string [i] the meaning of the statement contained in the file. 此后,它进入一个循环,在该循环中,文件中包含的语句的含义存储在string [i]中。 The beginning of the for and three examples are shown below: for和三个示例的开头如下所示:

    for (i = 0; i<numLinhas; i++){
    sscanf( string[i], "%s %s %s %s" ,str1, str,str2, str3);
    if(str[0]=='0' && str[1] == '0' && str[2]!= 'd') {
    temp[i] = "NOP";
    count++;
    }
    if(str[0]=='0'&& str[1] == '6' && str[2]!= 'd') {
    sprintf(temp[i],"%s,%s" , "MVI B", str2);
    count = count+2;
    }

    if(str[0]=='0'&& str[1] == '7' && str[2]!= 'd') {
    temp[i] = "RLC";
    count++;
    }

The error is casual - it does not always happen. 该错误是偶然的-并非总是会发生。 And it usually occurs when there is a call to sprintf. 它通常在调用sprintf时发生。 Ah yes! 是啊! And below is the txt file I'm loading as an example: 下面是我正在加载的txt文件作为示例:

0000 21a 11r 00r
0003 7Ea
0004 21a 12r 00r
0007 46a
0008 80a
0009 21a 13r 00r
000C 77a
000D 3Ea 01a
000F 3Da
0010 76a
0011 0Ad
0012 03d
0013 01d

Just saw something new. 刚看到一些新东西。 here's the compile window i'm getting: 这是我得到的编译窗口:

marcos@john:~/Desktop$ ./paraler
0000 21a 11r 00rValor de l: 16

Valor de l: 1
0003 7Ea
Valor de l: 9
0004 21a 12r 00rValor de l: 16

Valor de l: 1
0007 46a
Valor de l: 9
0008 80a
Valor de l: 9
0009 21a 13r 00rValor de l: 16

Valor de l: 1
000C 77a
Valor de l: 9
000D 3Ea 01a
Valor de l: 13
000F 3Da
Valor de l: 9
0010 76a
Valor de l: 9
0011 0Ad
Valor de l: 9
0012 03d
Valor de l: 9
0013 01d
Valor de l: 9
string:0000 21a 11r 00r
string:

string:0003 7Ea

string:0004 21a 12r 00r
string:

string:0007 46a

string:0008 80a

string:0009 21a 13r 00r
string:

string:000C 77a

string:000D 3Ea 01a

string:000F 3Da

string:0010 76a

string:0011 0Ad

string:0012 03d

string:0013 01d

Segmentation fault

The strange thing is that I get some empty spaces of the string array... Does it have anything to do with the error? 奇怪的是,我得到了字符串数组的一些空白...与错误有关吗?

Are you allocating memory for each temp[i] before calling sprintf ? 您是否在调用sprintf之前为每个temp[i]分配了内存? If not, there's your problem. 如果没有,那是你的问题。

if (!strncmp(str, "06", 2) && str[2] != 'd')
{
  temp[i] = malloc(5 + strlen(str2) + 2);  // Thanks, philippe
  if (temp[i])
    sprintf(temp[i], "%s,%s", "MVI B", str2);
}

Although now you'll have to keep track of which elements of temp were allocated with malloc so you can free them later. 尽管现在您必须跟踪使用malloc分配了哪些temp元素,以便以后可以释放它们。

Edit 编辑

At the end of your program, you can loop through the temp array and check the contents of each element against the leading part of your string above, and if they match, deallocate that element using free : 在程序结束时,您可以循环遍历temp数组,并对照上述字符串的开头部分检查每个元素的内容,如果它们匹配,请使用free释放该元素:

for (i = 0; i < numLinhas; i++)
{
  if (strcnmp(temp[i], "MVI B", strlen("MVI B")) == 0)
    free(temp[i]);
}

You do not need to do this for the array elements that were assigned "NOP" or "RLC"; 您无需为分配了“ NOP”或“ RLC”的数组元素执行此操作; in those cases, you simply copied the address of the string literal to the array element. 在这种情况下,您只需将字符串文字的地址复制到数组元素。 No new memory was allocated for those elements, so you don't have to worry about deallocating them. 没有为这些元素分配新的内存,因此您不必担心取消分配它们。

There are some issues I see immediately: 我立即看到了一些问题:

edit the first point might not apply to your environment, but you should keep it in mind 编辑第一点可能不适用于您的环境,但请记住这一点

  • You are trying to create char *temp[numLinHas] from an integer whose value will be determined at run-time. 您试图从一个整数创建char *temp[numLinHas] ,该整数的值将在运行时确定。 This is permissible in C99, or may be provided via a compiler extension, but in older C standards, array sizes must be known at compile-time. 这在C99中是允许的,也可以通过编译器扩展提供,但是在较早的C标准中,必须在编译时知道数组大小。 Your code might really be doing this: 您的代码可能确实是这样做的:

     int numLinHas = 0; char *temp[numLinHas]; 
  • Another problem is that when you do sprintf , you are attempting to copy to something in temp without allocating memory for the pointer to store the string. 另一个问题是,当您执行sprintf ,您试图复制到temp而没有为存储字符串的指针分配内存。

if(str[0]=='0'&& str[1] == '6' && str[2]!= 'd') {
    /* allocate memory to store instruction - to be freed later */  
    temp[i] = malloc(5 + strlen(str2) + 2); /* +2 for the comma */
    sprintf(temp[i],"%s,%s" , "MVI B", str2);
    count = count+2;
    }

The problem arises when a mnemonic starting with 06 and no ending with d is encountered; 当遇到以06开头但不以d结尾的助记符时,就会出现问题。 the sprintf() writes in a non-allocated area as temp[i] is not initialized . sprintf()在temp [i] 未初始化的情况下写入未分配的区域。 You shall allocate some space to store the result of sprintf. 您应该分配一些空间来存储sprintf的结果。

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

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