Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a user-inputted sized Array using new operator

I have a few array-related questions. I've studied that array-size must be constant on declaration/compiler must know its value. But using the GNU GCC compiler (C++11 standard filter) and I am able to perfectly compile and run a program using a variable as array size, when declaring said array dynamically (using new)

int num;
cout << "How big an array? ";
cin >> num;
int *arr = new int [num];

Ques1) Is this considered standard? My profs are contradictory.

Ques2) If it is standard, in that case, is it possible to extend the size of the array (or any array) after creation?

Ques3) Again, if this expression is standard, then is it possible to use it within a function - eg. using a function to create such an array? (if so, how?)

(PS: Hi, I'm new here and also still a novice in C++)

like image 555
Shisa Avatar asked Jun 13 '13 01:06

Shisa


People also ask

How do you let the user enter the size of the array?

To ask the user to enter the size of an array use: int size; cout<<"enter size of the array"; cin>>size; You can then use a for loop to have the user enter the values of the array.

How do you enter an array of size n?

Given a number N, the task is to create an array arr[] of size N, where the value of the element at every index i is filled according to the following rules: arr[i] = ((i – 1) – k), where k is the index of arr[i – 1] that has appeared second most recently.

How do you create an array can you declare an array without assigning the size of an array?

You can also use array literal and initialize array during declaration itself as shown below: int[] myarray = {1,3,5,7}; In the above statement, the length of the array is determined by the number of elements. Also, as you can see there is no need to use 'new'.

Can we take array size from user in C++?

In C++, we use sizeof() operator to find the size of desired data type, variables, and constants. It is a compile-time execution operator. We can find the size of an array using the sizeof() operator as shown: // Finds size of arr[] and stores in 'size' int size = sizeof(arr)/sizeof(arr[0]);


1 Answers

Ques1) Is this considered standard? My profs are contradictory.

Yes, this is completely valid. Note that you need to explicitly delete the memory pointed by arr using operator delete[]. It is much better to use a std::vector<int> which will perform the memory management for you.

You might be mistaken with variable length arrays(VLAs), which are not allowed in C++:

// same num as the one in your example
int arr[num]; // ERROR

This is valid in C but invalid in C++(C++14 will include VLAs, although they will have some differences with C VLAs).

Ques2) If it is standard, in that case, is it possible to extend the size of the array (or any array) after creation?

No, you can't extend it. You can allocate a bigger array, copy the elements and delete the previous one. Again, this is done automatically if you're using std::vector<int>.

Ques3) Again, if this expression is standard, then is it possible to use it within a function - eg. using a function to create such an array? (if so, how?)

Yes, of course:

int *allocate(size_t size) {
    return new int[size];
}

But again use std::vector:

int num;
cout << "How big an array? ";
cin >> num;
std::vector<int> arr(num); // num elements, all of them initialized to 0
// insert 42 at the end. reallocations(if any) are done automatically
arr.push_back(42); 
like image 68
mfontanini Avatar answered Sep 17 '22 15:09

mfontanini