10 Replies - 21539 Views - Last Post: 22 August 2007 - 04:08 AM Rate Topic: -----

#1 BrainStew  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 154
  • Joined: 02-August 07

cannot allocate an array of constant size 0

Posted 21 August 2007 - 01: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.

#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:
------ 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 August 2007 - 05:48 PM


Is This A Good Question/Topic? 0
  • +

Replies To: cannot allocate an array of constant size 0

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 854
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: cannot allocate an array of constant size 0

Posted 21 August 2007 - 01:21 PM

View PostBrainStew, on 21 Aug, 2007 - 09:06 PM, said:

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.

#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.
#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

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").
Was This Post Helpful? 1

#3 BrainStew  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 154
  • Joined: 02-August 07

Re: cannot allocate an array of constant size 0

Posted 21 August 2007 - 01:29 PM

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 :)

This post has been edited by BrainStew: 21 August 2007 - 01:30 PM

Was This Post Helpful? 1

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: cannot allocate an array of constant size 0

Posted 21 August 2007 - 03: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?
Was This Post Helpful? 0
  • +
  • -

#5 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 854
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: cannot allocate an array of constant size 0

Posted 21 August 2007 - 03:50 PM

View PostAmadeus, on 21 Aug, 2007 - 11:02 PM, said:

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++
Was This Post Helpful? 0
  • +
  • -

#6 BrainStew  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 154
  • Joined: 02-August 07

Re: cannot allocate an array of constant size 0

Posted 21 August 2007 - 03:59 PM

View PostBench, on 21 Aug, 2007 - 03:50 PM, said:

View PostAmadeus, on 21 Aug, 2007 - 11:02 PM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#7 BrainStew  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 154
  • Joined: 02-August 07

Re: cannot allocate an array of constant size 0

Posted 21 August 2007 - 04:06 PM

ps: Whats best iso/ansi standard way to keep a console window open?
Was This Post Helpful? 0
  • +
  • -

#8 MorphiusFaydal  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 43
  • View blog
  • Posts: 1,376
  • Joined: 12-May 05

Re: cannot allocate an array of constant size 0

Posted 21 August 2007 - 05:40 PM

http://www.dreaminco...snippet1067.htm
http://www.dreaminco.../snippet582.htm
Was This Post Helpful? 0
  • +
  • -

#9 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: cannot allocate an array of constant size 0

Posted 21 August 2007 - 07:09 PM

View PostBrainStew, on 22 Aug, 2007 - 01:36 AM, said:

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.

#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 August 2007 - 07:09 PM

Was This Post Helpful? 0
  • +
  • -

#10 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 854
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: cannot allocate an array of constant size 0

Posted 22 August 2007 - 03:52 AM

View PostXing, on 22 Aug, 2007 - 03:09 AM, said:

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 August 2007 - 03:52 AM

Was This Post Helpful? 0
  • +
  • -

#11 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: cannot allocate an array of constant size 0

Posted 22 August 2007 - 04:08 AM

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.
Was This Post Helpful? 1

Page 1 of 1