C++ How to create a dynamic array of integers using new Keyword ?
Its Not working a Getting a syntax Error C2061
Sudantha
2010-10-27 04:05:59
I knew int.....
aaa
2010-10-27 04:06:05
semicolon at the end?
Dima
2010-10-27 04:08:50
You're probably missing the colon, or haven't replaced SIZE with an *actual* size.
Montdidier
2010-10-27 04:08:55
Because the syntax is wrong.
Ed Swangren
2010-10-27 04:09:22
Love the snarky attitude considering you didn't even get the syntax right.
Ed Swangren
2010-10-27 04:10:07
Snarky attitude seems to have been edited away.
Ed Swangren
2010-10-27 04:11:05
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
2010-10-27 04:29:46
@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
2010-10-27 04:45:31
@PigBen Hukahan :D
Sudantha
2010-10-27 04:46:51
+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
2010-10-27 04:06:44
I won't -1, but if you can even possibly forget to `delete`, your code is wrong.
GMan
2010-10-27 04:32:50
+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
2010-10-27 04:12:51
@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
2010-10-27 04:23:13
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
2010-10-27 04:52:31
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
2010-10-27 04:55:47
@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
2010-10-27 05:01:19
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
2010-10-27 05:44:24
@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
2010-10-27 05:48:17
@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
2010-10-27 05:50:32
@GMan: I assume you're talking about separation of concerns in a design? Otherwise I'm not sure what you're talking about.
Montdidier
2010-10-27 06:27:37
@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
2010-10-27 06:35:11
Sorry about that. No idea where my head was. I should have provided an example.
John L Veazey
2010-10-27 07:17:30
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
2010-10-27 17:21:24