View Full Version : C++ defining arrays in if/end statements
Wilbert
13th September 2008, 15:09
Apparently i'm doing something stupid:
int table = 0;
if (table==0) {
long int lx_m[2] = {1,2};
long int lx_v[2] = {3,4};
} else {
long int lx_m[2] = {5,6};
long int lx_v[2] = {7,8};
}
long int lx = lx_m[1];
results in "error C2065: 'lx_m' : undeclared identifier". I guess i need to declare the arrays outside the if/else statement, but i can't do this properly. How should i do this?
Guest
13th September 2008, 15:16
Something like this?
int table = 0;
long int LX_M1[] = {1,2};
long int LX_V1[] = {3,4};
long int LX_M2[2] = {5,6};
long int LX_V2[2] = {7,8};
long int lx_m, lx_v;
if (table==0) {
lx_m = LX_M1;
lx_v = LX_V1;
} else {
lx_m = LX_M2;
lx_v = LX_V2;
}
Wilbert
13th September 2008, 15:34
long int LX_M1[] = {1,2};
long int LX_V1[] = {3,4};
long int LX_M2[2] = {5,6};
long int LX_V2[2] = {7,8};
That's possible, but depending on the value of table i only need two of the four arrays. I was hoping i could define them when they are actually used.
Guest
13th September 2008, 15:50
Well if they are small, then just assign the values at run time.
Wilbert
13th September 2008, 16:00
I've 10 arrays with 120 elements each :)
Manao
13th September 2008, 16:34
If you first example had been valid, all four arrays would have been allocated on the stack of the function anyway. 10 arrays of 120 elements is small, so don't bother trying to optimize that.
In comparison, it's much more important to declare the array as const if they are, that will matter much more than trying to save 10 KB on the stack.
Wilbert
13th September 2008, 17:40
You are right, thanks.
In comparison, it's much more important to declare the array as const if they are
They are indeed constant. Just curious, why is declaring them constant so important?
LoRd_MuldeR
13th September 2008, 18:13
I'd assume that the compiler doesn't need to reserve a memory address (or a place on the stack respectively) for constants, like it has to do for variables.
Whenever the constant is needed, it can simply be passed as an "immediate" value, because the value is already known at compile-time - it's constant.
Not that I'm a compiler expert :p
Manao
13th September 2008, 18:28
That way, they may not be stored on the stack (it depends on the compiler's implementation, but I hope nowadays most C compiler would not place a const array on the stack).
The compiler may need a memory address for the constant, it depends how you use it, and how advance the compiler is.
In the end, using the const keyword whenever possible is sound programming. It warns you when you mistakenly try to change a const value, it helps people reading your code, and it tells the compiler it can try its best to optimize it. It gets even more important in c++, with the notion of const member function.
krosswindz
22nd September 2008, 13:58
To the best of my knowledge the reason that it doesnt work is that when you declare a variable inside a block its only visible to the block and not outside. The only way for you to use it would be to declare it outside the block.
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.