diff --git a/labs/04_memory_debugging/README.md b/labs/04_memory_debugging/README.md index 0b66373..1f067fc 100644 --- a/labs/04_memory_debugging/README.md +++ b/labs/04_memory_debugging/README.md @@ -9,18 +9,11 @@ - Following the conventions used in Data Structures lecture for memory diagramming, draw a picture of the stack and the heap that result from executing the statements below. Use a ‘?’ to represent uninitialized values. -```cpp -char*** carrot; -char** broccoli; -char* tomato; -char radish = 'q'; -tomato = new char; -*tomato = 'z'; -broccoli = new char*; -*broccoli = tomato; -carrot = new char**; -*carrot = broccoli; -``` +[Program 1](fruits.cpp) +[Program 2](grains.cpp) +[Program 3](desserts.cpp) +[Program 4](veggies.cpp) +[Program 5](protein.cpp) Show your diagram to your teammate, and ask the teammate to reverse engineer code for your diagram; at the same time, you reverse engineer code for your teammate's diagram. diff --git a/labs/04_memory_debugging/dessert.cpp b/labs/04_memory_debugging/dessert.cpp new file mode 100644 index 0000000..7adc7a9 --- /dev/null +++ b/labs/04_memory_debugging/dessert.cpp @@ -0,0 +1,10 @@ +bool** cake; +bool pie; +bool fudge; +pie = true; +cake = new bool*[5]; +cake[1] = &pie; +bool* donut = new bool; +*donut = false; +cake[2] = donut; +cake[4] = &fudge; diff --git a/labs/04_memory_debugging/fruits.cpp b/labs/04_memory_debugging/fruits.cpp new file mode 100644 index 0000000..be8a768 --- /dev/null +++ b/labs/04_memory_debugging/fruits.cpp @@ -0,0 +1,10 @@ +int pear = 3; +int* apple; +int banana[pear]; +int* orange; +apple = new int[pear]; +orange = &banana[1]; +apple[0] = 6; +apple[1] = 7; +apple[2] = 8; +*orange = 5; diff --git a/labs/04_memory_debugging/grains.cpp b/labs/04_memory_debugging/grains.cpp new file mode 100644 index 0000000..32dfed6 --- /dev/null +++ b/labs/04_memory_debugging/grains.cpp @@ -0,0 +1,10 @@ +float* oat[3]; +oat[1] = new float; +*oat[1] = 3.14; +oat[2] = new float; +*oat[2] = 6.02; +float rice; +float* wheat; +wheat = oat[2]; +float** barley = new float*; +*barley = oat[1]; diff --git a/labs/04_memory_debugging/protein.cpp b/labs/04_memory_debugging/protein.cpp new file mode 100644 index 0000000..fb179f8 --- /dev/null +++ b/labs/04_memory_debugging/protein.cpp @@ -0,0 +1,11 @@ +int tofu = 3; +int chicken = 2; +double** fish = new double*[tofu]; +for (int beef = 0; beef < tofu; beef++) { + fish[beef] = new double[chicken]; +} +fish[0][0] = 1.41421; +fish[0][1] = 1.61803; +fish[1][0] = 2.71828; +fish[1][1] = 3.14159; +fish[2][0] = 6.02214; diff --git a/labs/04_memory_debugging/veggies.cpp b/labs/04_memory_debugging/veggies.cpp new file mode 100644 index 0000000..ad75f22 --- /dev/null +++ b/labs/04_memory_debugging/veggies.cpp @@ -0,0 +1,10 @@ +char*** carrot; +char** broccoli; +char* tomato; +char radish = 'q'; +tomato = new char; +*tomato = 'z'; +broccoli = new char*; +*broccoli = tomato; +carrot = new char**; +*carrot = broccoli;