Preface

This lab will cover a few things:

  1. We’ll start by showing a few examples and have you try to figure out what needs to be freed, and what shouldn’t be.
  2. We will then try to get you to run through the idea of 2D arrays with pointers.
  3. We will then give problems where allocation would be helpful.

Get Started

Download the Lab 7 code through Entainer and open a terminal in the Lab 7 directory

Activity 1’s debugging snippets are in activity-1/part-1 through activity-1/part-5; run make and ./main inside the part you are studying Activity 1’s debugging snippets are in activity-1/part-1 through activity-1/part-5; run make and ./main inside the part you are studying

Run make to compile the Activity 2 starters.


Activity 1

Part 1

#include <stdlib.h>
#include <stdio.h>
 
int main(){
	char *str = calloc(5, sizeof(char));
}

Is there anything missing from the above code snippet? If so, what should you add and where?

Part 2

#include <stdlib.h>
#include <stdio.h>
 
int main(){
	char *str = "asdf";
	printf("%s\n", str);
}

Is there anything missing from the above code snippet? If so, what should you add and where?

Part 3

#include <stdlib.h>
#include <stdio.h>
 
int main(){
	char *str = calloc(5, sizeof(char));
	char *p = str;
	free(str);
}

Is there anything missing from the above code snippet? If so, what should you add and where?

Part 4

#include <stdlib.h>
#include <stdio.h>
 
int main(){
	char *str = nullptr;
	size_t n = 0;
	getline(&str, &n, stdin);
	printf("Input line: %s\n", str);
}

Is there anything missing from the above code snippet? If so, what should you add and where?

Part 5

#include <stdlib.h>
#include <stdio.h>
 
size_t length_of_str(char *s){
	size_t n = 0;
	while(s[n] != '\0'){
		++n;
	}
	return n;
}
 
int main(){
	char *str = nullptr;
	size_t n = 0;
	getline(&str, &n, stdin);
	printf("input line length was %zu", length_of_str(str));
	free(str);
}

Is there anything missing from the above code snippet? If so, what should you add and where?


Activity 2

Ragged Allocation

Open activity-2/ragged-allocation/main.c.

#include <stdio.h>
#include <stdlib.h>
 
int **allocate_arrays(size_t num_arrays, size_t sizes[]){
	// Your code here
	return nullptr;
}
 
void free_arrays(int **arrays, size_t num_arrays){
	// Your code here
}
 
int main(){
	size_t sizes[] = {2, 0, 5};
	size_t num_arrays = 3;
	int **arrays = allocate_arrays(num_arrays, sizes);
	if(arrays == nullptr){
		fprintf(stderr, "Implement allocate_arrays first!\n");
		return 1;
	}
	for(size_t i = 0; i < num_arrays; ++i){
		if((sizes[i] == 0) != (arrays[i] == nullptr)){
			fprintf(stderr, "Unexpected row pointer!\n");
			free_arrays(arrays, num_arrays);
			return 1;
		}
		for(size_t j = 0; j < sizes[i]; ++j){
			arrays[i][j] = (int)j;
			printf("%d ", arrays[i][j]);
		}
		putchar('\n');
	}
	free_arrays(arrays, num_arrays);
}

Your Task: You are given an array that specifies a sequence of sizes. You should return an array of arrays where the capacity of the array is sizes[i]. Also write the corresponding code to free the entire array, using num_arrays to visit every row, including rows after an empty row. Any empty arrays should be set to nullptr. And yes, num_arrays can be 0. In which case, you should return nullptr. free_arrays(nullptr, 0) should do nothing. You may assume that all allocations succeed.

For example, if num_arrays = 3 and sizes = {2, 0, 5}, then the array should be able to fit integers, the array should be nullptr, and the last array should be able to fit integers.

List of Word Starts

Open activity-2/word-starts/main.c.

#include <stdio.h>
#include <stdlib.h>
 
void print_word(char const *s){
	while(*s && *s != ' '){
		putchar(*s);
		++s;
	}
}
 
char **find_word_starts(char *str, size_t *num_words_ptr){
	// Your code here
	*num_words_ptr = 0;
	return nullptr;
}
 
void free_word_starts_list(char **list){
	// Your code here
}
 
int main(){
	char *str = nullptr;
	size_t n = 0;
	ssize_t r = getline(&str, &n, stdin);
	if(r < 0){
		free(str);
		return 1;
	}
	if(r > 0 && str[r - 1] == '\n'){
		str[r - 1] = '\0';
	}
 
	size_t num_words = 0;
	char **word_list = find_word_starts(str, &num_words);
	for(size_t idx = 0; idx < num_words; ++idx){
		print_word(word_list[idx]);
		putchar('\n');
	}
 
	// What kind of frees/deallocations do you need to make?
 
}

Your Task: Given a string of words (only alphabets and spaces, with potentially more than 1 space between words). Return a list of pointers where the pointer points to the start of the word. Set the size_t pointed to by num_words_ptr to the number of words in the string. Also write the corresponding freeing function and make sure that everything is properly deallocated.

An empty string or a string containing only spaces has zero words: set *num_words_ptr to 0 and return nullptr. Do not modify or copy the original string. free_word_starts_list frees only the pointer list; the original string is freed separately in main. The freeing function should accept nullptr. You may assume all allocations succeed.


Activity 3

The final two exercises are part of Volume 7.

  • Histogram: allocate an array of character counts
  • Line Breaks: allocate pointers into a string and free only the pointer list

Take a look at the Volume 7 page for more details.