Initial Course

This commit is contained in:
David Gil de Gómez Pérez
2026-08-11 17:40:36 +03:00
commit 9aed1d1d86
202 changed files with 3420 additions and 0 deletions
@@ -0,0 +1,8 @@
add_course_exercise(NAME pointer_basics MODULE 05_pointers_and_references STANDARD 17
SOURCES src/pointer_basics.cpp test/pointer_basics_test.cpp)
add_course_exercise(NAME references MODULE 05_pointers_and_references STANDARD 17
SOURCES src/references.cpp test/references_test.cpp)
add_course_exercise(NAME pointer_arithmetic MODULE 05_pointers_and_references STANDARD 17
SOURCES src/pointer_arithmetic.cpp test/pointer_arithmetic_test.cpp)
add_course_exercise(NAME dynamic_memory MODULE 05_pointers_and_references STANDARD 17
SOURCES src/dynamic_memory.cpp test/dynamic_memory_test.cpp)
@@ -0,0 +1,17 @@
# Module 05: Pointers and References
## Learning Goals
- Understand pointers, addresses, and dereferencing
- Use references as aliases and for function parameters
- Apply pointer arithmetic carefully
- Manage dynamic memory with `new`/`delete` and prefer smart pointers later
## Exercises
| Exercise | Topic |
|----------|-------|
| `pointer_basics` | Address-of, dereference, null |
| `references` | Reference parameters and return |
| `pointer_arithmetic` | Iterating with pointers |
| `dynamic_memory` | Manual allocation patterns |
@@ -0,0 +1,11 @@
#pragma once
#include <cstddef>
namespace pointers {
[[nodiscard]] auto allocate_and_fill(std::size_t count, int value) -> int*;
void deallocate(int* ptr);
[[nodiscard]] auto clone_array(const int* source, std::size_t count) -> int*;
} // namespace pointers
@@ -0,0 +1,11 @@
#pragma once
#include <cstddef>
namespace pointers {
[[nodiscard]] auto pointer_distance(const int* begin, const int* end) -> std::size_t;
[[nodiscard]] auto find_pointer(const int* begin, const int* end, int target) -> const int*;
auto reverse_in_place(int* begin, int* end) -> void;
} // namespace pointers
@@ -0,0 +1,10 @@
#pragma once
namespace pointers {
[[nodiscard]] auto get_value(int* ptr) -> int;
void set_value(int* ptr, int value);
[[nodiscard]] auto is_null(const int* ptr) -> bool;
void swap_via_pointers(int* a, int* b);
} // namespace pointers
@@ -0,0 +1,10 @@
#pragma once
namespace pointers {
[[nodiscard]] auto double_value(const int& value) -> int;
void increment(int& value);
[[nodiscard]] auto max_ref(const int& a, const int& b) -> const int&;
[[nodiscard]] auto sum_three(const int& a, const int& b, const int& c) -> int;
} // namespace pointers
@@ -0,0 +1,9 @@
#include "dynamic_memory.hpp"
namespace pointers {
auto allocate_and_fill(std::size_t count, int value) -> int* { (void)count; (void)value; return nullptr; }
void deallocate(int* ptr) { delete[] ptr; }
auto clone_array(const int* source, std::size_t count) -> int* { (void)source; (void)count; return nullptr; }
} // namespace pointers
@@ -0,0 +1,9 @@
#include "pointer_arithmetic.hpp"
namespace pointers {
auto pointer_distance(const int* begin, const int* end) -> std::size_t { (void)begin; (void)end; return 0; }
auto find_pointer(const int* begin, const int* end, int target) -> const int* { (void)begin; (void)end; (void)target; return end; }
void reverse_in_place(int* begin, int* end) { (void)begin; (void)end; }
} // namespace pointers
@@ -0,0 +1,10 @@
#include "pointer_basics.hpp"
namespace pointers {
auto get_value(int* ptr) -> int { (void)ptr; return 0; }
void set_value(int* ptr, int value) { (void)ptr; (void)value; }
auto is_null(const int* ptr) -> bool { (void)ptr; return true; }
void swap_via_pointers(int* a, int* b) { (void)a; (void)b; }
} // namespace pointers
@@ -0,0 +1,10 @@
#include "references.hpp"
namespace pointers {
auto double_value(const int& value) -> int { (void)value; return 0; }
void increment(int& value) { (void)value; }
auto max_ref(const int& a, const int& b) -> const int& { (void)a; (void)b; return a; }
auto sum_three(const int& a, const int& b, const int& c) -> int { (void)a; (void)b; (void)c; return 0; }
} // namespace pointers
@@ -0,0 +1,18 @@
#include "dynamic_memory.hpp"
#include <gtest/gtest.h>
TEST(DynamicMemory, AllocateAndFill) {
int* data = pointers::allocate_and_fill(3, 7);
ASSERT_NE(data, nullptr);
EXPECT_EQ(data[0], 7);
EXPECT_EQ(data[2], 7);
pointers::deallocate(data);
}
TEST(DynamicMemory, CloneArray) {
int source[] = {1, 2, 3};
int* copy = pointers::clone_array(source, 3);
ASSERT_NE(copy, nullptr);
EXPECT_EQ(copy[1], 2);
pointers::deallocate(copy);
}
@@ -0,0 +1,19 @@
#include "pointer_arithmetic.hpp"
#include <gtest/gtest.h>
TEST(PointerArithmetic, Distance) {
int data[] = {1, 2, 3, 4};
EXPECT_EQ(pointers::pointer_distance(data, data + 4), 4U);
}
TEST(PointerArithmetic, FindPointer) {
int data[] = {1, 2, 3};
EXPECT_EQ(pointers::find_pointer(data, data + 3, 2), data + 1);
}
TEST(PointerArithmetic, ReverseInPlace) {
int data[] = {1, 2, 3, 4};
pointers::reverse_in_place(data, data + 4);
EXPECT_EQ(data[0], 4);
EXPECT_EQ(data[3], 1);
}
@@ -0,0 +1,21 @@
#include "pointer_basics.hpp"
#include <gtest/gtest.h>
TEST(PointerBasics, GetSetValue) {
int x = 42;
pointers::set_value(&x, 100);
EXPECT_EQ(pointers::get_value(&x), 100);
}
TEST(PointerBasics, IsNull) {
EXPECT_TRUE(pointers::is_null(nullptr));
int x = 1;
EXPECT_FALSE(pointers::is_null(&x));
}
TEST(PointerBasics, SwapViaPointers) {
int a = 1, b = 2;
pointers::swap_via_pointers(&a, &b);
EXPECT_EQ(a, 2);
EXPECT_EQ(b, 1);
}
@@ -0,0 +1,19 @@
#include "references.hpp"
#include <gtest/gtest.h>
TEST(References, DoubleValue) { EXPECT_EQ(pointers::double_value(21), 42); }
TEST(References, Increment) {
int x = 5;
pointers::increment(x);
EXPECT_EQ(x, 6);
}
TEST(References, MaxRef) {
int a = 3, b = 9;
EXPECT_EQ(&pointers::max_ref(a, b), &b);
}
TEST(References, SumThree) {
EXPECT_EQ(pointers::sum_three(1, 2, 3), 6);
}