Declaring array of int

前端 未结 8 494
-上瘾入骨i
-上瘾入骨i 2020-12-07 13:11

Is there any difference between these two declarations?

int x[10];

vs.

int* x = new int[10];

I suppose th

相关标签:
8条回答
  • 2020-12-07 14:01

    According to the standard, we should actually distinguish between three different types of array declarations:

    int x[10];
    
    void method() {
         int y[10];
         int *z = new int[10];
         delete z;
    }
    

    The first declaration, int x[10], uses static storage duration, defined by cppreference as: "The storage for the object is allocated when the program begins and deallocated when the program ends. Only one instance of the object exists. All objects declared at namespace scope (including global namespace) have this storage duration, plus those declared with static or extern."

    The second one, int y[10], uses automatic storage duration, defined by cppreference as: "The object is allocated at the beginning of the enclosing code block and deallocated at the end. All local objects have this storage duration, except those declared static, extern or thread_local."

    The third one, int *z = new int[10], is usually referred to as dynamic memory allocation, and is actually a two-step sequence:

    • First, the operator new is called, which allocated memory dynamically using either the default allocation methods of the standard library or a user defined implementation (since new can be overridden during run-time). The allocated memory will be sufficient to fit the N elements allocated, plus any additional memory required to keep metadata for the given allocation (so that it can be later successfully freed).
    • Second, if the first step is successful, we then proceed to initialize or construct each object in the array.

    As already mentioned by other comments, these types of declaration have their subtle differences, but the most common ones are:

    1. On most modern operating systems:

      • automatic storage is usually allocated on the stack, which is (generally speaking) a thread-specific pre-allocated memory space using a LIFO mechanism
      • static storage uses pre-allocated memory space reserved inside the executable (more specifically .BSS and .DATA segments, depending if the variable is zero initialized or not)
      • dynamic memory is allocated using heap memory, and is subject to the mercy of the system's RAM management system and other mechanisms such as paging.
    2. Dynamically allocated memory should be explicitly delete-ed by the programmer, whereas static and automatic storage variables are taken care of by the "environment"

    3. Static and automatic storage variables are limited to a specific scope, whereas dynamically allocated memory has no bounds, meaning, a variable declared in one module, can be passed to any other module that operates in the same address space

    4. When allocating an array using new[], the size can be 0

    5. (As already pointed out by @R Sahu) The types of &x and &z are different:

      • &x is int (*)[10]
      • &z is int **
    0 讨论(0)
  • 2020-12-07 14:04

    If you want to size an array dynamically, e.g.:

    void doSomething(int size)
    {
        int x[size];               // does not compile
        int *z  = new int[size];
        //Do something interesting ...
        doMore(z, size);
    }
    

    then x will not compile in C++, so you have to use z. The good news is that you can now use z, in most cases, as though it were statically allocated, e.g.:

    void doMore(int anArray[], int size)
    {
        // ...
    }
    

    takes z as an argument, treating the pointer as an array.

    0 讨论(0)
提交回复
热议问题