-3

This function reverses the array of pointers and returns it to the main. The main problem is that the code returns an Exception thrown: read access violation. fptr was 0xCCCCCCCC.

Minimal Description

What could be the source of error?

int* mirror(int* p[], int n) {
    int* ptr,* fptr;
    int swap;
    ptr = p[0];
    fptr = p[n-1];
    while (fptr > ptr) {
        swap = *ptr;
        *ptr = *fptr;
        *fptr = swap;
        ptr++;
        fptr--;
    }
    return *p;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
mohsen
  • 65
  • 6
  • please provide a [mcve] – 463035818_is_not_an_ai Feb 23 '20 at 14:03
  • 1
    This code is totally broken. `swap = *ptr;` assigns an integer, not a pointer. `ptr++` increments a wrong pointer (the first element of `p` instead of a pointer to the first element of `p`). No element of the `p` array changes value, ever. – n. m. could be an AI Feb 23 '20 at 14:07
  • you shoud replace your argument to be a pointer to pointer to be able to navigate it as you want: int* mirror(int** p, int n){ – user2019716 Feb 23 '20 at 14:10
  • @user2019716 it won't change a thing, the two definitions are equivalent – n. m. could be an AI Feb 23 '20 at 14:12
  • `0xCCCCCCCC` is uninitialized stack memory:[https://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations](https://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations) – drescherjm Feb 23 '20 at 14:13
  • @n. 'pronouns' m, yep but he might have seen that p[n-1] != p+n-1 – user2019716 Feb 23 '20 at 14:25
  • then he could have defined "int\*\*ptr = p; int\*\*fptr=p+n-1;" and iterate with them instead of incrementing the pointers the table contains. – user2019716 Feb 23 '20 at 14:31

4 Answers4

1

This is the problem:

while (fptr > ptr) { ... }

ptr is the first elements (the first pointer), and fptr is the last element (the last pointer), and you are going through the array while the first element is less than the last element, but this imply that the elements in the array are inserted in order of address, which i believe is not..

Instead you should use the dimension (n) in order to do that:

int** mirror(int* p[], int n) { // return a pointer to integers pointer, not a pointer to integer
    for(int i = 0; i < n/2 ; i++){ // go through the first half of the array
       int* tmp = p[i];  // and those three lines swap the current and the n - current elements
       p[i] = p[n-i-1];
       p[n] = tmp;
    }
    return p;
}
Alberto Sinigaglia
  • 12,097
  • 2
  • 20
  • 48
0

For starters the return type int * of the function does not make sense.

int* mirror(int* p[], int n) {

It is better to declare the function as having the return type void.

If you have an array of pointers of the type int * then a pointer to its elements has the type int **.

Moreover the user can pass 0 as the second argument. You should check this case.

The reason of the exception can be the array contains invalid pointers that point outside of the original array.

Also if the array of pointers is formed such a way that it is not necessary that for example the first pointer points to the first element of the original array then you function will not work.

The function can be defined like

void mirror( int * p[], size_t n ) 
{
    if ( n != 0 )
    {
        for ( int **first = p, **last = p + n; first < --last; ++first )
        {
            int tmp = **first;
            **first = **last;
            **last = tmp;
        }
    }
}

Here is a demonstrative program

#include <iostream>

void mirror( int * p[], size_t n ) 
{
    if ( n != 0 )
    {
        for ( int **first = p, **last = p + n; first < --last; ++first )
        {
            int tmp = **first;
            **first = **last;
            **last = tmp;
        }
    }
}

int main() 
{
    const size_t N = 5;
    int a[N] = { 10, 30, 30, 40, 50 };
    int * b[N] = { a, a + 1, a + 2, a + 3, a + 4 };

    for ( const auto &item : a )
    {
        std::cout << item << ' ';
    }
    std::cout << '\n';

    mirror( b, N );

    for ( const auto &item : a )
    {
        std::cout << item << ' ';
    }
    std::cout << '\n';

    return 0;
}

The program output is

10 30 30 40 50 
50 40 30 30 10

Pay attention to that instead of swapping elements "manually" you could use the standard function std::swap.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

The problem is that you've used the wrong types. The correct version would be:

    int **ptr, **fptr;
    int* swap;
    ptr = &p[0];
    fptr = &p[n - 1];

With those changes, your function works as expected although what it returns is unusable for most purposes. Either return p (int**) or make it void.

void mirror(int* p[], int n) {
    if(n > 0) {                  // check this or you risk undefined behavior
        int** ptr = &p[0];
        int** fptr = &p[n-1];
        int* swap;
        while(ptr < fptr) {
            swap = *ptr;
            *ptr = *fptr;
            *fptr = swap;
            ptr++;
            fptr--;
        }
    }
}

Note that there is already a standard utility for swapping two values called std::swap:

void mirror(int* p[], int n) {
    if(n > 0) {
        for(int **ptr = &p[0], **fptr = &p[n - 1]; ptr < fptr; ++ptr, --fptr) {
            std::swap(*ptr, *fptr);
        }
    }
}

As a last note, there's also a standard utility for reversing the values in a container called std::reverse which does what your mirror function does.

Instead of

mirror(arr, std::size(arr));

do

std::reverse(std::begin(arr), std::end(arr));
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
-1

try navigate your array as follow:

void mirror(int* p, int n){
  int* ptr,* fptr;
  int swap;
  ptr = p;
  fptr = p+n-1;
  while (fptr != ptr) {
    swap = *ptr;
    *ptr = *fptr;
    *fptr = swap;
    ptr++;
    fptr--;
  }
}
user2019716
  • 587
  • 4
  • 13