Lab 3 Assignment
Overview
This is an explore-and-reflect lab. The goal is to learn by trying things, not to finish a polished homework. You will read a few short lessons, then play with three pieces that sit underneath Homework 2: how an array is just contiguous memory you walk with a pointer, and the two ways C++ manages memory on the free store. When you are done, you record a short video where you show what you tried and explain what you learned. There is no written report.
import std;. In this class, and in these labs, we will use #include (for example, #include <iostream>). Both are valid modern C++.v[i] is pointer math. The buffer also keeps more room than it is using (a capacity larger than the size), which ordinary new[] cannot do but raw allocation plus placement new can. This lab is where you see all three for yourself.Reading
Read these lessons on learncpp.com (each opens in a new tab). If a lesson ends with a Quiz time, do it and check the provided solution.
Explore 1: an array is contiguous memory you walk with a pointer
An array's elements sit next to each other in memory, so a pointer can step through them. The begin / end / ++p loop below is exactly how a vector's iterators work. Run it.
#include <iostream>
int main() {
int data[5] = {2, 4, 6, 8, 10};
int* begin = data; // points at the first element
int* end = data + 5; // points one past the last
for (int* p = begin; p != end; ++p)
std::cout << *p << " ";
std::cout << "\n";
// p[i] and *(p + i) are the same thing:
std::cout << begin[2] << " == " << *(begin + 2) << "\n";
return 0;
}
2 4 6 8 10 and then 6 == 6. Now change the loop to print the elements in reverse, from end - 1 down to begin. Why is end one past the last element instead of at it?Explore 2: new[] and delete[] (allocate and construct together)
The everyday form. new[] allocates memory and constructs every element at once; delete[] destroys them all and frees the memory. Complete this little class, which owns a buffer this way, and run it (it should print 0 1 4 9).
#include <iostream>
class IntBuffer {
public:
IntBuffer(int n) {
// TODO: allocate an array of n ints with new[], and remember n
}
~IntBuffer() {
// TODO: free the array with delete[]
}
void set(int i, int value) { /* TODO: store value at index i */ }
int get(int i) const { /* TODO: return the value at index i */ }
int size() const { /* TODO: return how many ints this holds */ }
private:
// TODO: a pointer to the array, and an int for the size
};
int main() {
IntBuffer b(4);
for (int i = 0; i < b.size(); ++i) b.set(i, i * i);
for (int i = 0; i < b.size(); ++i) std::cout << b.get(i) << " ";
std::cout << "\n"; // expect: 0 1 4 9
return 0; // b's destructor frees the memory here
}
-fsanitize=address and run again. Then remove the delete[] from your destructor and watch the sanitizer report a leak. Put it back.Explore 3: raw allocation and placement new (allocate and construct separately)
The form your vector uses. ::operator new grabs raw bytes with no constructors run; placement new constructs one object in that memory; an explicit destructor call destroys one; and ::operator delete frees the bytes. Run this as is, then change it as the prompts ask.
#include <iostream>
#include <new> // placement new
struct Noisy {
int id;
Noisy(int i) : id(i) { std::cout << "construct " << id << "\n"; }
~Noisy() { std::cout << "destroy " << id << "\n"; }
};
int main() {
// 1. Allocate RAW memory for 4 Noisy objects. NONE are constructed yet.
void* raw = ::operator new(4 * sizeof(Noisy));
Noisy* buf = static_cast<Noisy*>(raw);
std::cout << "room for 4, constructed 0 so far\n";
// 2. Construct only TWO, in place, with placement new.
new (buf + 0) Noisy(1);
new (buf + 1) Noisy(2);
// buf[2] and buf[3] are allocated but NOT constructed. Leave them alone.
// 3. Destroy the two we built by calling the destructor explicitly. No delete.
(buf + 0)->~Noisy();
(buf + 1)->~Noisy();
// 4. Free the raw bytes (this calls no destructor).
::operator delete(raw);
return 0;
}
buf + 2, print its id, and destroy it before you free.Reflect and Record
Record a short video (about 3 to 5 minutes) and upload it to YouTube as Unlisted. In it:
- Show the three programs compiling and running on your screen.
- Explain, in your own words, how a pointer walks an array, and what each of the two forms of
newanddeletedoes and how they differ. - Say why a vector needs the second memory form (the capacity versus size idea), and mention one thing that surprised you or that you are still unsure about.
This is a reflection, not a presentation. Rough is fine. There is no need to use AI, as you are graded on effort and on showing your own exploration. If you get stuck, you can say so in the video and still get the grade.
Grading
Labs are graded as complete or incomplete.
The labs together are worth 5 percent of your course grade.
How to Submit
Upload your video to YouTube as Unlisted, and submit the link (paste it into the Canvas assignment). Make sure the link is Unlisted, not Private, or we cannot view it. Make sure you do not use your phone. Produce a video with your face and the screen showing what you learned. 5 minutes is more than enough.
Appendix: Online C++ Compilers
Any of the tools below will run the reading examples in your browser, with no local setup. Remember to use #include online, not import std;.
Our course toolchain is GCC 15.2 with -std=c++26. Only Compiler Explorer lets you set that exact combination. The other tools run an older fixed compiler, which is fine for these basic examples.
- In the compiler dropdown, choose x86-64 gcc 15.2.
- In the Compiler options box, type
-std=c++26. - To run the program (not just compile it), open the Execution or Add tool > Executor pane.
- Use the Share button to get a link to your code.
- Select C++ from the language menu.
- Type your code and press Visualize Execution.
- Step forward and back to watch values change.
-std=c++26 here. That is fine for the basic examples.- Set the language (top right) to C++.
- Use the settings gear to pick the C++ standard.
- Press Run, or Debug to step through the code.
c++26. Fine for the basic examples.- Pick the newest GCC from the compiler list.
- Choose C++26 from the standard menu, or add
-std=c++26to the options. - Press Run, then Share for a link.