• Register

In C++ compiler requires the size of the array at the compilation time. Array occupies the space in the memory according to the size given by the user.


C++ has defined size for all the variables for example int settle in 2 bytes, char settle in 1 byte and so on. Variables like arrays have a definite size as the user gave at the time of programming. If we do not provide the size of the array, then at the compilation time compiler will throw an error.


The solution is pretty clear: we just have to define the size of the array before the compilation time. And we cannot use the variable size for the arrays. For example, we can define an integer like int num; but we cannot define the size of an array like this.

class Constants
    static const unsigned int C_BufferSize;



#include "Constants.h"

const unsigned int Constants::C_BufferSize(128);


#include "Constants.h"

int main()
    char buffer[Constants::C_BufferSize];
    return 0;

Here in the main.cpp file, compiler will throw the error because of unknown size. 

Programmer should have done it like this

//in constants.h

class Constants
    static const unsigned int C_BufferSize = 128; //initialize here

//definition in cpp file

const unsigned int Constants::C_BufferSize; //no initialization here

Hope this helps.

posted Sep 10 in c++ 140 points