Getter/setters and constants
I dont want to start an argument.. but I want to get a few things straight.
Should this work
int x = 3;
int xarray[x];
?
It does not compile on my compiler, but my friend says it compiles fine.
Now, for getter/setters.. they return const of the value.. do I just use them like..
const int getX(){return x;}?
And if so, shouldn't this work
int xarray[getX()];
?
It doesnt work for me either.. so my question is.. is my C++ compiler (MSVC++ 6.0) not configured right or what?
Quote:
int x = 3;
int xarray[x];
This doesn't work in Visual Studio because arrays are expected to have a constant value at compile time and an int of x is not compile time constant. It might compile in something like GCC but seeing as I have not worked with it so I cannot confirm that.
Quote:
const int getX(){return x;}?
int xarray[getX()];
This won't work, statically allocated arrays get allocated into the stack or DATA section of the executable. So the data must be available before it enters the function containing the array declaration. (compliments to ravuya)
[Edited by - DevLiquidKnight on January 1, 2007 2:06:01 AM]
Quote:Original post by Crazyfool
Should this work
int x = 3;
int xarray[x];
Nope, the dimension size specifier in the array declaration needs to be a compile-time constant expression, which means that the compiler needs to know what x is at compile time. To allow the above the compiler would have to ensure x hasn't been modified since it's initialisation (which it can't, check out the code sample below), or do dynamic stack allocation (which isn't part of the standard, but is allowed by some compilers (like gcc) as an extension). Here's an example why the compiler can't ensure that x hasn't been modified:
void bad_monkey(const int& val){ int& nonconst_val = const_cast<int&>(val); nonconst_val = 10;}int main(){ int x = 5; bad_monkey(x); int xarray[x];}
This is allowed though:
const int x = 3;int xarray[x];
Quote:Original post by Crazyfool
Now, for getter/setters.. they return const of the value.. do I just use them like..
const int getX(){return x;}?
The const here is in the wrong location, what you probably want is this (assuming getX() is a member function):
int getX() const { return x; }
This will tell the compiler that getX() doesn't modify the state of the class.
Quote:Original post by Crazyfool
And if so, shouldn't this work
int xarray[getX()];
Nope, because getX() isn't a constant expression as explained above. On those compilers that do dynamic stack allocation as an extension it will probably be allowed though.
Quote:Original post by Crazyfool
It doesnt work for me either.. so my question is.. is my C++ compiler (MSVC++ 6.0) not configured right or what?
That compiler was created before C++ was even standardised and there's really no excuse to be using it instead of the free Visual Studio C++ Express which is a very good compiler. But this looks like something it actually gets right [smile]
Quote:Original post by Crazyfool
And if so, shouldn't this work
int xarray[getX()];
No it shouldn't work. When declaring something like "int array[size];", the size must be determined at compile time. Useing a const int works because its known at compile time what the value of that int will be. However returning a const value from a function just means you can't change that value. It does NOT mean its known at compile time. You can do something like this:
const int myFunc()
{
return timeGameHasBeenRunning;
}
Each time you call that function it'll return something different. So there is no way at compile time to know what the value will be, hence you can use this to create an array on the stack. The compiler needs to know how much stack space a function will take when it compiles, so an array declared on the stack must have a constant size that is known at compile time.
Edit: Beaten.
Yeah, it definately is not standard but I've seen gcc working with it. They use alloca(), which is kinda like malloc() except it allocates space in the stack.
Thanks, but the new alternative:
int getX() const { return x; }
Does not work either. I am not sure if it was supposed to work, but my point is.. what is the point of making it const?
int getX() const { return x; }
Does not work either. I am not sure if it was supposed to work, but my point is.. what is the point of making it const?
An assignment for a Data Structures class I took this past semester required that we write a program which would "manage multiple stacks within a single block of sequentially allocated memory". This essentially meant that we couldn't allocate memory off of the heap for this program, and that the program would require an array defined at run-time, as specified above. The language we had to (or was suggested we) use for the assignment was ADA, so this wasn't much of an issue, really.
I had learned most of the basics for C many, many years ago (around 10). I thought remembered reading that you "had to use a constant value to define array sizes at the time", but figured it had been so long that I thought maybe I was mistaken, so I booted up my old 200mhz laptop, running Linux and gcc, and, just out of curiosity, quickly wrote a program that did, indeed, compile with an array declared using a non-const variable. I tested it for a while, and it never once crashed.. so I assumed the compiler actually DID set aside the proper amount of memory.
Anyway, I looked around to figure out WHY this was happening, and I came up with some hits like this one:
http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=215&rl=1
Scroll down to "Variable-Length Arrays".
Do you think that gcc might just be mixing and matching standards here or something? To be honest, the test I used didn't involve classes or any 'C++ only features', so unless gcc bases the 'standard' it picks to use by file extension, it may have actually USED the C99 standard.
I had also read somewhere else a description of WHY this feature wasn't implemented in C++, but I can't remember the reason or the site I found it at.
Anyway, that's my guess as to why it works for some compilers, but not others. I could be wrong, but I think it's an interesting piece of trivia anyway (and maybe if you ever need to code pure C, it'll be helpful).
I had learned most of the basics for C many, many years ago (around 10). I thought remembered reading that you "had to use a constant value to define array sizes at the time", but figured it had been so long that I thought maybe I was mistaken, so I booted up my old 200mhz laptop, running Linux and gcc, and, just out of curiosity, quickly wrote a program that did, indeed, compile with an array declared using a non-const variable. I tested it for a while, and it never once crashed.. so I assumed the compiler actually DID set aside the proper amount of memory.
Anyway, I looked around to figure out WHY this was happening, and I came up with some hits like this one:
http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=215&rl=1
Scroll down to "Variable-Length Arrays".
Do you think that gcc might just be mixing and matching standards here or something? To be honest, the test I used didn't involve classes or any 'C++ only features', so unless gcc bases the 'standard' it picks to use by file extension, it may have actually USED the C99 standard.
I had also read somewhere else a description of WHY this feature wasn't implemented in C++, but I can't remember the reason or the site I found it at.
Anyway, that's my guess as to why it works for some compilers, but not others. I could be wrong, but I think it's an interesting piece of trivia anyway (and maybe if you ever need to code pure C, it'll be helpful).
ALl this info is like music to my ears! I love learning! ANyway, if anyone has anything to solve my question, please do so. ;)
slight hijack: when is a good time to use getters/setters? normally the way I used to use them, it seemed better make everything public.
Quote:Original post by Crazyfool
Thanks, but the new alternative:
int getX() const { return x; }
Does not work either. I am not sure if it was supposed to work, but my point is.. what is the point of making it const?
Say you have this piece of code:
class Foo{public: void Bar() { }};int main(){ const Foo foo; foo.Bar();}
This won't compile, because the method Foo::Bar is assumed,by default, to be a "read-write" function that can change the state of Foo. Since the variable "foo" has been declared a constant, this cannot be. However, if you define Bar like:
void Bar() const
{
}
then you notify the compiler that Bar is a "read-only" function that does not change the state of the object, so it can be used with a const object.
Notice that having something like "const int Bar(){}" is something else entirely, although it still doesn't mean that the return value stays the same during the whole duration of the program, and this is what the array dimension requires.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement