0

I have using the following code to set the Cache Size according to the User's Input

int size=1024;
Console::WriteLine("Select the Cache Size.\n a. 1 Kb \n b. 2 Kb \n c. 4 Kb \n d. 8 Kb\n");
    String^ CACHE_SIZEoption = Console::ReadLine();
    //Char wh=CACHE_SIZEoption->ToChar();


    switch(CACHE_SIZEoption[0])
    {case 'a':{
        size= 1024;
        break;}

    case 'b':{
        size=2048;
        break;}

    case 'c':{size= 4096;
        break;}

    case 'd':{size=8192;
        break;}
    default: {Console::WriteLine("Wrong Input");}

    }

#define CACHE_SIZE size
long tags[CACHE_SIZE];

The error is produced on the last line, "long tags[CACHE_SIZE]"

expected constant expression
 cannot allocate an array of constant size 0

Please tell if there is another way to do this thing

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
Hanya Idrees
  • 453
  • 1
  • 8
  • 24

2 Answers2

3

Arrays must have a compile-time fixed sizes. As you can see, your size variable can vary at run-time depending on the value of CACHE_SIZEoption[0]. Instead, you should use a run-time sized container, such as std::vector.

std::vector<long> tags(size);

Note that your #define probably isn't doing what you expect it to. Macros are expanded in the preprocessing stage. If you use CACHE_SIZE anywhere else in your code, it will be replaced with size before your code is compiled. If there is no size variable in those places, you'll get an error. It does not set CACHE_SIZE to be the value of size at that point in your code.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • I have used this std::vector tags[size]; giving errors error C2039: 'vector' : is not a member of 'std' – Hanya Idrees May 03 '13 at 12:02
  • 2
    @HanyaIdrees You need to `#include ` and note that it's `(size)` not `[size]`. You pass the size to its constructor. – Joseph Mansfield May 03 '13 at 12:03
  • @HanyaIdrees By doing `std::vector tags(size);`. You should get a [good C++ book](http://stackoverflow.com/q/388242/150634). – Joseph Mansfield May 03 '13 at 12:10
  • @HanyaIdrees Well then you're trying to access an element that is not in your vector. That is, you're doing `tags[i]` where `i` is greater than `size-1`. – Joseph Mansfield May 03 '13 at 12:12
  • and what is my vector, i have not defined any vector individually, rather then you given line of code – Hanya Idrees May 03 '13 at 12:13
  • @HanyaIdrees Exactly. That vector. `tags` is now a `std::vector` rather than an array of `long`. It behaves like an array though, so you can still go `tags[i]`. However, you're accessing outside of its bounds, which is why you get the runtime error. – Joseph Mansfield May 03 '13 at 12:14
  • @HanyaIdrees By not accessing outside its bounds. – Joseph Mansfield May 03 '13 at 12:16
  • size has four option, as mentioned in above code, if they out of range – Hanya Idrees May 03 '13 at 12:17
  • @HanyaIdrees `size` determines how many elements your vector `tags` will have. You can't access an element that is outside this `size`. That is what you are doing to get the runtime error. For example, if `size` is `1024`, you'll get an error if you do `tags[2000]` because there is no `2000`th element. As I said, please get a good C++ book. You seem to be in over your head. – Joseph Mansfield May 03 '13 at 12:18
  • i have included more code, where it seems the error is coming, please will you see it – Hanya Idrees May 03 '13 at 12:30
  • 1
    @HanyaIdrees I have reverted your question back to its original form. Please post a new question if you want to ask about this. Changing the question makes the existing answers not make sense any more. – Joseph Mansfield May 03 '13 at 12:31
2

When you use #define that's a preprocessor directive and not part of the C++ language. The preprocessor is run before the compiler and does simple text replacements.

What your compiler will see is

long tags[size];

And that is a variable length array and is not supported in C++.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621