-2

I've been searching through past discussions on this topic and I understand that arrays need to have a constant value, but I'm assigning it a constant value through a variable, and it is not working. I need help with this logic. The problem I believe is in my function declaration. The Error messasge is"Expression must have a constant value." Here's the code...

// Jason Delgado
// 10/13/16 ©
// Chapter 9: Array Expander

// This program has a function that doubles the size of an array.
// It copies the contents of the old array to the new array, and
// initializes the unused elements to 0. The function then 
// returns a pointer to the new array. Pointer notation
// must be used for the function, and within the function.

#include <iostream>
using namespace std;

// Function protoype
int *arrExpander(int[] , int);

int main()
{
    // Create and initialize and array.
    const int SIZE = 6;         // Number of elements the array is to hold
    int oArr[SIZE] = { 10, 28, 34,
    5, 18 };                        // The original array

    int *nArr = nullptr;            // A pointer to hold the return value of the function.

// Call the arrExpander function and store it in nArr.
nArr = arrExpander(oArr, SIZE);

// Display the results
cout << "Original Array: ";
for (int count = 0; count < (SIZE); count++)
    cout << oArr[count] << " ";
cout << "\n\nNew Array: ";
for (int count = 0; count < (SIZE * 2); count++)
    cout << *(nArr + count) << " ";

system("pause");
return 0;

}

int *arrExpander(int arr[], int size)
{
    int *ptr = arr;
    const int DSIZE = size * 2;     // Doubles the size parameter
    int newArray[DSIZE];



}
  • The problem is that your code should be using `new` to allocate the arrays. This has nothing to do with declarations. – Sam Varshavchik Oct 13 '16 at 22:42
  • How do you think newArray[DSIZE] has any effect on the rest of your code? – Captain Giraffe Oct 13 '16 at 22:44
  • @SamVarshavchik That is a rare suggestion. – Captain Giraffe Oct 13 '16 at 22:46
  • @CaptainGiraffe I havent finished writing the rest of my code for the function. It won't let me use newArray[DSIZE]. This is my issue. – Jason Delgado Oct 13 '16 at 22:48
  • "it is not working." And yet you don't bother showing the error message... – John3136 Oct 13 '16 at 22:49
  • @John3136 Very good point. I thought I had it in there. I appologize. I edited it to display the error message.. It says "Expression must have a constant value." – Jason Delgado Oct 13 '16 at 22:52
  • A "constant value" for purposes of defining a C-style array requires more than being marked `const`. It has to be a **compile-time** constant, not something that gets calculated at runtime. Yet another reason for using `new` to allocate the arrays... – Pete Becker Oct 13 '16 at 22:55
  • Check out this question to give you a hint to the answer here: http://stackoverflow.com/questions/4029870/how-to-create-a-dynamic-array-of-integers – Travis Gockel Oct 13 '16 at 23:00
  • @JasonDelgado `int newArray[DSIZE];` VLA's aren't valid standard c++. `DSIZE` can't be deduced as a `constexpr` at compile time. – πάντα ῥεῖ Oct 13 '16 at 23:18

1 Answers1

0

The value assigned to

const int DSIZE = size * 2;

cannot be changed after it is created, but it depends on non-constant value size which can, and given the name of the function probably should, be different on every call to arrExpander.

In

int newArray[DSIZE];

The compiler demands that the size of the array, DSIZE, be known and constant when the program is compiled so that it can size the stack and optimize the code. If the value can be changed on every call to the function, DSIZE constant and known at compile time, so the compiler rightly rejects this code as invalid.

Some compilers allow this sort of allocation, a Variable Length Array, but it is not standard and cannot be expected on all compilers. Due to a number of downsides and safer solutions it is unlikely that support will be added to the Standard. The general stance on Variable Length Arrays is "Why bother? Use std::vector instead."

This also has the smell that this code is intended to increase the size of an existing array. It can't. newArray is a local variable and will be destroyed on returning from the function leaving you with a "dangling pointer" a reference to memory that is no longer valid. If you wish this memory to persist either use and return a std::vector or allocate newArray as a dynamically allocated pointer with new[] and remember to delete[] the array when it is no longer required.

See std::unique_ptr for a memory management helper.

user4581301
  • 33,082
  • 7
  • 33
  • 54