tags:

views:

124

answers:

5

C++ How to create a dynamic array of integers using new Keyword ?

A: 
int* array = new int[SIZE]
Montdidier
Its Not working a Getting a syntax Error C2061
Sudantha
I knew int.....
aaa
semicolon at the end?
Dima
You're probably missing the colon, or haven't replaced SIZE with an *actual* size.
Montdidier
Because the syntax is wrong.
Ed Swangren
Love the snarky attitude considering you didn't even get the syntax right.
Ed Swangren
Snarky attitude seems to have been edited away.
Ed Swangren
Why should there be a semi-colon? It's not a complete statement. There might be more declarations. This, if included in a complete program, does what the OP asked.
PigBen
@PigBen : clearly the OP is a newbie and best is to give him what he can digest and what he is asking. Saying there can be many more declarations will just end up confusing him more. @Sudanta : You need to read up a little more in the books, atleast the basics before posting a q here. Reading first and if you dont understand asking q's will be much more helpful.
Als
@PigBen Hukahan :D
Sudantha
+7  A: 
int main()
{
  int size;

  std::cin >> size;

  int *array = new int[size];

  delete [] array;

  return 0;
}

Don't forget to delete every array you allocate with new.

Jason Iverson
I won't -1, but if you can even possibly forget to `delete`, your code is wrong.
GMan
A: 

It's all right here: http://www.cplusplus.com/doc/tutorial/dynamic/

muntoo
I had to resist posting this: http://tinyurl.com/3256l6q
muntoo
@muntoo: cool ;)
Als
A: 
int* array = new int[size];
Ed Swangren
Factually correct, but a bad practice. :/
GMan
It just answers the question.
Ed Swangren
+3  A: 

You might want to consider using the Standard Template Library . It's simple and easy to use, plus you don't have to worry about memory allocations.

http://www.cplusplus.com/reference/stl/vector/vector/

int size = 5;                    // declare the size of the vector
vector<int> myvector(size, 0);   // create a vector to hold "size" int's
                                 // all initialized to zero
myvector[0] = 1234;              // assign values like a c++ array
John L Veazey
Ok, but answering the actual question is nice too.
Ed Swangren
@Ed, the restriction in the question seems rather arbitrary. `std::vector` with the appropriate constructor works really well and should be pointed out as an alternative. Sometimes people ask the question poorly, and this might count as one of those cases - it's very brief and doesn't give any rationale for preferring `new`.
Mark Ransom
@Ed: There's no reason to use `new[]` instead of `std::vector`.
GMan
Gman.. On windows ce devices STL is huge.. You take a big hit for using it. Actually that would hold true for several embedded devices, where an extra meg on your app means your user gets hours less use between data dumps.
baash05
Oh.. also.. I believe STL uses a combination of linked list and array to accomplish the vector. The memory limits of embedded devices would take a hit in that respect too. NOTE: There is never a reason to say never a reason :) There are two reasons in as many minutes :)
baash05
@baash: In C++, there's never a reason. If you decide, for whatever reason, to "remove the standard library", you aren't programming in C++ anymore. My comment is for the C++ language, not the C++-language-we-use-on-my-device. Even so, you shouldn't need to `delete` anything manually, ever. Also, `std::vector` is a dynamic array, and does nothing with a linked list. It's just a wrapper around a chunk of memory.
GMan
I'm not saying my device.. I'm saying that SLT is a bit expensive. And that though it's a standard c++, it's not always practical on all platforms. You never create anything dynamically? If you do, how do you get rid of it.. (I learn every day)
baash05
We argue of this homework assignment :)
baash05
@GMan: Following your argument I could equally say - Using new and delete is part of the C++ language and not using it is not programming in C++. Surely it just depends on what you want to achieve?
Montdidier
@Montdidier: No. You still use `new` and `delete` to implement wrappers. The point is you don't manage a resource **and** use it, you do one or the other.
GMan
@GMan: I assume you're talking about separation of concerns in a design? Otherwise I'm not sure what you're talking about.
Montdidier
@Montdidier: Let's start with the claims: In C++, all instances of `new[]` can be replaced with `std::vector`. And because `std::vector` correctly separates resource management from resource usage (SBRM), we should do so.
GMan
Sorry about that. No idea where my head was. I should have provided an example.
John L Veazey
Ok, point was, show him how to do what he has asked, and then suggest better alternatives. The fact that you should be using std::vector does not mean you shouldn't understand how the rest of the language works.
Ed Swangren