尝试将内存分配给结构元素时收到分段错误错误
Receiving segmentation fault error when trying to allocate memory to a structure element
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 分配内存时出现错误 "Segmentation fault (core dumped)"。我在写 -
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 */
}
我终于弄清楚了错误。它不在将 space 分配给 ScanManager
。相反,我试图初始化 BT_ScanHandle
结构的一些成员,而不为其自身分配内存 space。工作代码是 -
openTreeScan(BT_ScanHandle **handle)
{
struct ScanManager *scanmeta = malloc(sizeof(ScanManager));
*handle = malloc(sizeof(BT_ScanHandle)); //Allocating some space
(*handle)->mgmtData = scanmeta; // Initializing
/* Remaining code */
}
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 分配内存时出现错误 "Segmentation fault (core dumped)"。我在写 -
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 */
}
我终于弄清楚了错误。它不在将 space 分配给 ScanManager
。相反,我试图初始化 BT_ScanHandle
结构的一些成员,而不为其自身分配内存 space。工作代码是 -
openTreeScan(BT_ScanHandle **handle)
{
struct ScanManager *scanmeta = malloc(sizeof(ScanManager));
*handle = malloc(sizeof(BT_ScanHandle)); //Allocating some space
(*handle)->mgmtData = scanmeta; // Initializing
/* Remaining code */
}