Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flipping an array using pointers

#include <iostream>
using namespace std;

int* flipArray(int input[], int n)
{
    int output[n];
    int pos = 0;
    for (int i = n-1; i >= 0; i--)
    {
        output[pos++] = input[i];
    }
    int* p = output;
    for (int k = 0; k < n; k++)
        cout << *p-k << endl << endl;
    return p;
}

int main()
{
    const int SIZE = 5;
    int firstArray[SIZE];
    for (int n = 0; n < SIZE; n++)
    {
        firstArray[n] = n+1;
    }
    int* a;
    a = flipArray(firstArray, SIZE);
    for (int j = 0; j < SIZE; j++)
        cout << *a-j << endl;

    cout << endl;
    cout << *a << '\t' << *a+1 << '\t' << *a+2;
    return 0;
}

I am attempting to flip firstArray using a function that returns a pointer, but I am struggling to understand how accessing an index using a pointer works.

Here is why I am confused: Within the function flipArray, the following for-loop:

for (int k = 0; k < n; k++)
    cout << *p-k << ' ';

prints "5 4 3 2 1" to the console. It was my understanding that I should be accessing an element of a vector with *(p+k), not *(p-k). If I print *(p+k), "5 6 7 8 9" is printed to the console. If I print the array without pointers and using k as the index location, "5 4 3 2 1" is printed to the console.

Within my main function, however, the values of *a which is assigned pointer p from the flipArray function, I do not get the same results:

for (int j = 0; j < SIZE; j++)
    cout << *a-j << endl;

prints 5 0 -1 -2 -3 to the console, and

    for (int j = 0; j < SIZE; j++)
    cout << *a+j << endl;

prints 5 2 3 4 5 to the console.

Further, I thought that the pointer location of *p and the pointer of location of *a should be the same! But when I print the address &p in the function, I get the location of 0x28fde0, and when I print the address of &a in the main, I get the location 0x28fedc. Of course, these were done during the same run.

Could someone tell me where I have gone astray? Thanks!


Thanks to everyone for the informative answers.

I have updated my solution, and it is now returning what I would expect it to. I have a new question about memory leaks and when pointers need to be deleted.

int* flipArray(int input[], int n)
{
    int* output = new int[n];
    int pos = 0;
    for (int i = n-1; i >= 0; i--)
        output[pos++] = input[i];
    return output;
}

int main()
{
    const int SIZE = 5;
    int firstArray[SIZE];
    for (int n = 0; n < SIZE; n++)
    {
        firstArray[n] = n+1;
    }
    int* a;
    a = flipArray(firstArray, SIZE);
    for (int j = 0; j < SIZE; j++)
        cout << a[j] << " "; // can also be written as *(a+j), which is more prone to bugs
    delete [] a;
    return 0;
}

Will the pointer output be deleted when the function flipArray returns? If not, how should I delete output while also returning it? Is deleting the pointer a in my main function the same thing as deleting output, because they point to the same location?

like image 423
greedIsGoodAha Avatar asked Apr 20 '15 17:04

greedIsGoodAha


People also ask

Can you use pointers with arrays?

As an alternative to using the traditional array index notation, you can also use a pointer to access and interact with the elements in an array. The process begins by making a copy of the pointer that points to the array: int *ptr = A; // ptr now also points to the start of the array.


1 Answers

It has been pointed out that your main problem is coming from the operator precedence. The * operator in *p - k is evaluated before the -. This means that k will be subtracted from the value of the int pointed at by p.

This is a huge pain, which is why the braces pointer[k] are commonly used. There are situations where using pointer arithmetic *(pointer + k) makes more sense, but it can be a source of bugs.

One point to note here: it is always better to use parenthesis even if you are not sure whether or not you need them.


You do have a second problem:

Here you are declaring output on the stack as a local variable, then you are returning output. When you return back to the previous stack frame, this pointer will be pointing to a decallocated buffer:

int* flipArray(int input[], int n)
{
    int output[n]; // allocated on the stack
    int pos = 0;
    for (int i = n-1; i >= 0; i--)
    {
        output[pos++] = input[i];
    }
    int* p = output;
    for (int k = 0; k < n; k++)
        cout << *p-k << endl << endl;
    return p; // this stack frame ends.
}

This means the contents of the buffer can be overwritten if the space the buffer is using is reallocated. Use new to allocate on the heap:

int* output = new int[n];

make sure to call delete on the pointer when you are done using it.

This bug can even present security vulnerabilities in your applications, so make sure you know when to allocate on the heap in C++.


Update:

Question: When this function returns, the array still exists in memory, and it's location is stored in the pointer a. Does returning the value output delete it? If not, will deleting the pointer a when I am done with it in the main function serve the same purpose?

When you delete the pointer, the memory pointed to that pointer is deallocated and the pointer is left dangling. A reference to a deleted pointer is pointing at memory that is technically free, which is bad. If the allocator library decides that it wants to reuse that space, your buffer, which is now in free space, will be reallocated. This means your buffer will lose all data integrity and the data inside of it cannot be trusted.

A common practice is to assign pointers to NULL when you are done using them. This way your program will crash and you will know where your bug is:

int* p = new int[10];
...
delete p;
p = NULL;
...
p[0] = 0; // this will now crash because you are accessing NULL.
like image 180
John Avatar answered Sep 18 '22 06:09

John