Click here to Skip to main content
13,287,856 members (54,141 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 7 Jul 2011

Checking an C-Array Size at Compile Time

, 7 Jul 2011
Rate this:
Please Sign up or sign in to vote.
Be sure that your array has the right size - without using size_of!
Assume you have an array being initialized on declaration in the following form, but with many more entries:

const char* arr[] =

It must be made sure, that the array has a certain size (maybe to fit an enumeration or for other reasons). Providing the number of elements to the array declaration is of limited help, as it would silently initialize any elements for which no value is given with 0. There will only be a compiler error if the initializer list is longer than the array.

The following code visualizes the vulnerability arising from this behavior:

const char* arr2[3] =

While arr2 looks pretty okay, its real contents are:

arr2[0] -> "One\0"<br />
arr2[1] -> "TwoThree\0"<br />
arr2[2] -> 0

(\0 visualizes the null-termination for C-strings - thats right there won't even be a null-value between "Two" and "Three")

Using a reduced or empty initialization list is actually a pretty good trick, if you want to initialize an array with 0-values, but not helpful in this case. So, if you have an array that is initialized on declaration it might be better not to specify its size directly but let the size be defined by the number of elements in the initializer list.

The array size can still be tested at compile time by the following function template:

    int test_len, 
    typename array_element, 
    int n>
inline void assert_array_size(array_element(& /* arr */)[n])
    // generate compile-time error if values mismatch:
    // (Static assertions provide a better alternative but
    // are not always available)
    int t[test_len == n];
    t[0] = 1;

This template causes an compiler error when the compile-time parameter test_len is different from the array size n automatically to be deducted from the function template parameter. In order to perform the test, a function or method needs to contain the following call to the function template.

void f() {
    assert_array_size<3>(arr); // OK
    // assert_array_size<4>(arr); // Compiler Error

As the test still runs a compile-time, it is not even necessary to call f() at run-time.

Note: For this to work, the array declaration must not state the number of elements. If the array size was given there (in this example to 3), the array will have that size and the assertion result would be forced to success even if it is not initialized correctly.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Doc Lobster
Software Developer
Germany Germany
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralReason for my vote of 5 go Pin
z54528444714-Jul-11 0:17
memberz54528444714-Jul-11 0:17 
GeneralReason for my vote of 1 news Pin
z54528444714-Jul-11 0:17
memberz54528444714-Jul-11 0:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 7 Jul 2011
Article Copyright 2011 by Doc Lobster
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid