Google Ads

Thursday, September 24, 2009

Allocating and Freeing Dynamic Variables

Allocating and Freeing Dynamic Variables


If X is any object, &X is a pointers to help implement dynamic linked lists.
In C a pointer variable to an integer can be created by the declaration

Int *p;

Once a variable p has been declared as a pointer to a specific type of object, it must be possible to dynamically create an object of that specific type and assign its address to p.

Malloc dynamically allocates a portion of memory and returns a pointer to an item.

Pi = (int *) malloc(sizeof(int));

Pr = (float *)malloc(sizeof(float));



Linked list using Dynamic variables

The capability of dynamically allocating and freeing a variable.

Struct node
{
Int info;
Struct node * next;
};



Tydedef struct node *NODEPTR;

NODEPTR p;

P=getnode();

NODEPTR getnode()
{
NODEPTR p;
P = (NODEPTR) malloc(sizeof(struct node));
Return(p);
}


Freenode(p);

No comments:

Post a Comment