Whats the difference? Pointer to an array vs regular array

后端 未结 9 1685
梦如初夏
梦如初夏 2021-01-04 21:40

I\'m familiar with Java and trying to teach myself C/C++. I\'m stealing some curriculum from a class that is hosting their materials here. I unfortunately can\'t ask the tea

相关标签:
9条回答
  • 2021-01-04 22:08

    On the one hand:

    int ar[10];
    

    is using memory allocated on the stack. You can think of it also locally available and while it is possible to pass a pointer / reference to otehr functions, the memory will be freed as soon as it goes out of scope (in your example at the end of the main method but that's usually not the case).

    On the other hand:

    int ar* = new int[10];
    

    allocates the memory for the array on the heap. It is available until your whole program exits or it is deleted using

    delete[] ar;
    

    note, that for delete you need the "[]" if and only if the corresponding new has had them, too.

    0 讨论(0)
  • 2021-01-04 22:09

    int array[10]; declares the array size statically, that means it is fixed - which is the only major difference. It also might be allocated to be inside the function's stack frame, i.e. on the program's stack. You do not need to worry about using delete [] on that kind of array, in fact, you might crash the program if you delete it.

    When you use operator new, you allocate memory dynamically which could be slower and the memory usually comes from the heap rather than the program's stack (though not always). This is better in most cases, as you are more limited in the stack space than the heap space. However, you must watch out for memory leaks and delete[] your stuff when you don't need it anymore.

    As to your array being filled with zeros, what your class material does not say is that you have to do this:

    int *arr = new int[20]; // old array
    //do magic here and decide that we need a bigger array
    int *bigger = new int[50]; // allocate a bigger array
    for (int i = 0; i < 20; i++) bigger[i] = arr[i]; // copy the elements from the old array into the new array
    delete[] arr;
    arr = bigger;
    

    That code extends the array arr by 30 more elements. Note that you must copy the old data into the new array, or else it will not be there (in your case, everything becomes 0).

    0 讨论(0)
  • 2021-01-04 22:13

    First, I'd look for some other place to learn C++. The page you cite is very confusing, and has little to do with the way one actually programs in C++. In C++, most of the time, you'd use std::vector for an array, not the complex solutions proposed on the page you cite. In practice, you never use operator new[] (an array new).

    In fact, std::vector is in some ways more like ArrayList than simple arrays in Java; unlike an array in Java, you can simply grow the vector by inserting elements into it, preferrably at the end. And it supports iterators, although C++ iterators are considerably different than Java iterators. On the other hand, you can access it using the [] operator, like a normal array.

    The arrays described on the page you cite are usually called C style arrays. In C++, their use is mostly limited to objects with static lifetime, although they do occasionally appear in classes. In any case, they are never allocated dynamically.

    0 讨论(0)
  • 2021-01-04 22:18

    There is a difference but not in the area that you point to. *pointerArray will point to the beginning of a block of memory of size 10 bytes. So will array. The only difference will be where it is stored in memory. pointerArray is dynamically assigned memory (at run-time) and hence will go on the heap, while array[10] will be allocated at compile-time and will go to the stack.

    0 讨论(0)
  • 2021-01-04 22:19

    My understanding is that an array in C is simply a reference to the memory address of the first element in the array.

    So, what is the difference between int *pointerArray = new int[10]; and int array[10]; if any?

    What you mention is the reason for much confusion in any C/C++ beginner.

    In C/C++, an array corresponds to a block of memory sufficiently large to hold all of its elements. This is associated to the [] syntax, like in your example:

    int array[10];
    

    One feature of C/C++ is that you can refer to an array by using a pointer to its type. For this reason, you are allowed to write:

    int* array_pointer = array;
    

    which is the same as:

    int* array_pointer = &array[0];
    

    and this allows to access array elements in the usual way: array_pointer[3], but you cannot treat array as a pointer, like doing pointer arithmetics on it (i.e., array++ miserably fails).

    That said, it is also true that you can manage arrays without using the [] syntax at all and just allocate arrays by using malloc and then using them with raw pointers. This makes the "beauty" of C/C++.

    Resuming: a distinction must be made between the pointer and the memory that it points to (the actual array):

    1. the [] syntax in declarations (i.e., int array[10];) refers to both aspects at once (it gives you, as to say, a pointer and an array);

    2. when declaring a pointer variable (i.e., int* p;), you just get the pointer;

    3. when evaluating an expression (i.e., int i = p[4];, or array[4];), the [] just means dereferencing a pointer.

    Apart from this, the only difference between int *pointerArray = new int[10]; and int array[10]; is that former is allocated dynamically, the latter on the stack.

    0 讨论(0)
  • 2021-01-04 22:26

    It is true that you can get most of array functionality by using a pointer to its first element. But compiler knows that a static array is composed of several elements and the most notable difference is the result of the sizeof operator.

    sizeof(pointerArray) = sizeof int*

    sizeof(array) = 10 * sizeof int

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