Assigning array with .length() ?

1
2
3
4
5
6
int main()
{
	std::string word = "This";

	bool arr[word.length()];
}


This works fine on onlinegdb.com but gives the error "expression must have a constant value" on visual studio. Any insight?

I also tried this:

1
2
3
4
5
6
int main()
{
	std::string word = "This";
	const int length = word.length();
	bool arr[length];
}


Visual Studio complains that it isn't a constant, not sure what the issue is. Anyone have an idea?
Last edited on
You are trying to declare a variable-length array. It's illegal in standard C++ (at the moment). It's permitted in the latest version of C, so some compilers tolerate it.
Non-dynamically allocated arrays needs to have a fixed size in C++. In C there is an optional feature called Variable Length Arrays (VLA) that allows you to use a size that is only known at runtime and some compilers allow this by default even when compiling C++ code. Visual Studio doesn't.

GCC will issue a warning for your code if the -pedantic compiler flag is used.
warning: ISO C++ forbids variable length array ‘arr’ [-Wvla]
  bool arr[word.length()];
                        ^

Last edited on
It was part of the template code they gave us for a lab assignment, so I find it odd that it's not even permitted in C++.

I tested the code out in about 5-8 different online compilers, not a single one threw an error (just warnings that the variable is unused), but they all compiled fine.
Alright, thanks lastchance and Peter87 . I guess since they use their own compiler to make the assignments they included this bit of code either without knowing it wasn't standard C++ or figuring we'd use the same compiler.
its not just the compiler; my g++ will take it with no flags and will reject it with the force compliance flags set. But your professors really should know better, I would alert them to this issue.

the fastest, easiest fix is just to say
vector <bool> arr(word.length()); //this will behave exactly the same as the array you had. you may need to include <vector>. arr[x] still works just fine so no other code changes seem necessary.
Last edited on
Thanks jonnin - I already added a vector to replace the array. No one taught vectors yet so I doubt the lab's professor will be very pleased to see it, but at most it'll be a few docked points. At best, he'll understand that its not a C++ function and not make a statement about me not using their Linux weird things to code. Also, vectors are usually frowned upon in these courses because they literally solve all your problems. Since he doesn't expect any of us to use any memory management, I think it should be fine.

your professors really should know better

If you saw half the stupidity I've witnessed, this is just nothing in comparison. Had to write code that did something stupid using nested loops. The issue is that it was so simple you only needed 1 loop. In the end, to use a nested loop, you had to split the work about 90/10 between the loops. Not only was it hard to implement (I think only 1 student other than me actually figured it out if any others at all), but you had to make a new variable and if statement to break out of the loop. There was no way to have the nested loop stop from its condition. It was just a giant mess. It was to enforce the knowledge of nested loops, but it instead just showed students how to write bad code.

After explaining how horrible it was, he said it still had to be done this way. So I made another loop and shoved the already done loop inside, told him I fulfilled the requirements. He looked at me and said something along the lines of, "You know that's not what I wanted." So I coded it "correctly" and turned it in.

Thanks also Duthomhas, read the posts, they get real into it xD . Seems that it would be nice to have VLAs, especially since a lot of compilers seem to allow them anyway. I can see it would cause some contradictions with existing standards, but one can always make compiler acceptable code that breaks. Maybe I don't know enough to speak on this though.

Thanks guys!
Last edited on
if your prof is using illegal code and docks you points for fixing it, you need to change schools asap if at all possible. You already know more than they do and will learn nothing good at that place. Even an online school might be better than this setup.

If you are stuck with graduating / going where you currently are, keep doing what you are doing... learn the right way, turn in the garbage they want. Its extra work, but you will be much better prepared for a job when you get out.


Sorry to reply late, thanks for the advice jonnin, very appreciated ^/^. I'll definitely keep teaching myself and give them what they want for a degree.
Topic archived. No new replies allowed.