0

I am trying to create an array, which doubles every time it is completely filled.

#include <iostream>
using namespace std;

int* array_doubler(int* array, int size){
    int *new_array = new int[size*2];

    for(int i = 0; i < size; i++){
        new_array[i] = array[i];
    }

    delete[] array;
    return new_array;
}


int main()
{
    int N = 10; // Size of array to be created
    int *array = new int[0];

    for(int i = 0; i < N; i++)
    {
        if(array[i] == '\0')
            array = array_doubler(array, i);
        array[i] = i*2;
    }

    //Printing array elemensts
    for(int i = 0; i < N; i++)
        cout << array[i] << '\t';

    cout << '\n';
    return 0;
}

Problem is when I create dynamic memory with new, all the spots have the null character \0 value in them (not just the last spot). i.e. If i write:

int* p = new int[5];

then all the 5 blocks in memory p[0],p[1],p[2],p[3],p[4],p[5] have \0 in them, not just the p[5]. So the if(array[i] == '\0') in my main() calls array_doubler for every single iteration of for loop. I want it to fill the available spots in the array first and when it reaches the last element, then call array_doubler.

3
  • 1
    Looks like new int[0] should be new int[N]. Edit : Never mind, that I see that it would defeat the purpose of your example. Though, you cannot have a 0 sized array in C++. Commented Jul 15, 2020 at 13:14
  • "Problem is when I create dynamic memory with new, all the spots have the null character" That is not correct, the array is uninitialized. Those elements have indeterminate values and if you observe them to all be 0, then that is just a coincidence. Commented Jul 15, 2020 at 13:16
  • No I checked, when I make int *p = new int[10] and then if i write for(i =0 to 10) if (p[i] == '\0') cout << "Yes"; ; It prints Yes 10 times. In fact the memory I don't even allocate does this, i.e. p[11], p[12], p[53], p[100] all have \0 in them. Commented Jul 15, 2020 at 13:42

2 Answers 2

2

Problem is when I create dynamic memory with new, all the spots have the null character \0 value in them (not just the last spot).

Actually they have undefined values in them. 0 is a valid value for them to have, but tomorrow the compiler might suddenly decide that they should all have 1 instead of 0.

If you want to detect the end of an array, then you have to remember how big the array is. C++ doesn't do it for you. Actually, it does do it for you if you use std::vector, but I suppose that's not the point of this exercise.

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

Comments

2

I'm not sure why you'd want to do this, as std::vector offer this kind of feature, and are more idiomatic of c++ (see isocpp faq on why C-style array are evil).

One of the issue of C-style array is the fact that they don´t know their own size, and that they don't have default value, thus stay uninitialized.

If for some reason you need to not use std::vector, the next best solution would be to wrap the array with it's size in a structure or a class (which is kinda what std::vector is doing), or to initialize your array using std::memset (which is the C function you would use if you were in C).

Do keep in mind that this is not considered as good practices, and that the STL offer plenty of solution when you need containers.

Comments

Your Answer

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