C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 307,177 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,582 people online right now. Registration is fast and FREE... Join Now!




cannot allocate an array of constant size 0

 

cannot allocate an array of constant size 0, init array to rand size

BrainStew

21 Aug, 2007 - 12:06 PM
Post #1

D.I.C Head
**

Joined: 2 Aug, 2007
Posts: 154



Thanked: 2 times
My Contributions
I think I'm going crazy... I'm trying to initialize an array to a random size between 1 and 20. I must be too tired or something.

CODE

#include <iostream>
#include <ctime>

int main(void)
{
    srand((unsigned)time(NULL));

    int iRand_Num = rand()%20+1;
    int iArray[iRand_Num] = {0};

    system("pause");
}


Errors:
CODE

------ Build started: Project: Sort Array, Configuration: Debug Win32 ------
Compiling...
main.cpp
c:\documents and settings\james\desktop\sort array\sort array\main.cpp(9) : error C2057: expected constant expression
c:\documents and settings\james\desktop\sort array\sort array\main.cpp(9) : error C2466: cannot allocate an array of constant size 0
Build log was saved at "file://c:\Documents and Settings\James\Desktop\Sort Array\Sort Array\Debug\BuildLog.htm"
Sort Array - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Thanks... I'm going to go jump off my roof now.

This post has been edited by skyhawk133: 21 Aug, 2007 - 04:48 PM

User is offlineProfile CardPM
+Quote Post

 
Reply to this topicStart new topic
Replies(1 - 10)

Bench

RE: Cannot Allocate An Array Of Constant Size 0

21 Aug, 2007 - 12:21 PM
Post #2

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 907



Thanked: 76 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(BrainStew @ 21 Aug, 2007 - 09:06 PM) *

I think I'm going crazy... I'm trying to initialize an array to a random size between 1 and 20. I must be too tired or something.

CODE

#include <iostream>
#include <ctime>

int main(void)
{
    srand((unsigned)time(NULL));

    int iRand_Num = rand()%20+1;
    int iArray[iRand_Num] = {0};

    system("pause");
}

Thanks... I'm going to go jump off my roof now.
The size of a statically allocated array must be constant i'm afraid.

You have two options - the first (preferable) is to forget arrays, because 'C' style arrays are a bit of a nuisance, and go with a vector from the C++ STL (standard template library). A vector is basically a C++ array, although it automatically resizes itself to suit the number of elements you wish to add to it, and has accessor methods with bounds checking.
CODE
#include <iostream>
#include <vector>
#include <ctime>
#include <cmath>

int main()
{
    srand( time(0) );
    int size = rand() % 20 + 1;
    std::vector<int> my_vec(size);
    std::cout << my_vec.size();
}


The other way (less preferable, but if you insist on having a raw C-style array..) is to create it dynamically with new

CODE
int rand_num = rand() %20 + 1;
int* arr = new int[rand_num];
Doing this, you must remember to delete [] the array when you're done with it, else your program will have a memory leak. (The great thing about vectors is that they clean up after themselves and don't "leak").
User is offlineProfile CardPM
+Quote Post

BrainStew

RE: Cannot Allocate An Array Of Constant Size 0

21 Aug, 2007 - 12:29 PM
Post #3

D.I.C Head
**

Joined: 2 Aug, 2007
Posts: 154



Thanked: 2 times
My Contributions
No I need to use a raw c array. In a number of C++ equivilency tests I keep seeing it asks you to sort an array. I was just goofing around with sorting arrays, blah blah blah.

Yeah I know STLs are a lot better but can't use them for what I was doing.

I'll just go with the pointer, didn't want to but meh.

Thanks smile.gif

This post has been edited by BrainStew: 21 Aug, 2007 - 12:30 PM
User is offlineProfile CardPM
+Quote Post

Amadeus

RE: Cannot Allocate An Array Of Constant Size 0

21 Aug, 2007 - 02:02 PM
Post #4

g+ + -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 13,284



Thanked: 149 times
Dream Kudos: 25
My Contributions
Although I'm sure you're aware, the use of the system function to call a pause.exe reduces the portability of your code. I'm assuming you've placed it there to hold an execution window open?
User is offlineProfile CardPM
+Quote Post

Bench

RE: Cannot Allocate An Array Of Constant Size 0

21 Aug, 2007 - 02:50 PM
Post #5

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 907



Thanked: 76 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(Amadeus @ 21 Aug, 2007 - 11:02 PM) *

Although I'm sure you're aware, the use of the system function to call a pause.exe reduces the portability of your code. I'm assuming you've placed it there to hold an execution window open?

Indeed - and for anyone using Visual C++ (Which is what it looks like, based on the debugger output shown), its unnecessary, because [CTRL] + [F5] .. 'a.k.a.' Start without debugging automatically pauses the console output window from VC++
User is offlineProfile CardPM
+Quote Post

BrainStew

RE: Cannot Allocate An Array Of Constant Size 0

21 Aug, 2007 - 02:59 PM
Post #6

D.I.C Head
**

Joined: 2 Aug, 2007
Posts: 154



Thanked: 2 times
My Contributions
QUOTE(Bench @ 21 Aug, 2007 - 03:50 PM) *

QUOTE(Amadeus @ 21 Aug, 2007 - 11:02 PM) *

Although I'm sure you're aware, the use of the system function to call a pause.exe reduces the portability of your code. I'm assuming you've placed it there to hold an execution window open?

Indeed - and for anyone using Visual C++ (Which is what it looks like, based on the debugger output shown), its unnecessary, because [CTRL] + [F5] .. 'a.k.a.' Start without debugging automatically pauses the console output window from VC++


didn't know about the ctrl+f5, thanks. Learn new tricks every day.
User is offlineProfile CardPM
+Quote Post

BrainStew

RE: Cannot Allocate An Array Of Constant Size 0

21 Aug, 2007 - 03:06 PM
Post #7

D.I.C Head
**

Joined: 2 Aug, 2007
Posts: 154



Thanked: 2 times
My Contributions
ps: Whats best iso/ansi standard way to keep a console window open?
User is offlineProfile CardPM
+Quote Post

MorphiusFaydal

RE: Cannot Allocate An Array Of Constant Size 0

21 Aug, 2007 - 04:40 PM
Post #8

D.I.C Lover
Group Icon

Joined: 12 May, 2005
Posts: 1,356



Thanked: 36 times
Expert In: Hardware, Networking

My Contributions
http://www.dreamincode.net/code/snippet1067.htm
http://www.dreamincode.net/code/snippet582.htm
User is offlineProfile CardPM
+Quote Post

Xing

RE: Cannot Allocate An Array Of Constant Size 0

21 Aug, 2007 - 06:09 PM
Post #9

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 15 times
Dream Kudos: 1575
My Contributions
QUOTE(BrainStew @ 22 Aug, 2007 - 01:36 AM) *

I think I'm going crazy... I'm trying to initialize an array to a random size between 1 and 20. I must be too tired or something.

CODE

#include <iostream>
#include <ctime>

int main(void)
{
    srand((unsigned)time(NULL));

    int iRand_Num = rand()%20+1;
    int iArray[iRand_Num] = {0};

    system("pause");
}


C++ doesn't support VLA's. That's a C99 feature. Follow what Bench is saying. You can further refine his second solution by using smart pointers(like auto_ptr, boost::shared_ptr etc).

This post has been edited by Xing: 21 Aug, 2007 - 06:09 PM
User is offlineProfile CardPM
+Quote Post

Bench

RE: Cannot Allocate An Array Of Constant Size 0

22 Aug, 2007 - 02:52 AM
Post #10

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 907



Thanked: 76 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(Xing @ 22 Aug, 2007 - 03:09 AM) *

You can further refine his second solution by using smart pointers(like auto_ptr, boost::shared_ptr etc).
No, you can't do that - auto_ptr is not designed to work with new[] because it doesn't use delete[] - it uses plain old delete (in other words its undefined behaviour). I believe the same holds true for boost::shared_ptr, but I'm not 100% sure on that.

The reason auto_ptr doesn't need to work with new[] is that the STL has the vector class, which is intended to be used instead.

This post has been edited by Bench: 22 Aug, 2007 - 02:52 AM
User is offlineProfile CardPM
+Quote Post

Xing

RE: Cannot Allocate An Array Of Constant Size 0

22 Aug, 2007 - 03:08 AM
Post #11

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 15 times
Dream Kudos: 1575
My Contributions
While giving general advice on using smart pointers over raw pointers I by mistakenly extended it to auto_ptr for dynamically allocated arrays. You are right about the point that there is nothing like auto_ptr for dynamically allocated arrays in C++. But boost::shared_ptr, boost::scoped_array can be used for that.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 11/21/09 07:35PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month