2

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?

3
  • AFIK you need to handle this by hand in your code. There is no automatically way to detect that. Maybe you could check this with its memory address but ALSR will prevent this. Commented Nov 25, 2012 at 11:10
  • This is a perfect example of why one should strive to avoid C-arrays in the first place. Use std::vector<> or std::array<> and the question becomes moot. Commented Nov 25, 2012 at 11:11
  • What is the declaration of m_array? Commented Nov 25, 2012 at 11:12

4 Answers 4

2

You can't know if it's dynamically allocated or not, because after all, int* array is an int pointer, not an array. You might as well pass:

int i;
A a(&i);

As you can imagine, bad things will happen if you try to delete[] that one, or try to access m_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 that int, or how or whether more ints follow after it, will be unknown.

If you want more safety, use an std::vector. This is what it was made for.

Sign up to request clarification or add additional context in comments.

Comments

0

You're initializing the array in the constructor, so it will always be initialized. It is pre-defined in the code that it will be allocated. If there are other constructors that do not allocate your array, you will need to do this check.

By the way, this is under the assumption that the array you are allocating is a member of the class. If you are assigning it to a new stack variable within the constructor, you won't be able to delete it in the destructor.

Comments

0

From what i understand you are trying to ask is whether the destructor will free the memory you have allocated to the array.

No, the memory you have allocated using new will have to be deleted by you either in the destructor or somewhere else the pointer is in scope as your memory allocation is not inside the constructor but outside.

Comments

0

You cannot know what it was because a static array decays into a pointer as well.

Basically you just need the value of the array passed to the constructor. You need not know whether it was a dynamically allocated or statically allocated array. What matters is the data member array which is part of the interface of your class and into which you are copying the data. Responsibility of the array passed to the constructor as an argument should rest with the caller regarding its deletion and lifetime.

It would make your life easier if you use std::vector instead of raw array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.