Products > Programming

Getting around VC's lack of support for C99 array initialisation designators

(1/2) > >>

HwAoRrDk:
Given that the Visual C++ compiler, when dealing with C code, doesn't have support for C99 array initialisation designators, does anybody have any clever ideas for initialising a sparsely-populated array?

What I'm talking about is the inability to do this:


--- Code: ---static const int foo[256] = {
    [64] = 12345,
    [120] = 9876,
    // etc...
};

--- End code ---

I have a read-only lookup table 256-element array that will be static const, but a significant proportion of the indices will not be populated. I was just going to include an appropriate number of dummy entries in the initialisation statement, but maybe there's a better way.

P.S. Dear Microsoft, it's been 20 years, and your compiler still doesn't support a useful standard language feature, despite you going to the trouble of adding features from later standards, like C17. Please add full C99 support. Thank you.

evb149:
Use a compiler that isn't horrible on a platform that isn't horrible?

Or I suppose if you really must

struct S_Sparse {
  int default_initialized_0[64];
  int initialized_0[5];
  int default_initialized_1[12];
};

static const struct S_Sparse s_initialized = {
 {0,},
 {1,2,3,4,5},
 {0,}
};

Or generate the code (.h file etc.) for a full explicit initialization from a sparse C++ or whatever definition then output that to the file you build with.


--- Quote from: HwAoRrDk on April 30, 2022, 03:01:22 pm ---Given that the Visual C++ compiler, when dealing with C code, doesn't have support for C99 array initialisation designators, does anybody have any clever ideas for initialising a sparsely-populated array?

What I'm talking about is the inability to do this:


--- Code: ---static const int foo[256] = {
    [64] = 12345,
    [120] = 9876,
    // etc...
};

--- End code ---

I have a read-only lookup table 256-element array that will be static const, but a significant proportion of the indices will not be populated. I was just going to include an appropriate number of dummy entries in the initialisation statement, but maybe there's a better way.

P.S. Dear Microsoft, it's been 20 years, and your compiler still doesn't support a useful standard language feature, despite you going to the trouble of adding features from later standards, like C17. Please add full C99 support. Thank you.

--- End quote ---

evb149:
Or of course

struct S_array {
   int a[120];
}

static S_array g_s_array;

const int * getter(void) {
  static bool initialized = false;
  if( ! initialized ) {
    static const int segment1[] = {1,2,3,4,5};
    memcpy( &g_s_array.a[64], &segment1[0], sizeof(segment1) );
    g_s_array.a[120] = 9876;
  }
  return &g_s_array.a;
}

void user(void) {
  const int * const p_array = getter();
  int x = p_array[120];
}

Or of course there's the old way of just creating an assembly section that creates the initialized data
using a combination of 'define dword' and 'define space' or similar constructs to create a pattern of
sparsely initialized read only data section chunk with the appropriate C symbol name
(if you want it const and not runtime initialized) then just refer to that symbol name from C as const int * p_array or whatever.  That will do exactly what you want except for the syntax of initializing the sparse data.

Or you might be able to compile a C99 .c file with the appropriate definition to a .o or .a or .s file which has the correct definitions made per. C99 with a C99 compliant compiler then link with that file from VC if you must.

TheCalligrapher:

--- Quote from: HwAoRrDk on April 30, 2022, 03:01:22 pm ---Given that the Visual C++ compiler, when dealing with C code, doesn't have support for C99 array initialisation designators, does anybody have any clever ideas for initialising a sparsely-populated array?
--- End quote ---

What??? Visual Studio compiler supports C99 since a long time ago. It has full support for designated initializers in arrays since, like VS 2013 or 2015.


--- Quote from: HwAoRrDk on April 30, 2022, 03:01:22 pm ---What I'm talking about is the inability to do this:


--- Code: ---static const int foo[256] = {
    [64] = 12345,
    [120] = 9876,
    // etc...
};

--- End code ---

--- End quote ---

This works perfectly fine in Visual Studio. No problems whatsoever.


--- Quote from: HwAoRrDk on April 30, 2022, 03:01:22 pm ---P.S. Dear Microsoft, it's been 20 years, and your compiler still doesn't support a useful standard language feature, despite you going to the trouble of adding features from later standards, like C17. Please add full C99 support. Thank you.

--- End quote ---

What are you going on about? Are you sure you enabled C11 ort C17 support in compiler settings? In old versions of Visual Studio support for C99 in C compiler had to be enabled semi-unofficially as a "language extension". In more recent Visual Studio it is simply enabled as official part of C11 or C17 support.

evb149:
You're right, the construct works fine in MSVC C99 mode:
https://godbolt.org/z/TjPbjPM78


--- Quote from: TheCalligrapher on April 30, 2022, 03:39:02 pm ---
What??? Visual Studio compiler supports C99 since a long time ago. It has full support for designated initializers in arrays since, like VS 2013 or 2015.

This works perfectly fine in Visual Studio. No problems whatsoever.

What are you going on about??? Are you sure you enabled C11 ort C17 support in compiler settings? In old versions of Visual Studio support for C99 in C compiler had to be enabled semi-unofficially as "language extension". In more recent Visual Studio it is is simply enabled as official part of C11 or C17 support.

--- End quote ---

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version