1 of 31

Lab_conway

CS225 - Data Structures

2 of 31

If we run the program it may or may not complete without errors.

int main(){

int * arr = new int[100];

return arr[100];

}

1

2

3

4

What happens if we run valgrind? �valgrind ./main

3 of 31

Valgrind - a programming tool for memory debugging, memory leak detection, and profiling. 

We will always check for memory errors and leaks on your assignments unless specified

4 of 31

int main(){

int * arr = new int[100];

return arr[100];

}

1

2

3

4

Example 1

What is wrong with this code?

5 of 31

6 of 31

int main(){

int * arr = new int[100];

return arr[100];

}

1

2

3

4

7 of 31

Out-of-bounds access to heap, stack, and globals. This error occurs when you allocate some memory and then try to access a region outside your allocated space.

int main(){

int * arr = new int[100];

return arr[100];

}

1

2

3

4

8 of 31

int main(){

int * arr = new int[100];

return arr[100];

}

1

2

3

4

9 of 31

int main(){

int x;

cout << x << endl;

}

1

2

3

4

Example 2 - Use of an uninitialized value.

int main(){

int * x = new int;

delete x;

delete x;

}

1

2

3

4

5

Example 3 - Invalid free error

10 of 31

Destructor

  • Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed. �
  • A destructor is called for a class object when that object passes out of scope or is explicitly deleted.

class Animal{

Animal();

~Animal();

}

Animal::Animal(){}

Animal::~Animal(){}

Animal.cpp

Animal.h

11 of 31

class A{

private:

int *n;

public:

A(int n1);

~A();

};

A::A(int n1){

n = new int(n1);

}

A::~A(){

delete n;

}

How will you change destructor if n was array of integers?

12 of 31

class A{

private:

int *n;

public:

A(int n1);

~A();

};

A::A(int n1){

n = new int(n1);

}

A::~A(){

delete n;

}

How will you change destructor if n was array of integers?

delete[]

13 of 31

int main(){

int * x = new int[6];

delete x;�}

1

2

3

4

Example 4 - Mismatched free() / delete / delete []

14 of 31

int main(){

int * arr = new int[10];

int * x = new int;

int * y;

arr[0] = *y;

delete arr;

delete x;

delete y;

return 0;

}

1

2

3

4

5

6

7

8

9

10

Spot the errors!

15 of 31

int main(){

int * arr = new int[10];

int * x = new int;

int * y;

arr[0] = *y; // y not initialized

delete arr; // Wrong delete, should be delete[] arr

delete x;

delete y; // Should not delete, not on heap return 0;

}

1

2

3

4

5

6

7

8

9

10

Spot the errors (Solution)

16 of 31

Tip: Valgrind output can get long

If necessary pipe the output of Valgrind to a file

valgrind ./exec &> log.txt

17 of 31

Tip: Read the Doxygen before beginning!

Make sure you understand what the code base is supposed to do!

The Snapshot class handles the visualization of a single frame of Game

18 of 31

Tip: Read the Doxygen before beginning!

Make sure you understand what the code base is supposed to do!

The Game class steps through different rounds, producing Snapshots

19 of 31

Once finished, post your animations on Discord!

20 of 31

(Optional) Conway Game of Life Rules

21 of 31

(Optional) Conway Game of Life Rules

Note: In the following examples, assume we are in an infinite empty grid

22 of 31

(Optional) Conway Game of Life Rules

23 of 31

(Optional) Conway Game of Life Rules

24 of 31

(Optional) Conway Game of Life Rules

25 of 31

(Optional) Conway Game of Life Rules

26 of 31

(Optional) Conway Game of Life Rules

27 of 31

(Optional) Conway Game of Life Rules

28 of 31

(Optional) Conway Game of Life Rules

29 of 31

(Optional) Conway Game of Life Rules

30 of 31

(Optional) Conway Game of Life Rules

31 of 31