Declaring the Array Size with a Non-Constant Variable

C++ declare an array based on a non-constant variable?


How can the size of the array come from a non-constant variable?

Currently, because that compiler has a non-standard extension which allows you to use C's variable length arrays in C++ programs.

Does the compiler automatically translate the int array[n] to int* array = new int[n]?

That's an implementation detail. I believe GCC places it on the stack, like normal automatic variables. It may or may not use dynamic allocation if the size is too large for the stack; I don't know myself.

Declaring the array size with a non-constant variable

This is a GCC extension to the standard:

You can use the -pedantic option to cause GCC to issue a warning, or -std=c++98 to make in an error, when you use one of these extensions (in case portability is a concern).

Can array length in declaration be non-constant?

sizeof(int) * 5 used in the example statement in your question: int my_array[sizeof(int) * 5];, is a constant expression, so although it does not serve as a good illustration of your primary question, it is legal syntax for C array declaration.

With the exception of C99, variable length arrays are optional in most recent C compiler implementations. (In C99 inclusion of VLA is mandated.)

So, if your compiler supports VLA, the following are an examples:

char string[100] = {0};
scanf("%99s", string);
int VLAarray1[strlen(string)+1];//per question in comments about functions to size array.
memset(VLA1array, 0, sizeof(VLAarray1));//see Note below for initialization

int arrayLen = 0;
scanf("%d", &arrayLen);
int VLAarray2[arrayLen];
memset(VLAarray2, 0, sizeof(VLAarray2));//see Note below for initialization
int nonVLAarray[100] = {0};//initialization during declaration of nonVLA

Note: that VLAs cannot be initialized in any form during its declaration. As with all variables though it is a good idea that it be initialized in subsequent statements by explicitly assigning values to its entire region of memory.

Passing VLAs as function arguments is not included within the scope of your question, but should it be of interest, there is a good discussion on that topic here.

Declaring an array with a non-constant size variable

C99 allows variable length arrays to be created on the stack. Your compiler may support this feature. This features is not available in C89.

What the summary told you was true, from a certain point of view. :-)

Is there a way to initialize an array with non-constant variables? (C++)

The compiler need to have the exact size of the class when compiling, you will have to use the new operator to dynamically allocate memory.

Switch char array[x][y]; to char** array; and initialize your array in the constructor, and don't forget to delete your array in the destructor.

class MyClass
{
public:
MyClass() {
x = 10; //read from file
y = 10; //read from file
allocate(x, y);
}

MyClass( const MyClass& otherClass ) {
x = otherClass.x;
y = otherClass.y;
allocate(x, y);

// This can be replace by a memcopy
for( int i=0 ; i<x ; ++i )
for( int j=0 ; j<x ; ++j )
array[i][j] = otherClass.array[i][j];
}

~MyClass(){
deleteMe();
}

void allocate( int x, int y){
array = new char*[x];
for( int i = 0; i < y; i++ )
array[i] = new char[y];
}

void deleteMe(){
for (int i = 0; i < y; i++)
delete[] array[i];
delete[] array;
}

MyClass& operator= (const MyClass& otherClass)
{
if( this != &otherClass )
{
deleteMe();
x = otherClass.x;
y = otherClass.y;
allocate(x, y);
for( int i=0 ; i<x ; ++i )
for( int j=0 ; j<y ; ++j )
array[i][j] = otherClass.array[i][j];
}
return *this;
}
private:
int x, y;
char** array;
};

*EDIT:
I've had the copy constructor
and the assignment operator

Creating array with non constant sizes

There is a non-standard extension in the GCC compiler which most people use that allows variable length arrays. You really shouldn't use it though, because VLAs have a lot of downsides, which is why they aren't in the C++ standard in the first place. Also, you will probably end up with a stack overflow when your program receives a large input due to attempting to create a big array on the stack.

Make your arrays constant size or use std::vector.

how can I declare a 2d array with non constant values in c?

You can use dynamics arrays. For that you can use malloc to declare the size you want, Try this: allocate matrix in C

How to create an array when the size is a variable not a constant?


int *a = new int[variable_int];

Remember to delete[] the allocated space when you are done with it!



Related Topics



Leave a reply



Submit