-2

Using dynamic memory, I am trying to make a class that stores numbers in a dynamic array (so 123 would be arr[0] = 1, arr[1] = 2, arr[2] = 3) and be able to append digits (e.g. if the number stored is 123, you could add more digits.. 45 and the new number would be 12345).

Here's my code so far: How would I go about making an append function?

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
    int *exampleArray; //new array into exsistence 

    exampleArray = new int[5]; // dynamically allocates an array of 5 ints
    for (int i = 1; i < 5; i++)
    {
        exampleArray[i] = i;
        cout << exampleArray[i] << endl;
    }

    delete exampleArray; // deleted from exsistence

    system("pause"); // to show the output
    return 0;
}
tda
  • 241
  • 1
  • 4
  • 16
  • 3
    what do you mean by "*append*"? Where do you intend to add digits? Think about checking if the currently allocated size is enough and resize + copy the array. Or even better - use `std::vector` instead of dynamic `int` array – Fureeish Oct 09 '17 at 21:05
  • 3
    You also have to [`delete []` what you `new []`](https://stackoverflow.com/q/1553382/10077). – Fred Larson Oct 09 '17 at 21:06
  • There is no guarantee than an additional memory allocation will allocate the memory at the end of the original array. – Thomas Matthews Oct 09 '17 at 21:13
  • 1
    What about using a `std::vector`?? – user0042 Oct 09 '17 at 21:14
  • 2
    The process for expanding or appending to an array, is to 1) Dynamically allocate a *new* larger array; 2) Copy old elements to new array; 3) delete old array. Many of us prefer to use `std::vector` which does this for us. – Thomas Matthews Oct 09 '17 at 21:15
  • @ThomasMatthews _"Many of us prefer ..."_ Some stubborn high school/university teachers still deny though :P – user0042 Oct 09 '17 at 21:17

2 Answers2

4

If you allocate an array with new[], the only way to "append" to it is to new[] a new array of larger size, copy the existing values from the old array into it, and then delete[] (not delete) the old array and update your array pointer to point at the new array.

Also, note that arrays are 0-indexed. Your loop is not populating exampleArray[0] with any data.

For example:

int *arr = new int[3];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;

...

int *newarr = new int[5];
std::copy(arr, arr+3, newarr);
newarr[3] = 4;
newarr[4] = 5;
delete[] arr;
arr = newarr;

...

delete[] arr;

You can optimize this a little by pre-allocating more memory than you actually need, and only "grow" when you actually exceed that memory. For example:

int *arr = NULL;
int num = 0, cap = 0;

void append(int digit)
{
    if (num == cap)
    {
        int *newarr = new int[cap + 10];
        std::copy(arr, arr+num, newarr);
        delete[] arr;
        arr = newarr;
        cap += 10;
    }

    arr[num] = digit;
    ++num;
}

...

append(1);
append(2);
append(3);

...

append(4);
append(5);

...

delete[] arr;

That being said, what you are asking for would be best handled using std:vector instead. It is a dynamic-length container that handles these ugly details for you.

For example:

std::vector<int> arr;

void append(int digit)
{
    arr.push_back(digit);
}

...

append(1);
append(2);
append(3);

...

append(4);
append(5);

...
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

An alternative solution that would not require you to use the std::vector container or any additional pointers would be to use the malloc()/realloc()/free() family of functions in the cstdlib C++ header as follows:

int *exampleArray;
exampleArray = (int *) malloc(5 * sizeof(int));
for (int i = 1; i < 5; i++)
{
   exampleArray[i] = i;
   cout << exampleArray[i] << endl;
}
// Now to add further elements
exampleArray = (int *) realloc(7 * sizeof(int)); // Added space for 2 new elements
for (int i = 5; i < 7; i++) {
   exampleArray[i] = i;
   cout << exampleArray[i] << endl;
}
free(exampleArray);
hecate
  • 620
  • 1
  • 8
  • 33