开发者

My class book says that arrays are set with const variables. What are some problems I could encounter when I use non const variables?

开发者 https://www.devze.com 2023-02-24 00:37 出处:网络
The C++ book I\'m reading tells us that arrays are to be set with constant variables, but I wanted to let user input instruct the size of the array, and it worked. Are there are pr开发者_运维知识库obl

The C++ book I'm reading tells us that arrays are to be set with constant variables, but I wanted to let user input instruct the size of the array, and it worked. Are there are pr开发者_运维知识库oblems I should worry about.


How did you declare the array?

You can create an array with a size based on a variable if you declare it this way:

int size;
cin >> size;
// (assert size is > 0 and < some really big number here)
int* myarray = new int[size];
// Do stuff with it here...
delete [] myarray;  // Don't forget this.

and that will work just fine. You shouldn't be able to create an array this way:

int size;
cin >> size;
int myarray[size]; // should break with an ugly compiler error.

If you did the second method and it worked, then you have a strange compiler, and you should not count on it working when you use other compilers on other platforms.


Using C arrays is not a best practice. Use std::vector instead.

int n;
std::cin>>n;
std::vector<int> vec(n);//declares int array size of n;

to use std::vector you should include <vector>.

Also vector is much more flexible. For example you can change size of vector at runtime.

std::vector<int> vec;//declares empty vector

int some_numb;

while(std::cin>>some_numb)//read untill end of file
   vec.push_back(some_numb);//add element at the end of array;

Edit: If you still want to use C style array instead of vector, here is syntax.

int n;
std::cin>>n;
int * arr = new int[n];

But remember, that after creating array this way you should also delete it manually. Here is syntax.

delete []arr;


There are two different kinds of array allocation techniques, the first is static, where you know the size of the array when the program is compiled. Since you have to know the size when you compile, it's a good idea to make the size integer const to prevent it from accidentally being changed, which could cause your program to crash:

const int myarray_size = 10;
int myarray[myarray_size];

The second is dynamic, where you don't know the size of the array at compile time, only at run time. You have to call the new operator here to tell the operating system to allocate memory for the array:

int myarray_size;
cin >> myarray_size;
int* myarray = new int[myarray_size];

You also must call delete[] myarray later when you're done using the array.

The following is an example of why using const for static array sizes is good practice:

const int myarray_size = 10;
cin >> myarray_size;  // This won't compile because you're trying to change a const
int myarray[myarray_size];

Whereas the following might compile (depending on your setup), but could easily cause your program to crash when run:

int myarray_size;
cin >> myarray_size;
int myarray[myarray_size]


If you don't add any checks the user could potentially create a huge array, which could consume a lot of memory.

0

精彩评论

暂无评论...
验证码 换一张
取 消