Dynamic array without new ones (C ++)

I'm new to C ++ and this is a very simple question.

In C ++ there are only two ways to create dynamic arrays (read the book, correct me if I'm wrong), using memory allocation either by an operator new

or malloc()

which is taken from C.

When declaring an array, int array[size]

square brackets []

must have const

.

However, the following code size

has a variable unsigned int

.

#include<iostream>

int main() {
    using namespace std;
    unsigned int size;
    cout<<"Enter size of the array : ";
    cin>>size;
    int array[size]; // Dynamically Allocating Memory
    cout<<"\nEnter the elements of the array\n";
    // Reading Elements
    for (int i = 0; i < size; i++) {
        cout<<" :";
        cin>>array[i];
    }
    // Displaying Elements
    cout<<"\nThere are total "<<size<<" elements, as listed below.";
    for (int j = 0; j < size; j++) {
        cout<<endl<<array[j];
    }
    return 0;
}

      

There are no errors when compiling g ++ and, besides, the program works fine.

Question 1: Could this be another way to create a dynamic array?

Question 2: Is the code correct?

Question 3: If []

can only contain const

, why does the code work?

+3


source to share


2 answers


  • That's right, you've discovered the extensiongcc

    . This was allowed in C for a long time, but it didn't make it into the C ++ standard until C ++ 14.
  • Yes, the code is correct if you think you will be fine with non-portable extensions to the language.
  • This applies to standard compilers. You can tell gcc

    that you do not want to use extensions by checking the -std=c++98

    or -std=c++11

    .


If you need a dynamically sized array in C ++, it is better to use std::vector<T>

. This gives you the flexibility of a dynamically allocated array and provides management of the allocated memory for you.

+4


source


In C ++ there are only two ways to create dynamic arrays (read in the book, correct me if I'm wrong), using memory allocation either with a new operator or malloc () which is taken from C.

It depends on what you mean by dynamic .

In a strictly technical perspective, in the context of an object's lifetime, dynamic means something with dynamic storage duration. Storage duration is the lifetime of an object after it is created. An object with dynamic storage duration will continue to run until it is explicitly destroyed. (Note that this explicit destruction can be confusing with smart pointers like std::unique_ptr

)

However, this doesn't sound like what you are really asking about in this question. You seem to be asking:

If I want to create a container for objects, but I don't know how big that container should be before runtime, new

and malloc

my only choice?



The answer to this question is no . And in fact, in modern C ++, usage new

should be the last choice, and usage malloc

should be even more recent than that.

Instead of creating an array of style elements N

, use instead vector

:

std::vector <int> myInts;
for (int i = 0; i < 10; ++i)
  myInts.push_back (i);

      

This has several advantages:

  • You don't need delete

    anything
  • You don't need to know how many elements will be placed in vector

    before you create vector

    - just keep adding elements until you are done
  • vector

    is guaranteed to have conditional storage, that is, elements can be accessed using operator[]

    just like a C-style array
+1


source







All Articles