I have the following two structs:
typedef struct label{
int id;
double p,*t,q,c;
int V[45];
struct label *next;
struct label *prev;
struct path *tail;
struct path *head;
}label;
typedef struct path{
int i;
struct path *Pperv;
struct path *Pnext;
}path;
void main (){
int i,j;
struct label *Current,*Head,*Tail;
struct path *test1,*path_head,*path_tail;
Head=(struct label*)malloc(1*sizeof(struct label));
Tail=(struct label*)malloc(1*sizeof(struct label));
Head->next=Tail;
Tail->prev=Head;
for (i=0;i<250000;i++)
{
Current=(struct label*)malloc(1*sizeof(struct label));
Current->t=(double*)malloc(15*sizeof(double));
Current->head=(struct path*)malloc(1*sizeof(struct path));
Current->tail=(struct path*)malloc(1*sizeof(struct path));
Current->head->Pnext=Current->tail;
Current->tail->Pperv=Current->head;
for (j=0;j<15;j++)
{
test1=(struct path*)malloc(1*sizeof(struct path));
test1->Pperv=Current->head;
test1->Pnext=Current->head->Pnext;
Current->head->Pnext->Pperv=test1;
Current->head->Pnext=test1;
test1->i=1;
Current->t[j]=23123.4323334;
}
Current->next=Tail;
Current->prev=Tail->prev;
Tail->prev->next=Current;
Tail->prev=Current;
Current->p=54545.323241321;
}
}
I just used an example of filling some of the variables in them so that I can make my question. What I am facing problem with is how to free the struct "Path" that is contained into the the first struct called name "Label".
I would me sth more than greatful if somenone could give me the code of how to correctly free both structs in C.
In general, you just need to be symmetrical with the calls to malloc/calloc:
label *current = malloc(sizeof(*current));
current->head = malloc(sizeof(*current->head));
...
free(current->head);
free(current);
Related
I'm trying to allocate memory for the code,of which i've only included excerpts from the actual program, that follows below, the problem I am having is that i don't know how to allocate memory to the type Key that lies within BStree_node this leads to the issue of segmentation errors when i try to assign values to variables within Key.
typedef int Data_Item;
typedef char* Sub_Key;
typedef struct {Sub_Key key1; Sub_Key key2;} Key;
struct BStree_node{
Key key;
Data_Item data;
struct BStree_node *left, *right;
}
typedef struct BStree_node BStree_node;
typedef BStree_node** BStree;
BStree bs_tree_ini(void){
BStree tempTreePointer;
tempTreePointer = malloc(sizeof(BStree_node*));
BStree_node *tempNode;
tempNode = malloc(sizeof(BStree_node));
tempNode = NULL;
tempTreePointer = &tempNode;
return tempTreePointer;
}
You could initialize your node like this, using calloc to zero the memory to initialize all the fields properly:
BStree_node *init_node()
{
BStree_node *rval = calloc(1,sizeof(BStree_node)); // so all data & pointers are zeroed
return rval;
}
use it like this: init main, and only left. right stays zeroed: no right node for that main node.
int main()
{
BStree_node *head = init_node();
head->left = init_node();
...
return 0;
}
I've a nested struct like this
typedef struct Node_link {
struct Node_base *parent, *left, *right;
}Node_link;
typedef struct Node_base {
struct Node_link link[2];
}Node_base;
typedef struct Node{
struct Node_base base;
int size;
int *address;
}Node;
Node_base *head[2] ={NULL, NULL};
//head[0] stores int size and head[1] it's corresponding address
The node has right, left and parent link, all are nested e.g node->left->link.parent=node. I've to maintain all links(parent, left and right) and delete nodes.
I've tried a lot of cases and still missing some. Can someone tell me what all cases I need to use? Or refer me to some material? I searched a lot but no success.
My insert function is as follows:
Node_base * insert(Node_base *location, Node_base *n) {
if (head[0]==NULL)
head[0]=n;
else
{
if (location==NULL){
location=n;
return location;
}
else{
if(((Node *)n)->size < ((Node *)location)->size){
if(location->link[0].left==NULL)
{
location->link[0].left=n;
location->link[0].left->link[0].parent=location;
}
else
location->link[0].left=insert(location->link[0].left,n);
return location;
}
}
And I've the same nested insert function for head[1] which stores the size of node inserted in head[0].
It's hard to tell what's going on here. Your code doesn't look remotely like any BST implementation I've ever seen. Why the need for the Node_Link struct? The pointers in the Node structs should define what the links are. Why the parent pointer? That shouldn't be needed in a standard BST implementation. All you should need is:
struct node {
node *left;
node *right;
void *data;
int size;
};
struct bst {
node *root;
};
I've written this code with all correct understandings i have. please check my problems.
#include<stdio.h>
#include<stdlib.h>
// Define a structure for the dequeue elements
This structure is all good, with data, next, previous pointers.
typedef struct RanElmt_ {
void *data;
struct DeqElmt_ *prev;
struct DeqElmt_ *next;
void (*destroy)(void *data);
//Your Code here
} RanElmt;
THis is ok too, acording to what i think is correct.
typedef struct RandQ_{
int size;
struct RanElmt *head;
struct RanElmt *tail;
}RandQ;
RandQ * RandomizedQueue(void (*destroy)(void *data)){
RandQ *relmt = (RandQ*)malloc(sizeof(RandQ));
} // construct an empty randomized queue
int isREmpty(RandQ *rQ){
if ( rQ->size == 0)
return 1;
return 0;
} // is the queue empty?
int rsize(RandQ *rQ){
return rQ->size;
}
// return the number of items on the queue
ACtually this is only one function,(enqueue) I'm going to get the idea and code other functions(dequeue, sample etc..)
int enqueue(RandQ *rQ, const void *data){
RanElmt *relmt = (RanElmt*)malloc(sizeof(RanElmt));
relmt->data = (void*)data;
if (rQ->head == NULL){
relmt = rQ->head;
relmt = rQ->tail;
relmt->prev = NULL;
relmt->next = NULL;
}
else{
rQ->head = relmt;
}
(rQ->head)->prev = relmt;
relmt->prev = rQ->head;
rQ->head = relmt;
} // add the item
main(){
Deque(free);
printf(" okk \n");
}
THis program is giving these errors:
Errors i'm getting
In C struct tags and type names live in different name spaces. That is struct RanElmt and RanElmt are two different types, in addition struct RanElmt is not completely defined.
Your RandQ should be defined something like
typedef struct RandQ_{
int size;
struct RanElmt_ *head; // or RanElmt* head;
struct RanElmt_ *tail; // or RanElmt* tail;
}RandQ;
in addition your RanElmt is probably not what you want, maybe you meant:
typedef struct RanElmt_ {
void *data;
struct RanElmt_ *prev; // pointer to a struct of the same type
struct RanElmt_ *next; // pointer to a struct of the same type
void (*destroy)(void *data);
// You cannot put code here in C (or even a function definition AFAIK).
} RanElmt;
You have confused the struct tag and the typedeffed alias for the queue elements in the definition of the queue:
typedef struct RandQ_{
int size;
struct RanElmt *head;
struct RanElmt *tail;
} RandQ;
Here, the head and tail are of the type struct RanElmt. This struct doesn't exist in your program. You have a struct RanElmt_ (with trailing underscore) that you can also call ´RanElmtwithout thestructkeyword, because you have combined the struct definition with atypedef`.
The compiler still generates the code, because pointers to unknown structs are okay, unless you try to get at their data. Obviously the compiler can't access the struct fields if it doesn't know them.
There's no need for the underscore. The names of structs are in a separate namespace, so you can have both a struct called RandQ and a type (in global namespace) called RanQ. I recommend to use the same name for struct tag and aliassed type.
You can also get rid of the need to use the struct keyword inside the struct defnition if you separate the typedef from the struct definition:
typedef struct RanElmt RanElmt; // use just RanElmt from now on
struct RanElmt {
void *data;
RanElmt *prev;
RanElmt *next;
} RanElmt;
Your code has several other problems, but I think he program is in an early state, so I don't address them here.
I know there are at least 10 questions already about this, but they all point to something I am not doing.
In a header file I have...
typedef struct Node {
struct Node *next;
struct pgmap page;
} Node;
typedef struct linkedlist {
struct Node *head_ptr;
struct Node *tail_ptr;
} LList;
In my c file I have
struct LList mainList;
int main()
{
struct LList *root;
root = &mainList;
root->head_ptr = NULL;
root->tail_ptr = NULL;
...
}
On the root-> lines I get the dereferencing ptr... error. All the threads already on here point to a problem where people accidentally create anonymous structs, such as
typedef struct{
int a;
}; monkey
instead of
typedef struct monkey{
int a;
}; monkey
So what am I missing????
There is no type called "struct LList". The code "typedef struct linkedlist { ... } LList;" creates two type names: one is struct linkedlist, and the other is just LList (without "struct"). You thus need to change "struct LList" to "LList."
I bumped into this error when I was trying to access a field in my defined struct:
struct linkNode{
struct linkNode *next;
char *value;
};
In the header file I defined a type called linkNode_t:
typedef struct linkNode linkNode_t;
When I tried to use this struct in the main of another file, everything else was fine except when I tried to do
linkNode_t* currentpath = /*a pointer to a struct of type linkNode_t*/
int something = strlen(currentpath->value);/****ERROR*****/
Compiler gave me the incomplete type error. Am I declaring the struct properly?
Struct has to be declared in header, before you do typedef. You can combine both:
typedef struct linkNode {
struct linkNode *next;
char *value;
} linkNode_t;
As the others pointed out, it's generally better to put your "typedef" and your struct definition all in the same place.
But that isn't required, and that's not the problem.
This test case compiles and runs correctly:
#include <stdio.h>
#include <string.h>
#define NULL 0
struct linkNode{
struct linkNode *next;
char *value;
};
typedef struct linkNode linkNode_t;
linkNode_t rec = {
NULL,
"abcdef"
};
int
main (int argc, char *argv[])
{
linkNode_t* currentpath = &rec;
int something = strlen(currentpath->value);
printf ("sizeof (rec)= %d, currentpath->value= %s, something= %d...\n",
sizeof (rec), currentpath->value, something);
return 0;
}
ACTUAL PROBLEM AND SOLUTION:
1) You're doing all the right stuff.
2) Just make sure you put your "typedef" AFTER (or, at least, as part of) your struct definition:
struct linkNode{
...
};
typedef struct linkNode linkNode_t;
struct linkNode{
struct linkNode *next;
char *value;
};
This is incomplete because you cannot use struct directly inside the structure.
You should use
typedef struct linkNode{
struct linkNode *next;
char *value;
}new_name;