Freeing memory by using free() whoes pointer is inside a pointer pointing to structure
Hey! I know title of question is very scary but I still I am not able to express the problem in single line!
So Here I go:
There is a Data Pointer say DataPtr which is pointing to a dynamically allocated memory and Another Structure Pointer say StructPtr which also points to a Dynamically allocated structure.
And These both are in another structure,say OuterStructure.
There is a Linked List which contains Pointer to OuterStructure.
struct StructONE
{
int a;
char b;
float c;
};
struct InnerStruct
{
char a;
int b;
};
struct StructTWO
{
int Num;
char * DataPtr;
struct InnerStruct * StructPtr;
};
struct LinkList
{
int NodeNum;
int NodeType; /* To Indicate Whether Pointer is of StructOne or StructTwo */
void * Ptr; /* This can be of type StructONE or StructTWO */
struct LinkList * Next;
};
LinkList * Start;
void main()
{
/* Structure Declarations */
InnerStruct * InnerStructure;
StructONE * OneStruct;
StructTWO * TwoStruct;
/* Fill up all the Structure */
InnerStructure= (InnerStruct *)calloc(100,sizeof(InnerStruct));
InnerStructure->a='a';
InnerStructure->b=5;
OneStruct= (StructONE *)calloc(100,sizeof(StructONE));
TwoStruct= (StructTWO *)calloc(100,sizeof(StructTWO));
TwoStruct->Dataptr=(char *) calloc(10,sizeof(char));
TwoStruct->StructPtr= In开发者_开发技巧nerStructure;
/* Add these to Linked List
void Add_to_Linked_List(int NodeNum,int NodeType,void *ptr)
*/
Add_to_Linked_List(1,1,OneStruct);
Add_to_Linked_List(2,2,TwoStruct);
/* Everything is Okey Till Here.
Now When I want to delete a node from linked list,
First I have to release Memory pointed by DataPtr and StructPtr. */
DeleteNode(1);
} /* End of Main */
The Code for DeleteNode is like this:
void DeleteNode(int Num)
{
LinkList * NodePtr,*TempNode;
NodePtr= Start;
while(NodePtr->NodeNum!=Num)
NodePtr=NodePtr->Next;
/* Now NodePtr points to desired node */
if(NodePtr->NodeType==1) /* Pointer is StructONE Type */
{
free(NodePtr->Ptr);
TempNode->Next=NodePtr->Next;
free(NodePtr);
return;
}
else
{
/* Now the Problem Begins....
In StructTWO type, I have to release Memory allocated for DataPtr as well Struct Ptr */
free((NodePtr->ptr)->DataPtr);
/* This Line Generates Error as
Error C2227: left of '->DataPtr' must point to class/struct/union/generic type */
}
}
What Should I do??
I know I have written code terribly... but Have to write this much complex!! :(
Thank you for reading this as well!!
#define STRUCT_ONE 1
#define STRUCT_TWO 2
struct LinkList
{
int NodeNum;
int NodeType;
union
{
StructONE * Ptr_One;
StructTWO * Ptr_Two;
};
};
................
if(NodePtr->NodeType==STRUCT_ONE)
{
free(NodePtr->Ptr_One);
TempNode->Next=NodePtr->Next;
free(NodePtr);
return;
}
else
{
free(NodePtr->Ptr_Two->DataPtr);
....
}
free((NodePtr->ptr)->DataPtr);
first of all, it is true that your code is awful. Second, you don't have a ptr
member. You have a Ptr
member. And the latter is void*, so if you want to get DataPtr
out of it, you should use a cast
free(((struct StructTwo*)(NodePtr->Ptr))->DataPtr);
You need to cast to the correct type
/* I like parenthesis :-) */
((struct StructTwo *)(NodePtr->ptr))->DataPtr
You need to cast :
if(NodePtr->NodeType==1) /* Pointer is StructONE Type */
{
....
}
else
{
struct StructTwo * s2ptr;
s2ptr = (struct StructTwo *)NodePtr->ptr;
free(s2ptr->DataPtr);
free(s2ptr->StructPtr);
free(NodePtr);
}
If you use the pointer more than once, declaring a variable to hold it avoids to do the type casting on each line.
精彩评论