-2
  1. I am trying to read the return value of the pointer_new_values variable.
  2. The Dynamic Array's elements double once the array reaches it's max capacity using the growArray function.
#include <iostream>

//Using the returnValue variable in order to be able to see the return value of pointer_new_values. 
int returnValue;
int* growArray(int* pointer_values, int current_size);

int* growArray(int* pointer_values, int current_size)
{
//This dynamic array grows in size here.
    int* pointer_new_values = new int[current_size * 2];
    for (int i = 0; i < current_size; ++i)
    {
//This allows me to free memory from pointer values. Using delete. 
        pointer_new_values[i] = pointer_values[i];

    }
//Freeing the allocated memory from pointer_values.
    delete[] pointer_values;
//Returning the pointer_new_values variable, value which is equal to zero.
  *p_new_values = returnValue;
    return p_new_values;
//Here I return the value 0. 
   
// I am trying to read the pointer_new_values integer. From the int function.
}
int main()
{

    int next_element = 0;
    int size = 10;
    int* pointer_values = new int[size];
    int values;
    std::cout << "Please input a number:";
    std::cin >> values;
    while (values > 0)
    {

        if (size == next_element + 1)
        {
            //Now we implement the growArray
            pointer_values = growArray(pointer_values, size);
        }
        pointer_values[next_element] = val;
        std::cout << "Please input another number or exit." << std::endl;
        std::cin >> val;
        //Here I would like to read from the int GrowArray Function.
        std::cout << returnValue; 
//Returns the value of 0. 
        
    }
}
Doomed
  • 11
  • 7
  • 2
    `return pointer_new_values; pointer_new_values = returnValue;` -- See anything wrong with the order of these two lines? Second, the correct form is `delete []`, not just `delete`. – PaulMcKenzie Nov 20 '20 at 05:04
  • 1
    There's a growing array already present in standard C++. It's called [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) – Ted Lyngmo Nov 20 '20 at 05:13
  • I did as you said it returned 0. Thank you. – Doomed Nov 20 '20 at 05:14
  • I know vectors exist. – Doomed Nov 20 '20 at 05:14
  • 1
    @Doomed Then why not use `std::vector` for this? – Ted Lyngmo Nov 20 '20 at 05:15
  • *I know there is an alternative called vectors but I want to learn more about dynamic arrays first* -- Then create a `vector` class instead of global functions. – PaulMcKenzie Nov 20 '20 at 05:24
  • Because from what I hear is that understanding dynamic arrays shows more mastery of C++. I know it is old school also in unreal engine 4 you use dynamic arrays for linetraces or raycasting usually for collisions. – Doomed Nov 20 '20 at 05:24
  • @Doomed -- *understanding dynamic arrays shows more mastery of C++* -- Who told you that? – PaulMcKenzie Nov 20 '20 at 05:27
  • 1
    And thank you for the advice @TedLyngmo. – Doomed Nov 20 '20 at 05:33
  • My college Professors. @PaulMcKenzie – Doomed Nov 20 '20 at 05:33
  • Sorry to tell you that most college professors know little, if anything about C++. That has been demonstrated by the many homework questions that come here. – PaulMcKenzie Nov 20 '20 at 05:36
  • My professors never used vectors because of their mastery of dynamic arrays I guess they just prefer dynamic arrays or just didn't update their logs. – Doomed Nov 20 '20 at 09:50
  • How can I improve my question in order to become satisfactory? – Doomed Nov 20 '20 at 11:04

2 Answers2

1

There are couple of issues with your code,

  1. Even though array is expanded size is not updated
  2. All the elements are added to the index zero of the array
  3. In function growArray expanded array size has to be returned, no statements are executed after encountering of return
  4. delete[] has to be used inorder to release memory allocated using new[]. refer

I tried to correct some part of your code,

#include <iostream>

int* growArray(int* pointer_values, int *size);

int* growArray(int* pointer_values, int* size)
{
    int current_size = *size * 2;
//This dynamic array grows in size here.
    int* pointer_new_values = new int[current_size];
    for (int i = 0; i < *size; ++i)
    {
        pointer_new_values[i] = pointer_values[i];

    }
    delete[] pointer_values;
    
    *size = current_size;
    
    std::cout<< "grow "<<*size<<std::endl;
    
    return pointer_new_values;
}

int main()
{
    int next_element = 0;
    int size = 1;
    int* pointer_values = new int[size];
    int values;
    std::cout << "Please input a number:";
    std::cin >> values;
    while (values >= 0)
    {
        if (size == next_element + 1)
        {
            //Size is updated on expanding the array
            pointer_values = growArray(pointer_values, &size);

        }

        pointer_values[next_element++] = values;
        //              ^^Here ,elements are added to new index 

        std::cout << "Please input another number or negative value for exit." << endl;
        std::cin >> values;
    }
    
     for (int i = 0; i < next_element; ++i)
    {
       std::cout<< pointer_values[i] << " ";

    }
}
TruthSeeker
  • 1,539
  • 11
  • 24
  • 1
    Thank you for helping understand more about dynamic arrays. – Doomed Nov 20 '20 at 05:30
  • The flaw in this code is that it does a reallocation when `growArray` is called for a smaller size instead of just reusing the already allocated memory. – PaulMcKenzie Nov 20 '20 at 05:33
  • @PaulMcKenzie: I didn't understand what you mean by re-using the allocated memory. Isn't it contiguous memory location is required? – TruthSeeker Nov 20 '20 at 05:38
  • 1
    @TruthSeeker `growArray(pointer, 20); growArray(pointer, 1);` -- What operations will be done in those two successive calls? You already have memory for 20 items, so why deallocate and reallocate again for 1 item? A better implementation would have a capacity as well as a size. That's the flaw I'm referring to. – PaulMcKenzie Nov 20 '20 at 05:39
  • @Doomed, If I understand you correctly, `growArray` updates the `size` on array expansion. – TruthSeeker Nov 20 '20 at 05:40
  • @PaulMcKenzie: What you have said is correct. Understanding is that `growArray` is called only to expand the array size but not to shrink. @Doomed: please look into the use case of shrinking array and re-using the allocated memory. – TruthSeeker Nov 20 '20 at 05:42
  • @TruthSeeker Yes. – Doomed Nov 20 '20 at 05:44
  • The grow array is supposed to double the current array size. – Doomed Nov 20 '20 at 05:46
  • @TruthSeeker mind if I ask why does return pointer_new_values return 0? – Doomed Nov 20 '20 at 07:21
  • @Doomed ` return pointer_new_values` is not a returning a value but the pointer of allocated memory. – TruthSeeker Nov 20 '20 at 07:33
  • @TruthSeeker So it is only returning the memory address? – Doomed Nov 20 '20 at 07:37
  • @Doomed yes, it is. Please find good [reference books] (https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). to get the understanding of the language – TruthSeeker Nov 20 '20 at 07:52
0

I did some addition research and I made the dynamic array on much simpler and cleaner terms rather then using a function in order to double the array's size. It is probably unnecessary to return a value from a function anyway.

#include <iostream>
//Here is a simpler way of doing everything I described.
int size;
int main()
{
    int pointerAddition = 10;
    int* ten = &pointerAddition;
    do 
    {
        std::cout << "How many indexs do you want?" << std::endl;
        std::cout << "Please enter 0 to exit the loop." << std::endl;
//This allows me to allocate spaces during runtime. 
        std::cin >> size;
        int* dynamic_array = new int[size];
        std::cout << "Enter " << size << " numbers." << std::endl;
        for (int x = 0; x < size; x++) {
//Allows you to add an integer to each index of the array.
            std::cin >> dynamic_array[x];
        }
        std::cout << "You entered: " << std::endl;
        for (int x = 0; x < size; x++) {
//Prints each index of the array.
            std::cout << dynamic_array[x] << std::endl;
        }
        //Rather then using a function I settled for a simple if statment to keep it simple.
//If the size of the array is greater then or equal to 4 then double the array's size.
        if (size >= 4)
        {
            int size_doubled;
            size_doubled = size * 2;
            //Shows the size of the array doubled.
            std::cout << "The size of the array doubled." << std::endl;
            std::cout << size_doubled << std::endl;
            std::cout << "Ten has been added to the first index of the array." << std::endl;
            for (int y = 0; y < size_doubled; y++)
            {
                //Adds ten to the first index of the array.
                dynamic_array[0] += *ten;
                //Prints out the newly modified array.
                //In order to prove that the indexes have been added during runtime.
                std::cout << dynamic_array[y] << std::endl;
            }
        }
    //Deallocating memory every time the base operation is complete.
        delete[] dynamic_array;
    } while (size != 0);
    system("pause");
    return 0;
}
Doomed
  • 11
  • 7