which malloc statement should i use??

let us say i have a node as follows..
struct hnode
{
char* data;
struct hnode* hlink;
struct vnode* vlink;
};
which malloc statement should i use in the following gethnode function which returns the address of a hnode... char* data has to be the string that has to be copied to char* data in hnode...
struct hnode* gethnode(char* hdata)
{
struct hnode *x;
x=(struct hnode *)malloc(sizeof(struct hnode)+strlen(hdata));
/*should i use the malloc statement mentioned above
or should i use malloc(sizeof(struct hnode)); the one without strlen(data) */

if(!x)
{
printf("Memory Insufficient");
exit(0);
}
return x;
}
malloc(sizeof(struct hnode));

struct hnode needs the size of 3 pointers, char* + struct hnode* + struct vnode* and you don't have to worry about what these pointer's point to while allocating memory for hnode*.

-Yogindar
http://yogindar.blogspot.com
malloc(sizeof(struct hnode));

struct hnode needs the size of 3 pointers, char* + struct hnode* + struct vnode* and you don't have to worry about what these pointer's point to while allocating memory for hnode*.

-Yogindar
http://yogindar.blogspot.com
Because this is a C++ forum (as opposed to C), I assume this is C++ code. Then you should not use a malloc statement, but:

 
struct hnode *x = new hnode;

Topic archived. No new replies allowed.