For example i have class with constructor that has array of ints as parameter:
A(int* array) : m_array(array) {}
I can use it like this:
int array[] = { ... }
A a(array);
Or like this:
int* array = new int[10];
A a(array);
If object then use it array, it must (or may be not?) delete it in desctructor (if it was dynamic). But how he will know, that memory for this array was allocated dynamically?
You can’t know if it’s dynamically allocated or not, because after all,
int* arrayis anintpointer, not an array. You might as well pass:As you can imagine, bad things will happen if you try to
delete[]that one, or try to accessm_array[N]with N > 0.So you have to rely on the caller to do the right thing; there’s nothing you can do to verify or enforce it. All you have is the address of an
int. Who created thatint, or how or whether moreints follow after it, will be unknown.If you want more safety, use an
std::vector. This is what it was made for.