Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the difference? Pointer to an array vs regular array

Tags:

c++

c

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 teacher since I'm not in the class. My concern is with the section under "dynamically declared arrays":

If you want to be able to alter the size of your array at run time, then declare dynamic arrays. These are done with pointers and the new operator. For the basics on pointers, read the pointers section.

Allocate memory using new, and then you access the array in the same way you would a static array. For example,

int* arrayPtr = new int[10]; for (int i = 0; i < 10; i++) { arrayPtr[i] = i; }

The memory picture is identical to the static array, but you can change the size if you need to. Don't forget you must deallocate the memory before allocating new memory (or you will have a memory leak).

delete [] arrayPtr; // the [] is needed when deleting array pointers arrayPtr = new int[50]; . . .

When you're completely done with the array, you must delete its memory:

delete [] arrayPtr;

Dynamic multi-dimensional arrays are done in a similar manner to Java. You will have pointers to pointers. For an example, see a

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?

I've done some tests that seem to indicate that they do the exact same thing. Is the website wrong or did I read that wrong?

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char** argv) {

    // Initialize the pointer array
    int *pointerArray = new int[10];
    for (int i = 0; i < 10; i++){

        pointerArray[i] = i;
    }

    // Initialize the regular array
    int array[10];
    for (int i = 0; i < 10; i++){

        array[i]= i;
    }

    cout << *(pointerArray + 5) << endl;
    cout << *(array + 5) << endl;

    cout << pointerArray[5] << endl;
    cout << array[5] << endl;

    cout << pointerArray << endl;
    cout << array << endl;

    return 0;
}

Output:

5
5
5
5
0x8f94030
0xbfa6a37c

I've tried to "dynamically re-size" my pointer array as described on the site, but my new (bigger) pointer array ends up filled with 0's which is not very useful.

like image 725
James T Avatar asked May 25 '11 09:05

James T


1 Answers

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.

like image 51
sergio Avatar answered Oct 22 '22 14:10

sergio