【问题标题】:Why segmentaion fault in C program?为什么C程序中出现分段错误?
【发布时间】:2016-09-14 14:29:48
【问题描述】:
#include<stdio.h>
#include<stdlib.h>

struct keyVal
{
    int key;
    int val;
};

int main()
{

    struct keyVal *arr[5];
    int i;
    for(i=0;i<5;i++)
        printf("\n : %p     %p      ",&arr[i][0].val,&arr[i]);
    printf("\n\n");
    printf("\n : %d     %d      ",arr[0][0].val,arr[0]->val);
    printf("\n\n");

    for(i=0;i<5;i++)
        printf("\n : %d     %d      ",arr[i][0].val,arr[i]->val);
    printf("\n\n");

    return 0;
}

首先,for( ; ; ); 会从arr[0][0]arr[4][0]arr[0]arr[4] 生成相同的%p %p,这意味着arr[i][0] == arr[i] 其中i = 0,1,2,3,4

第二个for( ; ; ); 应该打印arr[i][0].key (arr[i]-&gt;key) 的值(垃圾值)。

我们可以通过以下方式访问密钥:

arr[i][0].keyarr[i]-&gt;key 其中i = 0,1,2,3,4

【问题讨论】:

  • 编译器警告是什么?
  • arr没有初始化,所以整个代码是UB
  • 你能重新格式化你的问题吗?如图所示,代码非常不清楚。
  • @SouravGhosh 运行时错误是:分段错误(核心转储)
  • 提示:将newline 放在格式字符串的结尾,而不是开头。然后,当您崩溃时,不会缓冲未打印的消息,从而使您对程序到达的位置感到困惑。如果在另一个语句中要打印更多内容,例如循环中的数组值,则应该只省略最后的换行符。然后,循环后printf("\n");

标签: c struct segmentation-fault


【解决方案1】:

您使用了 5 个指针变量指向未为其分配内存的 keyVal 结构,这就是它遇到分段错误的原因。使用malloc分配内存然后使用。

【讨论】:

    【解决方案2】:

    您可以使用 &arr[i].val 而不是 &arr[i][0].val 。

    【讨论】:

      【解决方案3】:

      请参阅 cmets 以获得解释。
      我故意留下最后一个 for {...} 我希望您能够自己更正它

      #include<stdio.h> 
      #include<stdlib.h>
      
      struct keyVal {
          int key;
          int val;
      };
      
      int main()
      {
          /*
            You are creating an array composed of 5 cells.
            Each cell is bound to contain an address of a variable
            of which should be a keyVal.
          */
          struct keyVal *arr[5];
          int i;
      
          /* 
           Use curly brackets to wrap the content of the loop
           even though you only have one instruction it helps for readability
           and prevents errors.
          */
          for(i = 0; i < 5; i++) {
              printf(": %p | %p\n", &arr[i][0].val, &arr[i]);
          }
      
          /*
           You are getting a segmentation fault here because you are trying to access something which does not exist.
          */
          //printf(": %d - %d\n", arr[0][0].val, arr[0]->val);
      
          /***************************************************
           BEGIN CORRECTION :the field val will be initialized.
          ***************************************************/
      
          arr[0] = malloc(sizeof(struct keyVal)); // Allocating "sizeof(struct keyVal)" bytes in memory.
          arr[0]->val = 42; // Assigning a value to the field val in the first cell of arr. 
          printf(": %d | %d\n", arr[0][0].val, arr[0]->val); // Now we can print as a value now exists.
      
          /***************************************************
           END CORRECTION
          ***************************************************/
          /*
           Again use brackets ;p
           I think you can solve that one on your own, now.
          */  
          for(i = 0; i < 5; i++) {
              printf(": %d | %d \n", arr[i][0].val, arr[i]->val);
          }
          // Don't forget to free "ALL" the memory allocated with malloc.
          free(arr[0]);
      
          return 0;
      }
      

      了解malloc and free

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-17
        • 2014-11-29
        • 2020-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多