Week 7 Code Examples
#include <stdio.h>
#include <stdlib.h>
void do_something_on_heap() {
// malloc...
// free
}
int main(void) {
int num_elements;
scanf("%d", &num_elements);
int *data = malloc(num_elements * sizeof(int));
int i = 0;
while(i < num_elements) {
data[i] = i;
i++;
}
i = 0;
while(i < num_elements) {
printf("%d\n", data[i]);
i++;
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// create the initial malloc on the heap for an int array
int *create_data() { return malloc(sizeof(int)); }
int *add_data(int *data, int current_size, int value_to_add) {
// data -> array on the heap
data = realloc(data, (current_size + 1) * sizeof(int));
data[current_size] = value_to_add;
return data;
}
void print_data(int *data, int size) {
for (int i = 0; i < size; i++) {
printf("%d\n", data[i]);
}
}
int main(void) {
int array_size = 0;
// data stores the address on the heap of the "array"
int *data = create_data();
data[0] = 1;
array_size = 1;
data = add_data(data, array_size, 5);
array_size++;
data = add_data(data, array_size, 6);
array_size++;
data = add_data(data, array_size, 7);
array_size++;
print_data(data, array_size);
return 0;
}
// Jake's LL demo
// Add integers to a LL.
// Print the entire structure
#include <stdio.h>
#include <stdlib.h>
struct node {
// the data! Also called the payload.
int data;
// a pointer to the next node in the LL.
struct node *next;
};
// A function that creates a struct node, with some data that's passed in.
// Returns a pointer to the node on the heap.
// we do this to clean up main.
struct node *create_node(int data_to_add) {
// Allocate memory on the heap
// we'll use sizeof(struct node) to get the exact memory required
printf("Allocating %lu bytes on the heap for a new node\n", sizeof(struct node)); // keep in mind struct packing might make the answer surprise you
struct node *new_node = malloc(sizeof(struct node));
// initialises the new node's fields
// The aarrow oeprator -> is equivalent to a derefence then a field access: (*new_node).data = data_to_add;
new_node->data = data_to_add;
new_node->next = NULL; // NULL is a memory address reserved, that can't be dereferenced. usually at 0x000000
// we return the address of this node on the heap
return new_node;
}
void print_linked_list(struct node *head) {
// we need a current node that iterates through the list
// starting from the head node, we don't actually want to move the 'head' pointer.
// we are creating a pointer to refer to where head was pointing to...
struct node *current = head;
printf("Linked list: ");
// we loop. We loop as long as the current node is NOT at the NULL pointer.
while (current != NULL) {
printf("%d->", current->data);
current = current->next;
}
printf("NULL\n");
}
int main(void) {
// the first node in a linked list is the head of our list.
struct node *head = create_node(11);
// create the next few nodes
struct node *second_node = create_node(8);
struct node *third_node = create_node(7);
// we need to LINK the nodes in the linked list.
// the head node (11) should point to the second node
head->next = second_node;
// and so on.
second_node->next = third_node;
print_linked_list(head);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int num_elements;
scanf("%d", &num_elements);
// data is a pointer to a memory address on the heap
// int *data = malloc(sizeof(int));
int *data = malloc(num_elements * sizeof(int));
data[0] = 5;
data[1] = 10;
// these two lines are entirely equivalent
printf("%d\n", data[1]);
printf("%d\n", *(data + 1)); // this does a sizeof(int) and that's how many bytes it will shift when there's a +n
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int num_elements = 5;
int *data = malloc(num_elements * sizeof(int));
num_elements += 40;
data = realloc(data, num_elements * sizeof(int));
free(data);
}