kiriloff
kiriloff

Reputation: 26335

c++ array initialization, good practice

I would like to be sure that this is not wrong: I initialize array with

 double* lower = input->getLowerBox();

where function getLowerBox() returns some double*. is it correct? Or shold I initialize this way::

double* lower = new double[nbP];
for (int i=0;i<nbP;i++)
     lower[i]=input->getLowerBox()[i];

or to avoid multiple calls to getLowerBox,

double* lower = new double[nbP];
double* tmp = input->getLowerBox();
for (int i=0;i<nbP;i++)
     lower[i]=tmp[i];
delete[] tmp;

Upvotes: 1

Views: 352

Answers (5)

Panzercrisis
Panzercrisis

Reputation: 4750

I would definitely go with the first one for multiple reasons. It's cleaner, it avoids unnecessary calls / variable creation, etc. Just make sure that input's a pointer if you're using "->"; otherwise use ".".

Upvotes: 0

Component 10
Component 10

Reputation: 10487

Do you have the ability to change getLowerBox() ? If so, I would change it so it returns a vector.

Depending on the implementation, it might return you a pointer which you can own or a pointer to an internal static (bad but possible) so you need to know what it's doing and act accordingly by retaining the pointer or taking a copy of the array respectively.

If you don't have control over getLowerBox() and you know the size of the array it returns, it would be a reasonable idea copy it to a vector

double* lower = input->getLowerBox(); 
vector<double> lowerV(lower, lower + N );

(where N is the size of the array - BTW this is just from memory, I haven't compiled it.)

Upvotes: 0

MGZero
MGZero

Reputation: 5963

The first way is fine. You're going to return a pointer to a double, which I'm assuming is related to an array (If not, then please correct me). By doing that, you're pointing to the first element of that array and then you can just index lower or use pointer arithmetic to access the other elements.

EDIT: Could you post the definition of getLowerBox() so it's more clear to what you're trying to do?

Upvotes: 0

Matthieu M.
Matthieu M.

Reputation: 299750

Two steps recipe:

  1. Change Input::getLowerBox() to return std::vector<double> const&
  2. Use a copy if you want to modify the returned value, and the const reference instead

Upvotes: 4

Luchian Grigore
Luchian Grigore

Reputation: 258558

Well, it depends on what you want to do. Do you need a new array or not.

Your first snippet doesn't create a new array, so memory management is more important.

For example:

double* lower = input->getLowerBox();
delete[] lower;

would possibly render input->getLowerBox() invalid. Or something like:

double* lower = NULL;
{
   Class input;
   lower = input->getLowerBox();
}
//...

would make lower a dangling pointer, if input clears the contents of the array in the destructor.

The last two snippets create new arrays. It is safer IMO, but also uses extra memory.

Both are correct, depending on what you want to do. Whichever you choose, make sure to document it fully.

Upvotes: 0

Related Questions