views:

82

answers:

4

I was working on a program in Netbeans on Linux using a gcc compiler when, upon switching to Visual C++ on Windows 7, the code failed to compile as Visual C++ says it expected constant expression on several lines. On Netbeans, I simply did something similar to char name[fullName.size()];, while on Visual C++, I tried, among other things,

const int position = fullName.size();
char Name[position];

How can I create a constant to use for the array?

Note: I know about this question, but is there any way I can get this working without using vectors, since that would require a re-write of many parts of the program?

+2  A: 

This is not possible in VC++. I know, pretty sad :(

The solutions include:

  • Create it on the heap
  • Make it constant

The new C++ standard (C++0x) proposes a 'constant expression' feature to deal with this. For more info, check this out.

George Edison
Thanks! I can't make it constant since I have no idea what the size of the string will be. What do you mean by "creating it on the heap?" Sorry, I'm still a novice at programming.
wrongusername
Allocate memory using `new` and free with `delete`
George Edison
Which really means use `vector`. There is *no* reason to have raw memory allocations hanging about, they leak and make your life more difficult.
GMan
I'm not saying use `new` and `delete` directly - I'm just explaining what the heap is.
George Edison
+1  A: 

C++ requires that the size of the array be known at compile time. If you don't mind using a non-standard extension, gcc does allow code like you're doing (note that while it's not standard C++, it is standard in C, as of C99).

I'd also guess that you could use a vector (in this particular place) with less trouble than you believe though -- quite a bit of code that's written for an array can work with a vector with only a re-compile, and little or no rewriting at all.

Jerry Coffin
+1  A: 

In VC++ you can't do runtime declarations of stack array sizes, but you can do stack allocation via _alloca

so this:

const int position = fullName.size();
char Name[position];

becomes this:

const int position = fullName.size();
char * Name = (char*)_alloca(position * sizeof(char));

It's not quite the same thing, but it's as close as you are going to get in VC++.

John Knoeller
I think we should steer the asker toward the heap... I don't want to see a beginner start writing code with _alloca
George Edison
@George: The heap might be a better choice for an array of objects, but for a simple string buffer, it's a worse choice.
John Knoeller
@John: Why not just use std::string? He says he's a beginner, that's the easiest thing to use.
George Edison
Never mind guys, I found out a way to skip using arrays/vectors entirely. Thanks for the help! :)
wrongusername
@George: Because he asked how to solve a specific problem, not how to redesign his code using a completely different API.
John Knoeller
A: 

Your char name[fullName.size()]; is an example of a variable-length array which - as far as I know - are not standardized in C++ so you're at the mercy of the compiler. [Slightly off-topic they're part of the C99 standard]

Eugen Constantin Dinca