【发布时间】:2015-12-02 06:48:44
【问题描述】:
typedef struct Node
{
void ** pointers;
Value ** keys;
struct Node * parent;
bool is_leaf;
int num_keys;
struct Node * next;
} Node;
typedef struct ScanManager {
int keyIndex;
int totalKeys;
Node * node;
} ScanManager;
当我尝试为我的结构 ScanManager 分配内存时,我收到错误“分段错误(核心转储)”。我在写——
ScanManager * scanmeta = (ScanManager *) malloc(sizeof(ScanManager));
我尝试过使用calloc 而不是malloc,但这没有用。由于我想在代码中进一步使用它,如何为该结构分配内存?
typedef struct Value {
DataType dt;
union v {
int intV;
char *stringV;
float floatV;
bool boolV;
} v;
} Value;
另外,我还有一个结构 -
typedef struct BT_ScanHandle {
BTreeHandle *tree;
void *mgmtData;
} BT_ScanHandle;
我在下面提到的函数中传递了这个结构的引用,并尝试在此处访问我的 ScanManager 结构 -
openTreeScan(BT_ScanHandle **handle)
{
struct ScanManager *scanmeta = malloc (sizeof (struct ScanManager));
(** handle).mgmtData = scanmeta;
/* Remaining code */
}
【问题讨论】:
-
我猜问题不在于
malloc指令,而在于您尝试访问未分配的node或node内的指针。 -
试过了,没有错误:ideone.com/reEZrv分享Value代表什么。还分享您如何访问 scanmeta。
-
您可能在程序早期导致堆损坏
标签: c pointers memory-management struct malloc