动态分配的结构数组中的SegFault,C

动态分配的结构数组中的SegFault,C,c,struct,segmentation-fault,dynamic-allocation,C,Struct,Segmentation Fault,Dynamic Allocation,这是从parser.c源文件调用的函数 typedef struct { double x; double y; long out_x; long out_y; } coords; typedef struct { char name[FIGURE_LEN + 1]; int coordcount, size_tracker; coords *coordinate; } fig; fig figure; fig * figpoint; 我第一次打电话给creat

这是从parser.c源文件调用的函数

typedef struct {
  double x;
  double y;
  long out_x;
  long out_y;
} coords;

typedef struct {
  char name[FIGURE_LEN + 1];
  int coordcount, size_tracker;
  coords *coordinate;
} fig;

fig figure;
fig * figpoint;
我第一次打电话给create_Figaray就像这样

void initialize_fig(int n, char * name, double x, double y,
                         fig *fig_point)
{
  printf("inside function\n");
  strncpy(fig_point[n].name, name, FIGURE_LEN + 1);
  fig_point[n].size_tracker = 1;
  fig_point[n].coordinate = malloc(sizeof(coords) * fig_point[n].size_tracker);
  assert(fig_point[n].coordinate != NULL);
  fig_point[n].coordcount = 0;
  fig_point[n].coordinate[fig_point[n].coordcount].x = x;
  fig_point[n].coordinate[fig_point[n].coordcount].y = y;
}

void create_FigArray(fig * fig_point, int fig_size)
{
  fig_point = malloc(sizeof(fig) * fig_size);
  assert(fig_point != NULL);
  fig_point = &figure
}
我这里没有seg故障。。。 但后来我打电话给

create_FigArray(fig_point, 16);
这些参数实际上是通过变量传递的,但我只是想说明它们是正确的参数,并给出传递值的示例。 不管怎样,它击中了

initialize_fig(0, Qe, 10.0000, 10.0000, fig_point);
然后停下来。。这里一定有断层,但为什么


请提供帮助,解释并展示如何解决这个问题。谢谢。

分配内存,然后更改指针

strncpy(fig_point[n].name, name, FIGURE_LEN + 1);
因此,您的
fig_点
指针不再指向动态分配的内存。如果你这样做的话

fig_point = malloc(sizeof(fig) * fig_size); // allocate here
assert(fig_point != NULL);
fig_point = &figure;  // now you make fig_point point to something else
由于
不是数组,因此您正在访问内存out或range。此外,您还可以将指针
fig_point
直接传递到
create_figaray
。这将创建指针的副本,因此对该参数所做的任何更改实际上都只是对
副本的更改。这意味着,在
create\u figaray
之后,存储在
figu数组中的地址返回的动态内存与之前相同-它只是被函数更改的
copy
。如果要更改指针,需要使用函数的双指针参数,然后使用类似

fig_point[n]

分配内存,然后更改指针

strncpy(fig_point[n].name, name, FIGURE_LEN + 1);
因此,您的
fig_点
指针不再指向动态分配的内存。如果你这样做的话

fig_point = malloc(sizeof(fig) * fig_size); // allocate here
assert(fig_point != NULL);
fig_point = &figure;  // now you make fig_point point to something else
由于
不是数组,因此您正在访问内存out或range。此外,您还可以将指针
fig_point
直接传递到
create_figaray
。这将创建指针的副本,因此对该参数所做的任何更改实际上都只是对
副本的更改。这意味着,在
create\u figaray
之后,存储在
figu数组中的地址返回的动态内存与之前相同-它只是被函数更改的
copy
。如果要更改指针,需要使用函数的双指针参数,然后使用类似

fig_point[n]
我不知道,但是:

首先,将内存分配给fig_point:

void create_FigArray(fig** fig_point, int fig_size)
{
    *fig_point = malloc(sizeof(fig) * fig_size);
}
然后你给它分配了数字的地址,你不应该这样做

fig_point = malloc(sizeof(fig) * fig_size);
您可以这样做:

fig_point = &figure;
我不知道,但是:

首先,将内存分配给fig_point:

void create_FigArray(fig** fig_point, int fig_size)
{
    *fig_point = malloc(sizeof(fig) * fig_size);
}
然后你给它分配了数字的地址,你不应该这样做

fig_point = malloc(sizeof(fig) * fig_size);
您可以这样做:

fig_point = &figure;
  • create_FigArray
    中,执行以下操作:fig_point=malloc(sizeof(fig)*fig_size);然后这个:
    fig_point=&figure//figure是一个全局变量,这会导致内存泄漏

  • 而是写下:

  • 在调用函数的地方,确保释放分配的内存

  • 使用前将所有指针设为NULL,并在处理指针的函数中添加NULL参数检查,使用前检查NULL指针

      • create_FigArray
        中,执行以下操作:fig_point=malloc(sizeof(fig)*fig_size);然后这个:
        fig_point=&figure//figure是一个全局变量,这会导致内存泄漏

      • 而是写下:

      • 在调用函数的地方,确保释放分配的内存

      • 使用前将所有指针设为NULL,并在处理指针的函数中添加NULL参数检查,使用前检查NULL指针


      您无法将地物的地址分配给您分配的fig\u点。@askmish这就是我取消引用fig\u点的原因。。。您不能将地物的地址分配给您分配的fig_点。@askmish这就是我取消引用fig_点的原因@用户1787262否不要编辑问题!!这让任何试图回答你的问题的人都很困惑。所以,假设我改变了它,使函数接受了fig**fig_点。我是否需要将所有“.”运算符更改为“->”@user1787262否不要编辑问题!!这让任何试图回答你的问题的人都很困惑。所以,假设我改变了它,使函数接受了fig**fig_点。我是否需要将所有“.”运算符更改为“->”。请在得到答案后不要编辑问题。这会让人很困惑。请不要在得到答案后编辑问题。这让人很困惑。