Thread: Passing array size as a const variable do not compile

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    97

    Passing array size as a const variable do not compile

    Hi everyone,

    Could someone explain me why the following does not compile?


    Code:
    const uint8_t read_payload = 38;
    static char read_raw_data[read_payload] __attribute__((aligned(4))) = {0};
    Passing array size as a const variable do not compile-screenshot_1-jpg

    When I declare the read_payload as

    #define read_payload 38

    it works fine..

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Because constants in C are not the same as constants in C++.

    In C, const just means "I pinky-promise that I'm not going to try and modify this memory".

    You have to use the #define route as you've discovered.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    97
    Thanks Salem,

    However why this work?

    Code:
    const char read_payload = 38;
    char read_raw_data[read_payload] __attribute__((aligned(4)));

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Dunno.

    Maybe your first example would work if the size was also static.

    Or maybe your compiler is feeling generous.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    97
    Quote Originally Posted by Salem View Post
    Dunno.
    Maybe your first example would work if the size was also static.
    No it don't. I am using Segger Embedded Studio.
    Nevermind thanks!!

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    This happens because VLAs don't have a known constant length (ISO 9899 6.2.5 § 23). The compiler don't know the size of the array (even if you use a pre initialized size through a 'const' variable) at compile time.

    ISO states, as well, that VLAs shall not be static.
    Last edited by flp1969; 01-24-2021 at 10:04 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Define a const variable at compile time?
    By bedtime in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2019, 01:34 PM
  2. Replies: 3
    Last Post: 01-04-2019, 12:00 PM
  3. Why const variable can not define array bounds.
    By MartinR in forum C Programming
    Replies: 2
    Last Post: 05-03-2018, 02:43 PM
  4. 2D array, size unknown at compile time
    By gah_ribaldi in forum C# Programming
    Replies: 10
    Last Post: 12-04-2009, 05:42 PM
  5. Passing a 2d array of unkown size
    By dannyzimbabwe in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2009, 11:37 AM

Tags for this Thread