Can the size of an array in C++ be stated as a variable?
2008-10-11 07:11:21 UTC
here is a part of the program i am writing, my compiler is taking issue with the line: int nums[n];
is this not allowed? it seems like it should work.
int n;
cout << "enter number of terms: ";
cin >> n;
int nums[n];
for (i=0; i cout << "enter term " << i << ": ";
cin >> nums[i];
cout << "\n";
}
Five answers:
Damasta AM inductee
2008-10-11 07:52:35 UTC
What is going on is you are trying to dynamically allocate memory on the stack. The stack expects for variable declarations and such to be static at runtime so that it knows exactly what to push/pop/mov around as the stack is the most commonly used. To counter that, you have the heap. The lesser used heap is for dynamic memory allocation.R ules of scope apply differently as you must de-allocate the memory by hand. As was suggested, you can either use vectors, the new and delete keywords, or the C-style malloc()/calloc() functions with the free() function for deallocating memory.
It is possible to get the size of an array in C++ by doing
#include
#include
int main(int argc, char**argv){
//The size of the pointer is 24 bytes
//The size of the type int is 4 bytes
int pointer[]={0,1,2,3,4,5};
printf("The number of elements in the int array=%d",sizeof(pointer)/sizeof(int));
/*The same as typing cout<<"The number of elements in the int array="< */
getchar();
return 0;
}
It says the number of elements in the int array=6 which is correct.
2016-12-16 10:34:25 UTC
the least complicated answer is to pass a pointer to the array, and to specify the size of the array in different variables. in case you like, you are able to create a shape that holds the two the size of the array and the array contents, and pass a pointer to the form. that's basically how swifter Pascal handles strings - the 1st byte of the form specifies the size of the string, and something of the form holds the string itself. once you pass variables in C (and subsequently in C++), you're copying those variables to the stack. once you have an array, it is not basically slow and inefficient, you additionally can run out of stack. In C, there's no difference between arrayname[5] and 5[arrayname]. The compiler in basic terms provides mutually arrayname and 5 mutually, using pointer arithmetic, and then dereferences the pointer. Multi-dimensional arrays are rather single-dimensional arrays; that's why you may desire to declare the size of a multi-dimentional array. without the statement, there's no way for the compiler to calculate whether arrayname[5][3] is rather arrayname[80 3] or arrayname [123]. it rather is recommended to forget approximately approximately Mantrid's answer; he does not look to understand that there is a difference between an array and an ASCIIZ string. you may desire to pass the size once you're passing an array.
saintmarl
2008-10-11 07:23:48 UTC
No it is not allowed to declare an array using a variable as size.
The compiler expects it to be a constant expression like:
int num[3];
In order for your code to work, just replace the declaration with this one:
int *nums = new int[n];
Hope this helps.
2008-10-11 07:15:48 UTC
No the value has to be constant in order for that to work. If you want it to work at runtime you need to allocate the space dynamically with pointers
int *nums =new int[n];
and release it after you're done
delete[] nums;
nums =NULL;
or just use a vector if you don't want to deal with that directly yet
#include
..in your headers
in your main function
std::vector nums(n);
nums[0] = 100;
..do stuff with nums..
2008-10-11 15:22:09 UTC
You can only do that if you allocate the array dynamically, as shown in this video: