0
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;

I am getting an error "Segmentation fault (core dumped)" when I try to allocate memory for my structure ScanManager. I am writing -

ScanManager * scanmeta = (ScanManager *) malloc(sizeof(ScanManager));

I have tried using calloc instead of malloc but that didn't work. How do I allocate memory for that structure since I want to use it further in my code?

typedef struct Value {
  DataType dt;
  union v {
    int intV;
    char *stringV;
    float floatV;
    bool boolV;
  } v;
} Value;

Also, I have one more structure -

typedef struct BT_ScanHandle {
    BTreeHandle *tree;
    void *mgmtData;
} BT_ScanHandle;

I am passing this structure's reference in a function as mentioned below and try to access my ScanManager structure here -

openTreeScan(BT_ScanHandle **handle)
{
    struct ScanManager *scanmeta = malloc (sizeof (struct ScanManager));
    (** handle).mgmtData = scanmeta;

    /* Remaining code */
}
  • 2
    I guess the problem is not on `malloc` instruction, but where you try to access `node` that is not allocated, or a pointer inside `node`. – LPs Dec 02 '15 at 06:59
  • 3
    General warning: [do not cast malloc return](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – LPs Dec 02 '15 at 07:01
  • Tried this and there is no error: https://ideone.com/reEZrv Share what Value stands for. Also share how you are accessing scanmeta. – thepace Dec 02 '15 at 08:50
  • You probably caused heap corruption earlier in the program – M.M Dec 02 '15 at 21:09

1 Answers1

1

I finally figured out the error. It was not in allocating space to ScanManager. Instead, I was trying to initialize some of the members of BT_ScanHandle structure without allocating memory space to itself. The working code is -

openTreeScan(BT_ScanHandle **handle)
{
    struct ScanManager *scanmeta = malloc(sizeof(ScanManager));
    *handle = malloc(sizeof(BT_ScanHandle)); //Allocating some space
    (*handle)->mgmtData = scanmeta; // Initializing

    /* Remaining code */
}