Table of Contents

Class Pointer [C++], Data

Definition

Holds the address of another variable.

References

Data Structures Keyword Phase 2

Void Pointers

Definition

The void pointer is a generic pointer type. A pointer to void can store an address to any non-function data type, and, in C is implicitly converted to any other pointer type on assignment, but it must be explicitly cast if dereferenced inline.

References

Data Keyword 1 Phase 2 Phase 2

Dynamic Memory Allocation (Malloc/Free)

Definition

Dynamic memory allocation is the task of allocating a free chunk of memory specific to the size you predetermine in bytes, by using the malloc function. The chunk of memory is not always in the same location hence being “dynamic” instead of static. By using the “free” function, that will release the block of memory back to the system.

References

Demonstration (Malloc/Free)

/*
 * Sample code block
 */
#include<stdio.h>
#include<stdlib.h>
 
struct node {
        char value;
        struct node *next;
};
typedef struct node Node;
 
int main()
{
        Node *start, *tmp, *tmp2;       // Node pointers
        char input, i = 0;
 
        start = tmp = tmp2 = NULL;
 
        printf("Enter a value (-1 to end): ");
        scanf("%hhd", &input);
 
        while (input != -1)     // input does not equal -1
        {
                if (start == NULL)
                {
                        tmp = tmp2 = start = (Node *) malloc(sizeof(Node));     //allocating the size of the node
                        start->value = input;
                        start->next = NULL;     // Starts next element is null
                }
                else
                {
                        tmp->next = (Node *) malloc(sizeof(Node));
                        tmp->next->value = input;       //tmp next value= new node(user input)
                        tmp->next->next = NULL; //tmp next next =NULL
                        tmp = tmp->next;
                }
                printf("Enter a value (-1 to quit): ");
                scanf("%hhd", &input);
        }
 
        tmp = start;
        i=0;
        while (tmp != NULL)     //tmp does not equal NULL
        {
                printf("(%hhd)%hhd -> ",i, tmp->value); // print currrent value at node
                tmp = tmp->next;
                i = i + 1;
   }
        printf("NULL\n");
        printf("Enter node # to delete: ");
        scanf("%hhd", &input);
 
        tmp = start;
        if (input != 0)
        {
                for (i = 0; i < input -1; i++)
                        tmp = tmp->next;
                tmp2 = tmp -> next;
                        tmp -> next = tmp -> next -> next;
                tmp2 -> next = NULL;
                free (tmp2);
        }
        else
         {
                start = start -> next;
                tmp -> next = NULL;
                free(tmp);
        }
 
        tmp=start;
        i=0;
        while (tmp != NULL)     //tmp does not equal NULL
        {
                printf("(%hhd)%hhd -> ",i, tmp->value);  // print currrent value at node
                tmp = tmp->next;
                i = i + 1;
        }
        printf("NULL\n");
 
        return (0);
}

Alternatively (or additionally), if you want to demonstrate something on the command-line, you can do so as follows:

lab46:~$ cd src/data/Linked_List
lab46:~/src/data/Linked_List$ ls
lab46:~/src/data/Linked_List$ append  append.c  insert  insertion.c  link  linked.c  linkedlist.h  node  node.c  remove  remove.c
lab46:~/src/data/Linked_List$ ./remove
Enter a value (-1 to end): 1
Enter a value (-1 to quit): 2
Enter a value (-1 to quit): 3
Enter a value (-1 to quit): 4
Enter a value (-1 to quit): 5
Enter a value (-1 to quit): -1
(0)1 -> (1)2 -> (2)3 -> (3)4 -> (4)5 -> NULL
Enter node # to delete: 3
(0)1 -> (1)2 -> (2)3 -> (3)5 -> NULL
lab46:~/src/data/Linked_List$