Static array vs. dynamic array in C++

后端 未结 11 1536
遥遥无期
遥遥无期 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 10:58

    static is a keyword in C and C++, so rather than a general descriptive term, static has very specific meaning when applied to a variable or array. To compound the confusion, it has three distinct meanings within separate contexts. Because of this, a static array may be either fixed or dynamic.

    Let me explain:

    The first is C++ specific:

    • A static class member is a value that is not instantiated with the constructor or deleted with the destructor. This means the member has to be initialized and maintained some other way. static member may be pointers initialized to null and then allocated the first time a constructor is called. (Yes, that would be static and dynamic)

    Two are inherited from C:

    • within a function, a static variable is one whose memory location is preserved between function calls. It is static in that it is initialized only once and retains its value between function calls (use of statics makes a function non-reentrant, i.e. not threadsafe)

    • static variables declared outside of functions are global variables that can only be accessed from within the same module (source code file with any other #include's)

    The question (I think) you meant to ask is what the difference between dynamic arrays and fixed or compile-time arrays. That is an easier question, compile-time arrays are determined in advance (when the program is compiled) and are part of a functions stack frame. They are allocated before the main function runs. dynamic arrays are allocated at runtime with the "new" keyword (or the malloc family from C) and their size is not known in advance. dynamic allocations are not automatically cleaned up until the program stops running.

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

    Static arrays are allocated memory at compile time and the memory is allocated on the stack. Whereas, the dynamic arrays are allocated memory at the runtime and the memory is allocated from heap.

    int arr[] = { 1, 3, 4 }; // static integer array.   
    int* arr = new int[3]; // dynamic integer array.
    
    0 讨论(0)
  • 2020-11-22 11:02

    I think in this context it means it is static in the sense that the size is fixed. Use std::vector. It has a resize() function.

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

    Static Array :

    1. Static arrays are allocated memory at compile time.
    2. Size is fixed.
    3. Located in stack memory space.
    4. Eg. : int array[10]; //array of size 10

    Dynamic Array :

    1. Memory is allocated at run time.
    2. Size is not fixed.
    3. Located in Heap memory space.
    4. Eg. : int* array = new int[10];
    0 讨论(0)
  • 2020-11-22 11:12

    I think the semantics being used in your class are confusing. What's probably meant by 'static' is simply "constant size", and what's probably meant by "dynamic" is "variable size". In that case then, a constant size array might look like this:

    int x[10];
    

    and a "dynamic" one would just be any kind of structure that allows for the underlying storage to be increased or decreased at runtime. Most of the time, the std::vector class from the C++ standard library will suffice. Use it like this:

    std::vector<int> x(10); // this starts with 10 elements, but the vector can be resized.
    

    std::vector has operator[] defined, so you can use it with the same semantics as an array.

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

    You could have a pseudo dynamic array where the size is set by the user at runtime, but then is fixed after that.

    int size;
    cin >> size;
    int dynamicArray[size];
    
    0 讨论(0)
提交回复
热议问题