malloc problem

Hello there, I'm having a problem with this function:

1
2
3
4
5
6
7
8
ResNode* addRes(Resource *newRes, ResNode* old){

    ResNode *newNode = (ResNode*)malloc(sizeof(ResNode));
    newNode->resN = newRes;
    newNode->nextRes = old;

    return newNode;
};


Here is ResNode struct,
1
2
3
4
struct resNode{
    struct Resource *resN;
    struct ResNode *nextRes;
};


The program is crashing when it come to the malloc line, any error on code?
Thanks!
what's the error message saying?
btw, your structure is for resNode and not ResNode
Last edited on
it says the .exe stops working, I use ResNode cause I have a .h with this:
 
typedef struct resNode ResNode;
Maybe you should check the return value of malloc, which can return NULL, before dereferencing newNode?

If that's not the culprit, you've likely trashed some memory you shouldn't have somewhere else in the code.
So I checked that, and its really returning NULL, its possible to fix?
thank you so far
Last edited on
Topic archived. No new replies allowed.