-8

As the title suggests,

int [] a = new int[size];

constantly throws me an error. I am not yet familiar with C++ so please help me tweak the code above to as closely similar to the syntax above (above was a given pseudo(?) code in a class) so I can create an array.

Thank you.

#include <iostream>
using namespace std;

// Test program
int main( )
{

    int [] a = new int[10];
    cout << a[0];

    return 0;
}

Tried running the above code and it failed to compile.

Jay
  • 1
  • 2
  • What did the error message say? This one is pretty helpful... https://godbolt.org/z/cosna9zrd – jtbandes Dec 01 '22 at 01:14
  • 1
    Array would be `int a[10];` dynamically allocated array would be `int *a = new int[10];` – Slava Dec 01 '22 at 01:18
  • 1
    Maybe you just want `std:vector vec(10);` which should be preferred over using new[] in modern `c++` : [https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) – drescherjm Dec 01 '22 at 01:20
  • 5
    *throws me an error* should be followed **immediately** by the **exact, complete error message**. It's on the screen right in front of you, so there is absolutely no reason for you not to include it in your post. You're asking us for free help to solve **your** problem, and it's in your best interest to make it as easy as possible for us to do so by giving us the details you already have. – Ken White Dec 01 '22 at 01:22
  • What's wrong with this line? It is invalid syntax in C++. Some other languages may do things that way, but C++ is not one of them. Different programming languages are described as different because different is what they are. – Peter Dec 01 '22 at 01:25
  • 1
    *"I am not yet familiar with C++"* -- this is a good reason to not set "closely similar to the syntax" as your goal. How do you know that your syntax is similar to good? How are we supposed to guess your intent from wrong code? Your goal should be more related to functionality, the "so I can create an array" part of your question. So I advise leading with "How do I define an array?" then presenting your attempt and associated error message. – JaMiT Dec 01 '22 at 03:50

1 Answers1

0

new is for dynamic allocation (of an array on the heap in this case), and returns a pointer to the new array.

[] in the declaration are for declaring it to be a stack array (and the brackets belong the right side of the variable name).

So the two legal approaches your code is mixing would simplify to either:

int a[10];  // Declares a stack array of size 10

or:

int *a = new int[10];  // Dynamically allocates a heap array of size 10 and stores the pointer to it in `a`

The former is the better solution here (there's no benefit to a heap allocation for such a small array that is only used within the scope of this function, and it would require explicit delete[] a; later to properly clean it up).

That said, neither version initializes the values, so the contents of a[0] are uninitialized, and reading from them will get random garbage. Both versions can be changed to initialize the data with zeroes though, e.g.:

int a[10] = {0};  // Declares a stack array of size 10 and initializes to zero

or:

int *a = new int[10]();  // Dynamically allocates a heap array of size 10 and stores the pointer to it in `a`, initializing to zero

Final note: Using raw new is generally frowned upon in modern C++. If you needed heap-allocated contiguous data storage, you're probably better off using a std::vector.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271