为嵌套结构指针分配内存
Allocating memory for nested structure pointer
我正在使用 C 代码生成器创建具有以下结构的头文件:
typdef struct Place {
struct Forest {
int trees;
} *Forest;
} Place ;
并在 C++ 项目中使用它们。
当我尝试访问 Place.Forest->trees 时,出现段错误,因为 Place.Forest 是一个悬空指针。
我无法正确分配它,因为
Place.Forest = malloc(sizeof(Place.Forest));
将只是 return 指针的大小。
我不会用
Place.Forest=malloc(sizeof(struct Forest));
因为我正在从 C++ 访问 Place,并且范围限制使我无法看到 Forest。
如何在不更改 Place 或取消嵌套 Forest 的情况下为 Forest 分配内存?
由于自动生成的代码量很大,修改结构是不切实际的。
Place.Forest = malloc(sizeof(Place.Forest));
应该是
Place.Forest = malloc(sizeof(struct Forest));
因为如您所见,Forest
是指向您的结构的指针,而 sizeof(pointer)
不是您要查找的内容,您想要的是 sizeof(struct Forest)
要为 Forest
分配内存,请这样做。
Place.Forest=malloc(sizeof(struct Forest));
它将根据该结构的大小分配内存。
在 C 中,嵌套的 struct
在整个程序中都是可见的,因此嵌套它们没有意义。只需单独定义它们(并使用 typedef
s 这样你就不必每次都写 struct x
:
typedef struct {
int trees;
} Forest;
typedef struct {
Forest *forest;
} Place;
现在你可以写
malloc(sizeof(Forest));
折腾了几个小时后,我找到了解决办法。
您必须使用 extern C
让编译器使用 C 样式链接,但您还必须使用 C++ 的作用域解析 ::
才能正确解析结构类型。
头文件:
#ifdef __cplusplus
extern "C" {
#endif
typdef struct Place {
struct Forest {
int trees;
} *Forest;
} Place ;
#ifdef __cplusplus
}
#endif
节目:
#include <stdlib.h>
#include <iostream>
extern "C" {
static void allocateForest(Place *p){
p->Forest = (struct Place::Forest *)malloc(sizeof(struct Place::Forest));
}
}
int main(void){
Place p;
allocateForest(&p);
p.Forest->trees = 1;
std::cout << p.Forest->trees << std::endl;
return 0;
}
你应该给指针分配内存,否则它们就是NULL。使用这个:
Place.Forest = (struct Forest*) malloc(sizeof(struct Forest));
另一件事:不要将名称命名为 typedefs。
我正在使用 C 代码生成器创建具有以下结构的头文件:
typdef struct Place {
struct Forest {
int trees;
} *Forest;
} Place ;
并在 C++ 项目中使用它们。
当我尝试访问 Place.Forest->trees 时,出现段错误,因为 Place.Forest 是一个悬空指针。
我无法正确分配它,因为
Place.Forest = malloc(sizeof(Place.Forest));
将只是 return 指针的大小。
我不会用
Place.Forest=malloc(sizeof(struct Forest));
因为我正在从 C++ 访问 Place,并且范围限制使我无法看到 Forest。
如何在不更改 Place 或取消嵌套 Forest 的情况下为 Forest 分配内存?
由于自动生成的代码量很大,修改结构是不切实际的。
Place.Forest = malloc(sizeof(Place.Forest));
应该是
Place.Forest = malloc(sizeof(struct Forest));
因为如您所见,Forest
是指向您的结构的指针,而 sizeof(pointer)
不是您要查找的内容,您想要的是 sizeof(struct Forest)
要为 Forest
分配内存,请这样做。
Place.Forest=malloc(sizeof(struct Forest));
它将根据该结构的大小分配内存。
在 C 中,嵌套的 struct
在整个程序中都是可见的,因此嵌套它们没有意义。只需单独定义它们(并使用 typedef
s 这样你就不必每次都写 struct x
:
typedef struct {
int trees;
} Forest;
typedef struct {
Forest *forest;
} Place;
现在你可以写
malloc(sizeof(Forest));
折腾了几个小时后,我找到了解决办法。
您必须使用 extern C
让编译器使用 C 样式链接,但您还必须使用 C++ 的作用域解析 ::
才能正确解析结构类型。
头文件:
#ifdef __cplusplus
extern "C" {
#endif
typdef struct Place {
struct Forest {
int trees;
} *Forest;
} Place ;
#ifdef __cplusplus
}
#endif
节目:
#include <stdlib.h>
#include <iostream>
extern "C" {
static void allocateForest(Place *p){
p->Forest = (struct Place::Forest *)malloc(sizeof(struct Place::Forest));
}
}
int main(void){
Place p;
allocateForest(&p);
p.Forest->trees = 1;
std::cout << p.Forest->trees << std::endl;
return 0;
}
你应该给指针分配内存,否则它们就是NULL。使用这个:
Place.Forest = (struct Forest*) malloc(sizeof(struct Forest));
另一件事:不要将名称命名为 typedefs。