字符串数组的分段错误

发布于 2024-10-05 23:26:00 字数 2082 浏览 3 评论 0原文

我遇到了一些字符串数组问题,这些问题似乎侵入了内存的保留空间。代码太大,无法在这里发布,因此我将在下面发布重要部分:

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]; 

之后,它进入一个循环,其中将文件中包含的语句的含义存储在 string [i] 中。 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++;
    }

该错误是偶然的 - 它并不总是发生。它通常发生在调用 sprintf 时。 啊,是的!下面是我正在加载的 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

刚刚看到一些新内容。这是我得到的编译窗口:

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

奇怪的是我得到了字符串数组的一些空白...它与错误有什么关系吗?

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. The beginning of the for and three examples are shown below:

    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.
Ah yes! And below is the txt file I'm loading as an example:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心奴独伤 2024-10-12 23:26:00

您是否在调用 sprintf 之前为每个 temp[i] 分配内存?如果没有,那就是你的问题了。

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

尽管现在您必须跟踪 temp 的哪些元素是通过 malloc 分配的,以便稍后可以释放它们。

编辑

在程序末尾,您可以循环遍历 temp 数组,并根据上面字符串的前导部分检查每个元素的内容,如果它们匹配,使用free释放该元素:

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

您不需要对分配了“NOP”或“RLC”的数组元素执行此操作;在这些情况下,您只需将字符串文字的地址复制到数组元素即可。没有为这些元素分配新的内存,因此您不必担心释放它们。

Are you allocating memory for each temp[i] before calling sprintf? 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.

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:

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"; 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.

一腔孤↑勇 2024-10-12 23:26:00

我立即看到一些问题:

编辑第一点可能不适用于您的环境,但您应该记住它

  • 您正在尝试创建 char *temp[numLinHas] 来自一个整数,其值将在运行时确定。这在 C99 中是允许的,或者可以通过编译器扩展提供,但在较旧的 C 标准中,必须在编译时知道数组大小。您的代码可能确实是这样做的:

    int numLinHas = 0;
    char *temp[numLinHas];
    
  • 另一个问题是,当您执行 sprintf 时,您试图复制到 temp 中的某些内容,而不为指向的指针分配内存存储字符串。

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. 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. 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.

悟红尘 2024-10-12 23:26:00
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;
    }

当遇到以06开头,不以d结尾的助记词时,就会出现这个问题; sprintf() 在未分配的区域中写入,因为 temp[i] 未初始化。您应该分配一些空间来存储 sprintf 的结果。

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; the sprintf() writes in a non-allocated area as temp[i] is not initialized. You shall allocate some space to store the result of sprintf.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文