How to store and remove dynamically and automatic variable of generic data type in custum list data
Posted
by Vineel Kumar Reddy
on Stack Overflow
See other posts from Stack Overflow
or by Vineel Kumar Reddy
Published on 2010-05-01T14:30:39Z
Indexed on
2010/05/01
14:37 UTC
Read the original article
Hit count: 247
Hi
I have created a List data structure implementation for generic data type with each node declared as following.
struct Node
{
void *data;
....
....
}
So each node in my list will have pointer to the actual data(generic could be anything) item that should be stored in the list. I have following signature for adding a node to the list
AddNode(struct List *list, void* eledata);
the problem is when i want to remove a node i want to free even the data block pointed by *data pointer inside the node structure that is going to be freed. at first freeing of datablock seems to be straight forward
free(data) // forget about the syntax.....
But if data is pointing to a block created by malloc then the above call is fine....and we can free that block using free function
int *x = (int*) malloc(sizeof(int));
*x = 10;
AddNode(list,(void*)x); // x can be freed as it was created using malloc
what if a node is created as following
int x = 10;
AddNode(list,(void*)&x); // x cannot be freed as it was not created using malloc
Here we cannot call free on variable x!!!!
How do i know or implement the functionality for both dynamically allocated variables and static ones....that are passed to my list....
Thanks in advance...
© Stack Overflow or respective owner