1

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?

14
  • 6
    Note that the use of <iostream>, using namespace std, new, std::cout, operator << and std::endl mean that your program is C++ rather than C Commented May 9, 2013 at 13:44
  • Please remember, you should almost never handle memory manually in C++. I.e. no new or delete operators at all. Only STL containers and smart pointers. Commented May 9, 2013 at 13:45
  • and don't forget to delete[] Commented May 9, 2013 at 13:46
  • sorry its C++, not C... :) Commented May 9, 2013 at 13:50
  • @Mikhail: There will definitely be times you want to use new. I'd agree about delete; at least in C++11, std::unique_ptr and std::shared_ptr effectively automate deletion. But you still have to initialize them somehow. Commented May 9, 2013 at 13:52

5 Answers 5

2

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.

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

Comments

2

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.

1 Comment

I rather like the simplicity and relative jargon-free-ness of this explanation. Well, that and the fact that it makes pointers look less mysterious. :)
2

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.

Comments

1
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

Comments

0

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

2 Comments

It's C++, so void temp(int*& x) is better than an int**.
I missed seeing iostream and looked at the post title that asked for a solution in C.

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.