0

Question

What is the most efficient way to add memory allocation to an already declared variable?

For example:

int n=3;
int m = 4;
int * p;
p = new int[n];
for(int i = 0;i<n;i++) {/*function that stores values in p*/}

From there, how to get to:

p = new int[n+m];

By copying

I've thought about making a new variable (y for example) with the required memory allocation (n+m) and copy the value of p to it and then copy y to p. Is that the most efficient way to do this ?

Example:

int * y;
y = new int[n + m];
for(int i = 0; i < n;i++) {y[i] = p[i];}
p = y;
delete [] y;

Related question:

Is there a purpose for memory allocation without an array?

Example:

int * p;
p = new int;

What would require this method instead of declaring int p?

Undo
  • 25,519
  • 37
  • 106
  • 129
JoGooD
  • 3
  • 2
  • Why on earth did you `delete [] y;` after assigning it? – MikeCAT Mar 03 '16 at 05:16
  • 3
    The most efficient way is to use `std::vector`, where "efficient" is about both computing and programmer effort. –  Mar 03 '16 at 05:16
  • Why would I keep both y and p if I only needed the final result ? – JoGooD Mar 03 '16 at 05:25
  • Because y and p are both pointing to the same space in memory. If you delete y, you also delete p. You need to allocate a whole new array for p, copy the contents of the array at y to the array at p, and then you can safely delete y. but there isn't much need for this. so long as p is still pointing at y's memory, y can go the way of the dodo and later you can delete p. the real problem is in deleting p's memory before you lose the pointer to it by copying y over it. – user4581301 Mar 03 '16 at 05:55

2 Answers2

0

Is that the most efficient way to do this ?

No, this is no good because you freed what to use and caused memory leak by not freeing what will be no longer used. At least it should be

int * y;
y = new int[n + m];
for(int i = 0; i < n;i++) {y[i] = p[i];}
delete [] p;
p = y;

There may be more efficient method, depending on the usage. For example, linked list may be more efficient if you don't need randam access for elements.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • So instead of using p, I should just delete p and use y from there ? (there is a mistake on the last line of your code) – JoGooD Mar 03 '16 at 05:29
  • @JoGooD No error was found in [my test](https://ideone.com/srNmWL). Please tell me the mistake you found. Which of `p` or `y` should be used may depend what you want, but I guess typically using `p` is better because it may be declared for larger scope. – MikeCAT Mar 03 '16 at 05:34
  • `delete [] p;` `p = y;` `p` is deleted then `y` is assigned to `p` – JoGooD Mar 03 '16 at 05:38
-1

I think what you want is std::vector. You can use this like an array that you can resize dynamically.

int n=3;
int m = 4;
std::vector<int> p(n);
for(int i = 0;i<n;i++) { ... }

then later

p.resize(n+m);

The memory will also freed automatically when p goes out of scope.

Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132