1 / 18

Dynamic Allocation and Linked Lists

Dynamic Allocation and Linked Lists. Dynamic memory allocation in C. C uses the functions malloc() and free() to implement dynamic allocation. malloc is defined in <stdlib.h> header files. malloc returns a void pointer.

cody
Download Presentation

Dynamic Allocation and Linked Lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Dynamic Allocation and Linked Lists

  2. Dynamic memory allocation in C • C uses the functions malloc() and free() to implement dynamic allocation. • malloc is defined in <stdlib.h> header files. • malloc returns a void pointer. • the pointer returned from malloc requires a type cast to make it usable. (void *: just a memory address)

  3. Null Pointers • When a memory allocation function is called, there’s always a possibility that it won’t be able to locate a block of memory large enough to satisfy the requres. • A null pointer will be returned. • Test the return value: p = malloc(10000); if(p == NULL){ }

  4. Example struct rec { char LastName[41]; char FirstName[41]; }; struct rec *r; r = (struct rec *) malloc(sizeof(struct rec));

  5. Freeing Memory • Memory allocated with malloc must be released after you are done with them. • this memory is released by calling the function free(). • free expects as an argument the pointer returned by malloc. • free( r );

  6. Allocate Memory for Arrays int *p; // one dimension int **q; // tow dimension int i; p = (int *) malloc( sizeof(int) * n); q = (int **) malloc( sizeof(int*) * n); for(i=0; i<n; ++i) { q[i] = (int *) malloc( sizeof(int) * n); } //…… free(p); free(q);

  7. The “Dangling Pointer” Problem char *p = malloc(4); …. free(p); …. strcpy(p, “abc”); // WRONG

  8. Linked List • A linked list is a basic data structure. • For easier understanding divide the linked list into two parts. • Nodes make up linked lists. • Nodes are structures made up of data and a pointer to another node. • Usually the pointer is called next.

  9. Nodes struct node { struct rec r; struct node *next; };

  10. LIST • The list will contain a pointer to the start of the list. • For our purposes all data will be added to the start of the list. • Initialize the start pointer to NULL.

  11. Creating a New Node • Allocate space for a new node. • Set the next pointer to the value of NULL • Set the data value for the node.

  12. Adding Nodes to the List • If the start node is null then the start node becomes the new node. • If start is not null then start becomes the new node’s next and the start becomes the new node.

  13. Deleting a node Q R P I want to delete Q

  14. Deleting a node Q R P P->next = Q->next; Q->next = NULL; Free(Q); I want to delete Q

More Related