views:

460

answers:

6

Hi, I'm having trouble understanding the following bit of code that I was hoping would create an array on the heap and fill it with the characters 9 down to 0 (I know I could just index the array like a normal stack array with [] notation to do this but I'm doing it this way to try to understand pointers in more depth):

int *ptrHeapArray = new int[10]; 

    for(int f=9; f>=0 ;f--)
    {
     *ptrHeapArray = f;
     ptrHeapArray++;
    }
    for(int f=0; f<10; f++)
     cout << ptrHeapArray[f]  << "\n";

It prints out compleletly unexpected values.

As I understand the above, the 'new' command creates an array on the heap and sends me back a pointer to the address where the array is. Since the pointer I assign (ptrHeapArray) is of int size I assumed I could use pointer post incrementing to navigate through the array. However the results indicate that my assumptions are wrong.

This got me to thinking that perhaps the pointer passed back by the 'new' keyword is just a pointer to the whole array and can't be used to step through the array for some reason. So I tried creating another pointer to the pointer returned by the 'new' keyword and used that to do my array population:

int *ptrHeapArray = new int[10];  //array to hold FRANK data in 32 bit chunks
int *ptrToHeapArrayPointer = ptrHeapArray;

for(int f=9; f>=0 ;f--)
{
 *ptrToHeapArrayPointer = f;
 ptrToHeapArrayPointer++;
}
for(int f=0; f<10; f++)
 cout << ptrHeapArray[f]  << "\n";

This worked fine. Can anyone explain to me why I had to do this and couldn't just have used the pointer passed backed to me by the 'new' keyword?

Thanks

+6  A: 

The line

ptrHeapArray++;

in your first for loop increments the pointer, such that it doesn't point to the beginning of the array anymore.

The line

int *ptrHeapArray = new int[10];

allocates the memory for 10 integers and points ptrHeapArray to the beginning of that memory. In your for loop you then move this pointer. When ptrHeapArray points to the third of the integers:

[0] [1] [2] [3] [4]
 ^       ^       ^
orig.    |       |
         |       +-- ptrHeapArray[2]
         |
         +-- ptrHeapArray now points here

then ptrHeapArray[2] would give you the integer at the position orignally numbered with 4.

balpha
It basically clobbers the head of your array
Nathan Fellman
@Nathan: don't put it that way. You're not distinguishing between the pointer and what it points to. This is a distinction new programmers often find difficult. "The head of the array" is not clobbered(overwritten) at all. It's unchanged. There is simply no pointer to it anymore.
MSalters
+4  A: 

You are modifying the pointer in code. After the first loop in the first snippet, the pointer will point to the end of the array rather than the beginning. To make things clearer, this would work too (not suggested but demonstrates the behavior):

int *ptrHeapArray = new int[10]; 

for(int f=9; f>=0 ;f--)
{
    *ptrHeapArray = f;
    ptrHeapArray++;
}

ptrHeapArray -= 10; // reset the pointer to its original location

for(int f=0; f<10; f++)
    cout << ptrHeapArray[f]  << "\n";
Mehrdad Afshari
A: 

When you do ptrHeapArray++ it inscrements ptrHeapArray. When you come to print out the data ptrHeapArray no longer points to the start of the array.

Tom Carter
A: 

The problem is that in your first example ptrHeapArray is initially set to the beginning of the array. As you iterate through your loop you are incrementing the pointer, and by the end of the for loop you are pointing to the last element in the array. When you go through the for loop to display all of your values, you are indexing to values past the end of the array since ptrHeapArray is pointing to the last element in the array you allocated.

It is also important to remember, that you should probably make sure not to loose the original pointer you got back from using the new operator so that you can properly free up the memory that was allocated on the heap.

heavyd
+1  A: 

Your ptrToHeapArrayPointer is misnamed, it is just a standard int ptr that you've pointed to the same place as ptrHeapArray. If you renamed it to currentPositionPtr your code might make more sense to you.

Patrick
A: 

In the first loop you incremented your pointer ptrHeapArray until the end of array. So after executing the first for loop your pointer is pointing to end of the array and not at the beggining. Hence, when you are trying to print the contents of the array, you are accessing invalid memory allocations and the behavior will be unexpected. In the second case, you are taking a copy of your starting address before assigning values to the array. So when you try to print the contents using the copy, it will be pointing to the beginning of the array.

Naveen