0

i have some trouble in dynamic allocation with 'new' and reference. Please see a simple code below.

#include<iostream>
using namespace std;
void allocer(int *pt, int *pt2);
int main()
{
    int num = 3;
    int num2 = 7;
    int *pt=&num;
    int *pt2 = &num2;
    allocer(pt, pt2);
    cout << "1. *pt= " << *pt << "   *pt2= " << *pt2 << endl;
    cout << "2. pt[0]= " << pt[0] << "   pt[1]= " << pt[1] << endl;

}


void allocer(int *pt, int *pt2)
{
    int temp;
    temp = *pt;
    pt = new int[2];
    pt[0] = *pt2;
    pt[1] = temp;
    cout << "3. pt[0]= " << pt[0] << "   pt[1]= " << pt[1] << endl;
}

What i want to do is to make the function 'allocer' get 2 arguments which are the int pointer and allocate memory on one of them. As you can see, *pt becomes an array to take 2 integers. Inside of the function it works well which means the sentence that i marked 3. printed as what i intended. However, 1, 2 doesn't work. 1 prints the original datas(*pt= 3, *pt2= 7), 2 prints error(*pt= 3, *pt2= -81203841). How to solve it?

1

2 Answers 2

1

You are passing in the pt and pt2 variables by value, so any new values that allocer assigns to them is kept local to allocer only and not reflected back to main.

To do what you are attempting, you need to pass pt by reference (int* &pt) or by pointer (int** pt) so that allocer can modify the variable in main that is being referred to.

Also, there is no good reason to pass pt2 as a pointer at all since allocer doesn't use it as a pointer, it only dereferences pt2 to get at the actual int, so you should just pass in the actual int by value instead.

Try something more like this:

#include <iostream>
using namespace std;

void allocer(int* &pt, int i2);

int main()
{
    int num = 3;
    int num2 = 7;
    int *pt = &num;
    int *pt2 = &num2;
    allocer(pt, *pt2);
    cout << "1. *pt= " << *pt << " *pt2= " << *pt2 << endl;
    cout << "2. pt[0]= " << pt[0] << " pt[1]= " << pt[1] << endl;
    delete[] pt;
    return 0;
}

void allocer(int* &pt, int i2)
{
    int temp = *pt;
    pt = new int[2];
    pt[0] = i2;
    pt[1] = temp;
    cout << "3. pt[0]= " << pt[0] << " pt[1]= " << pt[1] << endl;
}

Or

#include <iostream>
using namespace std;

void allocer(int** pt, int i2);

int main()
{
    int num = 3;
    int num2 = 7;
    int *pt = &num;
    int *pt2 = &num2;
    allocer(&pt, *pt2);
    cout << "1. *pt= " << *pt << " *pt2= " << *pt2 << endl;
    cout << "2. pt[0]= " << pt[0] << " pt[1]= " << pt[1] << endl;
    delete[] pt;
    return 0;
}

void allocer(int** pt, int i2)
{
    int temp = **pt;
    *pt = new int[2];
    (*pt)[0] = i2;
    (*pt)[1] = temp;
    cout << "3. pt[0]= " << (*pt)[0] << " pt[1]= " << (*pt)[1] << endl;
}
Sign up to request clarification or add additional context in comments.

Comments

0

What you just did was that you dynamically allocated the pt that is inside the function. And this function variable pt is local and is not the same as the pt in the main function. What you can do is, that you can pass the address of the pointer itself if you want to dynamically allocate memory to that pointer.

Comments

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.