none
Initialize an array with a const value

    Question

  • Hi there,

    I can initialize an array with a const value like this:

    const int SIZE = 10;
    int arr[SIZE] = {0};

    There is no compile error.

    However, if I initialize an array like this:

    class Cls
    {
    public:
        Cls() : LEN(10)
        {
        }

        void Foo()
        {
            int arr[LEN] = {0};
        }

    private:
        const int LEN;
    };

    There are 2 compile errors

    error C2057: expected constant expression
    error C2466: cannot allocate an array of constant size 0

    Why?

    Thanks in advanced.

    Tuesday, January 29, 2013 6:36 AM

Answers

  • Arrays are created in Stack.So the size of array is needed during compilation itself. But in your code LEN have no value. For allocating during run time you need to use dynamic memory allocation. Refer below links

    Constants

    Arrays

    Dynamic Memory


    Thanks, Renjith V R

    • Proposed as answer by Bordon Tuesday, January 29, 2013 9:43 AM
    • Marked as answer by JupiterLee Wednesday, January 30, 2013 3:42 AM
    Tuesday, January 29, 2013 8:53 AM
  • if I initialize an array like this:

    class Cls
    {
    public:
        Cls() : LEN(10)
        {
        }

        void Foo()
        {
            int arr[LEN] = {0};
        }

    private:
        const int LEN;
    };

    There are 2 compile errors

    error C2057: expected constant expression
    error C2466: cannot allocate an array of constant size 0


    A workable alternative is:

    class Cls
    {
    public:
        Cls()
        {
        }

        void Foo()
        {
            int arr[LEN] = {0};
        }

    private:
        static const int LEN = 10;
    };

    - Wayne

    • Marked as answer by JupiterLee Wednesday, January 30, 2013 3:42 AM
    Tuesday, January 29, 2013 10:12 AM
  • On 1/29/2013 5:12 AM, WayneAKing wrote:

    A workable alternative is:

    class Cls
    {
         static const int LEN = 10; };

    Or similarly,

    enum {LEN = 10};

    Igor Tandetnik

    • Marked as answer by JupiterLee Wednesday, January 30, 2013 3:42 AM
    Tuesday, January 29, 2013 10:19 AM

All replies

  • On 1/29/2013 1:36 AM, JupiterLee wrote:

    However, if I initialize an array like this:

    class Cls
    {
    public:
         Cls() : LEN(10)
         {
         }

         void Foo()
         {
             int arr[LEN] = {0};
         }

    private:
         const int LEN;
    };

    There are 2 compile errors

    error C2057: expected constant expression
    error C2466: cannot allocate an array of constant size 0

    Why?

    Array bound must be a compile-time integral constant. LEN is not a compile-time constant.


    Igor Tandetnik

    • Proposed as answer by Bordon Tuesday, January 29, 2013 9:43 AM
    Tuesday, January 29, 2013 6:38 AM
  • Arrays are created in Stack.So the size of array is needed during compilation itself. But in your code LEN have no value. For allocating during run time you need to use dynamic memory allocation. Refer below links

    Constants

    Arrays

    Dynamic Memory


    Thanks, Renjith V R

    • Proposed as answer by Bordon Tuesday, January 29, 2013 9:43 AM
    • Marked as answer by JupiterLee Wednesday, January 30, 2013 3:42 AM
    Tuesday, January 29, 2013 8:53 AM
  • if I initialize an array like this:

    class Cls
    {
    public:
        Cls() : LEN(10)
        {
        }

        void Foo()
        {
            int arr[LEN] = {0};
        }

    private:
        const int LEN;
    };

    There are 2 compile errors

    error C2057: expected constant expression
    error C2466: cannot allocate an array of constant size 0


    A workable alternative is:

    class Cls
    {
    public:
        Cls()
        {
        }

        void Foo()
        {
            int arr[LEN] = {0};
        }

    private:
        static const int LEN = 10;
    };

    - Wayne

    • Marked as answer by JupiterLee Wednesday, January 30, 2013 3:42 AM
    Tuesday, January 29, 2013 10:12 AM
  • On 1/29/2013 5:12 AM, WayneAKing wrote:

    A workable alternative is:

    class Cls
    {
         static const int LEN = 10; };

    Or similarly,

    enum {LEN = 10};

    Igor Tandetnik

    • Marked as answer by JupiterLee Wednesday, January 30, 2013 3:42 AM
    Tuesday, January 29, 2013 10:19 AM