Community
cancel
Showing results for 
Search instead for 
Did you mean: 
kami09
Beginner
119 Views

Problems defining an array? linux ok, windows fails

Jump to solution
Hello,

I have a program that compiles fine on:

- Linux GCC
- Linux Intel C++ 11.1 compiler,

However when I try to compile with Visual Studio 2005 and Intel C++ 11.1, I get an error.

The code is as easy as this:

int n=3;
if (some_condition) n=4;

double myarray;

And the error is: "expression must have a constant value"

I cannot make the n variable const because it can change its value. I also think my code should compilein windows platform as fine as in linux. The awful thing is that this compiles with Intel c++ compiler on Linux. What am I missing?

I have tried using -std=c99 but it seems to do nothing.

Thank you in advance,

Jaime
0 Kudos
1 Solution
Michael_K_Intel2
Employee
119 Views
Hi Jaime,

Are you compiling C or C++ code?

If it is C code, the /Qstd=c99 option should solve the problem. The option might be deprecated as most compilers (including ICC) are likely to support C99 in the future by default. That should not be much of a problem.

If you're writing your program in C++, there's no other option than using the malloc call and pointers to "simulate" variable length arrays (VLA). C++ simply does not support them and I am not aware of a way to turn on VLAs in C++.

BTW, I doubt that you need to rewrite all of your code. You only need to touch code that allocates the array. The rest of the program should work fine as you can use the [] operator on the pointer to access the array elements.

Cheers,
-michael

View solution in original post

6 Replies
Michael_K_Intel2
Employee
119 Views
Quoting - kami09
Hello,

I have a program that compiles fine on:

- Linux GCC
- Linux Intel C++ 11.1 compiler,

However when I try to compile with Visual Studio 2005 and Intel C++ 11.1, I get an error.

The code is as easy as this:

int n=3;
if (some_condition) n=4;

double myarray;

And the error is: "expression must have a constant value"

I cannot make the n variable const because it can change its value. I also think my code should compilein windows platform as fine as in linux. The awful thing is that this compiles with Intel c++ compiler on Linux. What am I missing?

I have tried using -std=c99 but it seems to do nothing.

Thank you in advance,

Jaime

Hi!

In C (C89), there's a requirement that the size of an array has to be a compile time constant. With C99, variable length arrays have been added to the language. Your compiler setting assume that you're compiling C89 code and that's why the error happens.

You have two options:(1) turn on support for C99 (using the /Qc99 switch on Windows) or (2) use the following malloc call:

double *myarray;
myarray = (double *) malloc(sizeof(double) * n);

Cheers,
-michael

PS: When compiling C++ code, you're stuck to option (2) as C++ is based on C89 and does not support variable length arrays.

kami09
Beginner
119 Views

Hi!

In C (C89), there's a requirement that the size of an array has to be a compile time constant. With C99, variable length arrays have been added to the language. Your compiler setting assume that you're compiling C89 code and that's why the error happens.

You have two options:(1) turn on support for C99 (using the /Qc99 switch on Windows) or (2) use the following malloc call:

double *myarray;
myarray = (double *) malloc(sizeof(double) * n);

Cheers,
-michael

PS: When compiling C++ code, you're stuck to option (2) as C++ is based on C89 and does not support variable length arrays.


Hello Michael,

Thank you for answering so fast. However I have tried using both:

/Qc99

/Qstd=c99

with both of them i cannot achieve my objective. For example with /Qc99 I get a warning for future deprecation and the same error:

------ Operacin Generar iniciada: proyecto: definovector, configuracin: Debug Win32 ------
Compiling with Intel C++ 11.1.046 [IA-32]... (Intel C++ Environment)

icl: command line remark #10010: option '/Qc99' is deprecated and will be removed in a future release. See '/help deprecated'
example.cpp

.example.cpp(10): error: expression must have a constant value
double myarray;
^

compilation aborted for ./example.cpp (code 2)


I got the same result adding the /Qstd=c99 (without the deprecated warning). I am really interested in making my code compile without having to rewrite all my files (it's a huge project), however I think i'm not managing well to "enable" the c99 compatibility

Michael I would be very pleased if you could point me anything that could help me.

Thank you in advance.
Michael_K_Intel2
Employee
120 Views
Hi Jaime,

Are you compiling C or C++ code?

If it is C code, the /Qstd=c99 option should solve the problem. The option might be deprecated as most compilers (including ICC) are likely to support C99 in the future by default. That should not be much of a problem.

If you're writing your program in C++, there's no other option than using the malloc call and pointers to "simulate" variable length arrays (VLA). C++ simply does not support them and I am not aware of a way to turn on VLAs in C++.

BTW, I doubt that you need to rewrite all of your code. You only need to touch code that allocates the array. The rest of the program should work fine as you can use the [] operator on the pointer to access the array elements.

Cheers,
-michael

View solution in original post

kami09
Beginner
119 Views
Hi Jaime,

Are you compiling C or C++ code?

If it is C code, the /Qstd=c99 option should solve the problem. The option might be deprecated as most compilers (including ICC) are likely to support C99 in the future by default. That should not be much of a problem.

If you're writing your program in C++, there's no other option than using the malloc call and pointers to "simulate" variable length arrays (VLA). C++ simply does not support them and I am not aware of a way to turn on VLAs in C++.

BTW, I doubt that you need to rewrite all of your code. You only need to touch code that allocates the array. The rest of the program should work fine as you can use the [] operator on the pointer to access the array elements.

Cheers,
-michael


Hello Michael,

Now I understand it... c99 gives support for C code. As you have imagined my code is C++ not C (my mistake when posting).

You are right, I'm gonna change all this occurences to mallocs to make my code full crossplatform.

Thank you so much, 5 stars :)

Jaime
Michael_K_Intel2
Employee
119 Views
Quoting - kami09

Thank you so much, 5 stars :)


Hi Jaime,

you're welcome :-).

Cheers,
-michael

jimdempseyatthecove
Black Belt
119 Views

If your arrays are local (stack) and small-ish consider using alloca to allocate on stack. If your array is static then use malloc. The only potential problem is what you expect for return value ofsizeof(array).

Jim Dempsey
Reply