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?

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. Commented Jun 6, 2021 at 11:47
  • single item in passed ptr and accessing ptr[1] means second item Commented Jun 6, 2021 at 11:53
  • oh, sorry guys i fixed my code... Commented Jun 6, 2021 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.. Commented Jun 6, 2021 at 11:59

1 Answer 1

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>

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for answer, but , can i use pointer again that had deleted?
what happened in memory when i delete memory allocation? is deleted pointer pointing NULL or garbage value?
than, can i use deleted pointer whenever i want with being sure avoid dangling pointer?
stackoverflow.com/questions/28379457/… oh, i found my question's answer! thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.