0

I faced some problem when i use Reference pointer type as parameter.

first, this is my code.

#include<iostream>
using namespace std;

void test(int*& ptr);

int main(void)
{
int* ptr = new int[1];
ptr[0] = 100;
cout << ptr[0];

test(ptr);
cout << ptr[0] << '\t' << ptr[1];
}

void test(int*& ptr)
{
int* tmp = new int[2];
tmp[0] = 1;
tmp[1] = 2;

int* tmp2 = ptr;
ptr = tmp;
cout << ptr[0] << '\t' << ptr[1];
delete[]tmp2;
}

and when compiled this code, the output is

100
1       2

this output is what i want to get

but when i debug this code,

Exception thrown: read access violation.

this error has occurred.

how to avoid this error, and what is my fault? :(

and what should i do when reallcoate parameter's memory without using reference type?

PangDae
  • 13
  • 4
  • Reference types aren't the problem; fundamental and basic understand of dynamic memory management and pointers *is*. Ex: `delete[]ptr;` destroys the memory you just allocated. Back in `main` you now have a dangling pointer. You're also leaking the original memory allocated in `main`. Unrelated, `tmp2` is worthless, and arguably so is `tmp`. – WhozCraig Jun 06 '21 at 11:47
  • single item in passed ptr and accessing ptr[1] means second item – Build Succeeded Jun 06 '21 at 11:53
  • oh, sorry guys i fixed my code... – PangDae Jun 06 '21 at 11:56
  • what i want to know is when i call a function that have parameter(memory allocated pointer(reference type)), can i change the address that paramter(pointer) point in function.. – PangDae Jun 06 '21 at 11:59

1 Answers1

1

The reference isn't the problem. It's your dynamic memory management that is dreadfully broken.


#include <iostream>
using namespace std;

void test(int *&ptr);

int main(void)
{
    int *ptr = new int[1]; // allocates a sequence of 1
    ptr[0] = 100;
    cout << ptr[0];

    test(ptr); // sends pointer to sequence by reference to test
    cout << ptr[0] << '\t' << ptr[1];
}

void test(int *&ptr)
{
    int *tmp = new int[2]; // allocates a sequence of size 2
    tmp[0] = 1;
    tmp[1] = 2;

    int *tmp2 = ptr; // stores original passed-in pointer to tmp2
    ptr = tmp; // assigns new sequence pointer to reference argument (leaks original)
    cout << ptr[0] << '\t' << ptr[1];

    delete[] ptr; // deletes the new sequence. (leaves dangling pointer)
}

What you seem to be trying to do is this:

#include <iostream>
using namespace std;

void test(int *&ptr);

int main(void)
{
    int *ptr = new int[1];
    ptr[0] = 100;
    cout << ptr[0] << '\n';

    test(ptr);
    cout << ptr[0] << '\t' << ptr[1] << '\n';
    delete [] ptr;
}

void test(int *&ptr)
{
    delete [] ptr;    // delete original sequence
    ptr = new int[2]; // create new sequence
    ptr[0] = 1;
    ptr[1] = 2;
}

Stop Using Raw Pointers

Alternatively, use smart pointers to manage this.

#include <iostream>
#include <memory>
using namespace std;

void test(std::unique_ptr<int[]>& ptr);

int main(void)
{
    std::unique_ptr<int[]> ptr = std::make_unique<int[]>(1);
    ptr[0] = 100;
    cout << ptr[0] << '\n';

    test(ptr);
    cout << ptr[0] << '\t' << ptr[1] << '\n';
}

void test(std::unique_ptr<int[]>& ptr)
{
    ptr = std::make_unique<int[]>(2);
    ptr[0] = 1;
    ptr[1] = 2;
}

Or better still, just use a std::vector<int>

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • thanks for answer, but , can i use pointer again that had deleted? – PangDae Jun 06 '21 at 12:01
  • what happened in memory when i delete memory allocation? is deleted pointer pointing NULL or garbage value? – PangDae Jun 06 '21 at 12:05
  • [What is a dangling pointer](https://stackoverflow.com/questions/17997228/what-is-a-dangling-pointer). – WhozCraig Jun 06 '21 at 12:06
  • than, can i use deleted pointer whenever i want with being sure avoid dangling pointer? – PangDae Jun 06 '21 at 12:08
  • https://stackoverflow.com/questions/28379457/reusing-a-pointer-after-delete/28379518 oh, i found my question's answer! thank you! – PangDae Jun 06 '21 at 12:10