Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

Two VLAs in a struct

qpalz
Beginner
735 Views
Hi, I am using icc to compile a source code with a struct which consists of two VLA:
int a;
...
struct vla
{
int x;
int y;
};
An error happens: error: no more than one vla field is supported in the struct
Two vla in a struct is support in gcc. Can icc support that?
0 Kudos
11 Replies
Georg_Z_Intel
Employee
735 Views
Hello,

according to the C99 standard VLAs are not allowed to be members of structs & unions.

On the other hand I can confirm that GCC supports your example. We only seem to allow a struct with just one VLA - removing member "int y" works.
For better GCC compatibility I'll file a ticket (edit: DPD200228467) and let you know when implemented.

For the time being, the only workaround is to avoid VLAs as struct/union members like that.

Best regards,

Georg Zitzlsberger
0 Kudos
qpalz
Beginner
735 Views
Thanks for your reply. Before it isimplemented, the only way to compile the code is to modify it, right? Hope it will beimplemented soon.
0 Kudos
Georg_Z_Intel
Employee
735 Views
Hello,

unfortunately yes.
You can use pointers & manual (stack) allocation as a workaround, for example:

[bash]#include ... int a = ... struct vla { int *x; int *y; }; struct vla test; test.x = (int *)alloca(sizeof(int) * a); test.y = (int *)alloca(sizeof(int) * a); [/bash]Allocating on the stack fits best since VLAs are in block/function scope anyways.
Also using macros can surely make the workaround better readable and less error prone.

Best regards,

Georg Zitzlsberger
0 Kudos
SergeyKostrov
Valued Contributor II
735 Views
Hi everybody,

I wonder if it will work:

...
#define X 0
#define Y 1
...
int a;
...
struct vla
{
int p[2];
};
...

'p' is for point

Best regards,
Sergey
0 Kudos
SergeyKostrov
Valued Contributor II
735 Views
Please take a look at an article 'Incompatibilities between C99 and C++09':

http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=325
0 Kudos
SergeyKostrov
Valued Contributor II
735 Views

Just checked withg++ and it successfullycompiles that code:

...
int iSize;

struct VLA
{
void DoProcessing( void )
{
int x[iSize];
int y[iSize];
...
};
};
...

0 Kudos
Georg_Z_Intel
Employee
735 Views
Hello,

adding this feature for the current version (Intel Composer XE 2011 and all its update versions) cannot be done. However, the next major product version will contain support for this kind of VLAs.

Thank you for bringing this up!

Best regards,

Georg Zitzlsberger
0 Kudos
SergeyKostrov
Valued Contributor II
735 Views
Hi everybody,

I understood that VLAs aredependant on 'alloca' CRT function. I've done some investigationfor 'alloca' CRT function
and it doesn't allow to create two arrays in the same scope of some function. A workaround could be used instead by Partitioning of allocated memory.

Best regards,
Sergey
0 Kudos
SergeyKostrov
Valued Contributor II
735 Views

Why should somebody wait for a builtin support of VLAs in some C/C++ compiler? There are some
limitations with VLAs at the moment and they won't go away easily. Isn't that better to use some kind of workaround?

Here is an example of portable C++ template class that could be used instead:

...
template< class T > struct TStackArray
{
public:
TStackArray( int iSize )
{
m_ptData = NULL;
m_iSize = iSize;
};

virtual inline void Process( void )
{
m_ptData = ( T * )alloca( sizeof( T ) * m_iSize );
if( m_ptData == NULL )
return;

//...
// Some functionality
//...
};

T *m_ptData;
int m_iSize;
};
...

...
void SomeFunction( void )
{
TStackArray< int > iSA( 131072 ); // A 128K array of integers will be allocated on the Stack
iSA.Process();
}
...

Also, if several arrays are needed a main memory block allocated by 'alloca' could be easilyPartitioned.

Best regards,
Sergey

0 Kudos
Georg_Z_Intel
Employee
735 Views
Hello Sergey,

what makes you think there's such a limitation for alloca? To my knowledge you can use it as many times as you want (even per scope) unless you exceed stack limits, of course.

Best regards,

Georg Zitzlsberger
0 Kudos
SergeyKostrov
Valued Contributor II
735 Views
...What makes you think there's such a limitation for alloca? To my knowledge you can use it as many times as you want (even per scope) unless you exceed stack limits, of course...

Hi Georg,

Thank you for the note and brining my attention again.

The situation is very interesting because'alloca' didn't work as expected with Visual Studio 2005 without SP1 and
I couldn'tallocate two memory blocks with 'alloca' in a function or in some scope.

Aboutone weekago I've installed SP1 for Visual Studio 2005 and then 3 days agoyou've posted a message.

So, I've just completed re-testing and, to my surprize, 'alloca' succesfully allocated two memory blocks in my test cases.

I'm really glad that aproblem with 'alloca'was resolved.

Theidea to do Partitioning came to my mind aftera set ofunsuccessful attempts to allocate several memory blocks
with 'alloca' in the same scope.

Best regards,
Sergey
0 Kudos
Reply