【发布时间】:2015-03-27 05:27:19
【问题描述】:
我正在使用 C 代码生成器创建具有以下结构的头文件:
typdef struct Place {
struct Forest {
int trees;
} *Forest;
} Place ;
并在 c++ 项目中使用它们。
当我尝试访问 Place.Forest->trees 时,我得到一个段错误,因为 Place.Forest 是一个悬空指针。
我无法正确 malloc 它,因为
Place.Forest = malloc(sizeof(Place.Forest));
只会返回指针的大小。
我无法使用
Place.Forest=malloc(sizeof(struct Forest));
因为我正在从 C++ 访问 Place,而范围界定使我无法看到 Forest。
如何在不更改 Place 或取消嵌套 Forest 的情况下为 Forest 分配内存?
由于自动生成大量代码,修改结构是不切实际的。
【问题讨论】:
标签: c++ c pointers struct nested