0

I want to allocate memory dinamically in netfilter programming, but some problems came out.

These are structures what I want to make new dynamic memory. (Source : http://git.netfilter.org/libnetfilter_queue/tree/src/libnetfilter_queue.c)

struct nfq_q_handle
{
    struct nfq_q_handle *next;
    struct nfq_handle *h;
    uint16_t id;

    nfq_callback *cb;
    void *data;
};

struct nfq_data {
    struct nfattr **data;
};

And this is a function what I am making now.

NODE*   create_node(struct nfq_q_handle* qh_data, struct nfgenmsg* nfmsg_data, struct nfq_data* nfa_data, void* data_data) 
{
    NODE* node  = (NODE*)malloc(sizeof(NODE));
    struct nfq_q_handle* temp_qh = (struct nfq_q_handle*)malloc(sizeof(struct nfq_q_handle));
    struct nfgenmsg* temp_nfmsg = (struct nfgenmsg*)malloc(sizeof(struct nfgenmsg));
    struct nfq_data* temp_nfa = (struct nfq_data*)malloc(sizeof(struct nfq_data));
    void* temp_data = (void*)malloc(malloc_usable_size(data_data));

    memcpy(temp_qh, qh_data, sizeof(struct nfq_q_handle));
    node->node_qh = temp_qh;
    memcpy(temp_nfmsg, nfmsg_data, sizeof(struct nfgenmsg));
    node->node_nfmsg    = temp_nfmsg;
    memcpy(temp_nfa, nfa_data, sizeof(struct nfq_data);
    node->node_nfa  = temp_nfa;
    memcpy(temp_data, data_data, malloc_usable_size(data_data));
    node->node_data = temp_data;
    return node;
}

When I use malloc in function and compile it, Error messages come out. "error: invalid application of `sizeof' to incomplete type 'struct..' Actually struct nfgenmsg is working fine, but struct nfq_q_handle and struct nfa_data are not working and show error message.

Is there any solutions? Please help me.

And also, I used 'malloc_usable_size', but I don't know how to use it correctly. If you have an idea with 'malloc_usable_size', please talk to me. Thanks.

Plus: I notified struct is declared in header file only "struct nfq_q_handle". But in c file, it has its parameters. Like, struct nfq_q_handle { .... }

  • 3
    First of all, *where* do you get the error? And have you included the header file where the structure is actually *defined* (not only declared)? Lastly, please read [this question and its answers](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) about casting the result of `malloc` in C. – Some programmer dude Mar 06 '17 at 10:23
  • Thanks for your comment! I got the error when I compile that source. The errors came out from 1. struct nfq_q_handle* temp_qh = (struct nfq_q_handle*)malloc(sizeof(struct nfq_q_handle)); and 2. struct nfq_data* temp_nfa = (struct nfq_data*)malloc(sizeof(struct nfq_data)); The message told me that sizeof(struct nfq_q_handle) and sizeof(nfa_data) are wrong. I included all header files. – Wonjoon Lee Mar 06 '17 at 10:35

0 Answers0