That's some really error-ridden code. I sure hope that is only used for teaching purposes, and even then it's so weird.
Anyway....
You're assigning a nullptr to buffer.
As soon as you hit line 13, you are dereferencing a null pointer, and that makes your program undefined behavior (mostly likely violently segfaulting/crashing).
If you want to actually be able to assign values to an array, pass in a pointer to an allocated array.
(1)
1 2
|
int size = 10;
int* buffer = new int[size];
|
or
(2)
1 2
|
const int size = 10; // needs to be const to be used with a static array
int buffer[size];
|
If you use (1), which is probably what your assignment is looking for, then you need to use delete[].
If you use (2), then you shouldn't use delete[].
Correct,
only delete something created with new.
only delete[] something created with new[].
Also, you don't need to check size every iteration, you can check that at the beginning of the get_data function.