Static array vs. dynamic array in C++

后端 未结 11 1545
遥遥无期
遥遥无期 2020-11-22 10:43

What is the difference between a static array and a dynamic array in C++?

I have to do an assignment for my class and it says not to use static arrays, only dynamic

相关标签:
11条回答
  • 2020-11-22 11:17

    Static array :Efficiency. No dynamic allocation or deallocation is required.

    Arrays declared in C, C++ in function including static modifier are static. Example: static int foo[5];

    0 讨论(0)
  • 2020-11-22 11:18

    It's important to have clear definitions of what terms mean. Unfortunately there appears to be multiple definitions of what static and dynamic arrays mean.

    Static variables are variables defined using static memory allocation. This is a general concept independent of C/C++. In C/C++ we can create static variables with global, file, or local scope like this:

    int x[10]; //static array with global scope
    static int y[10]; //static array with file scope
    foo() {
        static int z[10]; //static array with local scope
    

    Automatic variables are usually implemented using stack-based memory allocation. An automatic array can be created in C/C++ like this:

    foo() {
        int w[10]; //automatic array
    

    What these arrays , x, y, z, and w have in common is that the size for each of them is fixed and is defined at compile time.

    One of the reasons that it's important to understand the distinction between an automatic array and a static array is that static storage is usually implemented in the data section (or BSS section) of an object file and the compiler can use absolute addresses to access the arrays which is impossible with stack-based storage.

    What's usually meant by a dynamic array is not one that is resizeable but one implemented using dynamic memory allocation with a fixed size determined at run-time. In C++ this is done using the new operator.

    foo() {
       int *d = new int[n]; //dynamically allocated array with size n     
    

    But it's possible to create an automatic array with a fixes size defined at runtime using alloca:

    foo() {
        int *s = (int*)alloca(n*sizeof(int))
    

    For a true dynamic array one should use something like std::vector in C++ (or a variable length array in C).

    What was meant for the assignment in the OP's question? I think it's clear that what was wanted was not a static or automatic array but one that either used dynamic memory allocation using the new operator or a non-fixed sized array using e.g. std::vector.

    0 讨论(0)
  • 2020-11-22 11:20

    Local arrays are created on the stack, and have automatic storage duration -- you don't need to manually manage memory, but they get destroyed when the function they're in ends. They necessarily have a fixed size:

    int foo[10];
    

    Arrays created with operator new[] have dynamic storage duration and are stored on the heap (technically the "free store"). They can have any size, but you need to allocate and free them yourself since they're not part of the stack frame:

    int* foo = new int[10];
    delete[] foo;
    
    0 讨论(0)
  • 2020-11-22 11:22

    Yes right the static array is created at the compile time where as the dynamic array is created on the run time. Where as the difference as far is concerned with their memory locations the static are located on the stack and the dynamic are created on the heap. Everything which gets located on heap needs the memory management until and unless garbage collector as in the case of .net framework is present otherwise there is a risk of memory leak.

    0 讨论(0)
  • 2020-11-22 11:22

    static arrary meens with giving on elements in side the array

    dynamic arrary meens without giving on elements in side the array

    example:

         char a[10]; //static array
           char a[];  //dynamic array
    
    0 讨论(0)
提交回复
热议问题