Note that there are some explanatory texts on larger screens.

plurals
  1. POFreeing memory by using free() whoes pointer is inside a pointer pointing to structure
    text
    copied!<p>Hey! I know title of question is very scary but I still I am not able to express the problem in single line!</p> <p>So Here I go:</p> <p>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.</p> <p>And These both are in another structure,say OuterStructure.</p> <p>There is a Linked List which contains Pointer to OuterStructure. </p> <pre><code>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-&gt;a='a'; InnerStructure-&gt;b=5; OneStruct= (StructONE *)calloc(100,sizeof(StructONE)); TwoStruct= (StructTWO *)calloc(100,sizeof(StructTWO)); TwoStruct-&gt;Dataptr=(char *) calloc(10,sizeof(char)); TwoStruct-&gt;StructPtr= InnerStructure; /* 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 */ </code></pre> <p>The Code for DeleteNode is like this:</p> <pre><code>void DeleteNode(int Num) { LinkList * NodePtr,*TempNode; NodePtr= Start; while(NodePtr-&gt;NodeNum!=Num) NodePtr=NodePtr-&gt;Next; /* Now NodePtr points to desired node */ if(NodePtr-&gt;NodeType==1) /* Pointer is StructONE Type */ { free(NodePtr-&gt;Ptr); TempNode-&gt;Next=NodePtr-&gt;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-&gt;ptr)-&gt;DataPtr); /* This Line Generates Error as Error C2227: left of '-&gt;DataPtr' must point to class/struct/union/generic type */ } } </code></pre> <p>What Should I do??</p> <p>I know I have written code terribly... but Have to write this much complex!! :(</p> <p>Thank you for reading this as well!!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload