memory allocation & illegal access


In a practice question, we had to give examples of three different types of wrong memory access in C or C++:

Which I identified as the following:

• Reading/Writing without allocation

• Read/Writing after de-allocation

• Reading/Writing without previous write

Since I haven’t done much work in these languages before, I couldn’t come up with appropriate examples. For example, I understand that we deallocate in C like this:

int main(void)  {      int *ptr = (int*)malloc(10);       return 0;  }  

but how could I modify it to show an example of the second type?

Similarly, for the first type, I think this should suffice, but does it?

void foo() {    int i = 5;    MyClass obj;  ... }