tigerden
tigerden

Reputation: 728

Passing pointer to a function in C++

I wrote the following piece of code

#include <iostream>

using namespace std;

void temp (int * x)
{
    x=new int [2];
    x[0]=1;
    x[1]=2;
}

int main()
{
    int *ptr;
    temp(ptr);
    cout<<ptr[0]<<endl;
    cout<<ptr[1]<<endl;
    return 0;
}

Running it gives seg fault, so is the memory allocation which happens inside temp function local to function? The memory gets deallocated while returning from temp? I know, that to solve this problem, I need to pass pointer to pointer ptr, but still, why exactly does this thing not work?

Upvotes: 1

Views: 154

Answers (5)

Joseph Mansfield
Joseph Mansfield

Reputation: 110748

C++ answer:

You are passing the int* argument into temp by value. This means that you are copying ptr into the function, and the pointer x is a completely separate object. You then assign the result of new int[2] to this copy, but the ptr in main is left unaffected. To be able to modify the pointer passed as an argument, you need to take it by reference:

void temp (int*& x)
{ 
  // ...
}

This means that x now refers to the pointer that is passed as an argument. The alternative solution here is to return an int* instead:

int* temp()
{
    int* x = new int [2];
    x[0]=1;
    x[1]=2;
    return x;
}

int main()
{
    int *ptr = temp();
    // ...
}

However, the caller of temp might be unclear about the ownership of the int object. They need to delete[] it, but this isn't made explicit in the interface. Instead, you can return a std::unique_ptr.

Upvotes: 2

Suvarna Pattayil
Suvarna Pattayil

Reputation: 5239

int *ptr;

You created an automatic variable here and you passed it to

temp(ptr);

This is pass by copy so x will get the value of ptr and x scope is within the temp function. It is an automatic variable in that scope.When you return from temp its value is lost. Now, the memory allocated and pointed to by x is in no way reflected to ptr in main. (They are not connected)

You need to do temp(int*& ptr) i.e. pass by reference. Or temp(int** ptr) i.e. pass by address

Note: You have a memory leak in your temp

Upvotes: 1

john
john

Reputation: 8027

Think about this code

void temp(int x)
{
    x = 2;
}

int main()
{
    int y = 3;
    temp(y);
    cout << y << '\n';
}

What the output going to be 2 or 3? Of course it's three. Now what's the difference between this and your example? Nothing at all. Unless you use a reference everything in C++ is passed by value. x is a copy of y, so changes to x never affect y. This is true whetever the types involved, its true of ints and its true of pointers.

Upvotes: 2

Mats Petersson
Mats Petersson

Reputation: 129524

To alter something in a function in C, you need to pass a pointer to it. In this case, you want to alter a pointer, so you need a pointer to a pointer:

void temp (int** x)

then in the function use *x where you now have x (you will need (*x)[n], as *x[n] means something else)

Then call temp with:

temp(&ptr);

This should solve it in C, and will work in C++.

In C++, you could also pass a reference to a pointer:

 void temp(int*&x)

which will allow the syntax you have already to be used unchanged.

Upvotes: 2

unxnut
unxnut

Reputation: 8839

You need to pass a ** because you are allocating x inside of your function.

Upvotes: 0

Related Questions