为什么我初始化结构时,你得到段错误? [英] Why I get segfault when initializing the struct?

查看:118
本文介绍了为什么我初始化结构时,你得到段错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

搜索周围一小时。我想我最好在这里发表的问题。

我简化了code。该段错误是在功能 initMyStruct

 的#includestdlib.h中typedef结构{
        INT * ARR1;
        INT * ARR2;
} MYSTRUCT;无效allocMyStruct(MYSTRUCT *一,INT NUM){
        A =的malloc(sizeof的(MYSTRUCT));
        A-> ARR1 =的malloc(10 * sizeof的(INT));
        A-> ARR2 =的malloc(10 * NUM *的sizeof(INT));
}
无效initMyStruct(MYSTRUCT *一,INT NUM){
        INT I;
        对于(I = 0; I&小于10;我++)A-> ARR1 [I] = 0;
        对于(I = 0; I&小于10 *民;我++)A-> ARR2 [I] = -1;
}
无效freeMyStruct(MYSTRUCT *一,INT NUM){
        INT I;
        对于(I = 0; I&小于10;我++)自由(A-> ARR1);
        对于(I = 0; I&小于10 *民;我++)自由(A-> ARR2);
        自由(一);
}
诠释主要(无效){
        INT NUM = 3;
        MYSTRUCT *一个;
        allocMyStruct(一,NUM);
        initMyStruct(一,NUM);
        freeMyStruct(一,NUM);
        返回1;
}


解决方案

由于你没有保持指向新分配的内存,而不是你使用未初始化的指针,并得到不确定的行为。

您传递 A 可变进 allocMyStruct(),但通话(所有其他人一样)的通过值的,因此新的值被赋予给它的函数内部没有的的影响值 A 的main()

更改它,以便 allocMyStruct()或者返回的新指针值,或者需要一个指针的指针。我想preFER前,它的清洁,使用函数返回值往往会导致更好的code:

  MYSTRUCT * allocMyStruct(INT NUM)
{
  MYSTRUCT * P;  如果((P =的malloc(sizeof的* P +
                 10 * sizeof的* P-> ARR1 +
                 10 * NUM * sizeof的* P->!ARR2))= NULL)
  {
    对 - > ARR1 =为(int *)(P + 1);
    P-> ARR2 = P-> ARR1 + 10;
  }
  回磷;
}

以上code也简化了内存分配,做这一切在一个大的的malloc()通话,然后切成三个部分,你实际上需要的。

如果大小 ARR1 是由路总是10,有一个在拥有它动态分配没有意义,应该仅仅是 INT ARR1 [ 10]; 于结构声明

Searched around for one hour. I guess I'd better post the question here.

I simplify the code. The segfault is in the function initMyStruct.

#include "stdlib.h"

typedef struct {
        int * arr1;
        int * arr2;
} myStruct;

void allocMyStruct (myStruct * a, int num) {
        a = malloc(sizeof(myStruct));
        a->arr1 = malloc(10*sizeof(int));
        a->arr2 = malloc(10*num*sizeof(int));
}
void initMyStruct (myStruct * a, int num) {
        int i;
        for (i = 0; i < 10; i++)     a->arr1[i]  =  0;
        for (i = 0; i < 10*num; i++) a->arr2[i]  = -1;
}
void freeMyStruct (myStruct * a, int num) {
        int i;
        for (i = 0; i < 10; i++)     free(a->arr1);
        for (i = 0; i < 10*num; i++) free(a->arr2);
        free(a);
}
int main (void) {
        int num = 3;
        myStruct * a;
        allocMyStruct (a, num);
        initMyStruct  (a, num);
        freeMyStruct  (a, num);
        return 1;
}

解决方案

Because you're not keeping the pointer to the newly allocated memory, instead you use an uninitialized pointer and getting undefined behavior.

You pass the a variable into allocMyStruct(), but that call is (like all others) by value, so the new value being assigned to it inside the function does not affect the value of a in main().

Change it so that allocMyStruct() either returns the new pointer value, or takes a pointer to the pointer. I would prefer the former, it's cleaner and using function return values often leads to better code:

myStruct * allocMyStruct(int num)
{
  myStruct *p;

  if((p = malloc(sizeof *p +
                 10 * sizeof *p->arr1 +
                 10 * num * sizeof *p->arr2)) != NULL)
  {
    p->arr1 = (int *) (p + 1);
    p->arr2 = p->arr1 + 10;
  }
  return p;
}

The above code also streamlines the memory allocation, doing it all in one big malloc() call which is then "sliced" into the three parts you actually need.

If the size of arr1 is always 10 by the way, there's no point in having it dynamically allocated, it should just be int arr1[10]; in the struct declaration.

这篇关于为什么我初始化结构时,你得到段错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆