0

I want to ask about passing pointers between functions or between objects or returning them .. I heard that passing or returning pointers in general (whether they point to an array or an object or whatever they are pointing at) isn't safe and the output isn't guaranteed.

now I've tried it and so far everything seems OK but I don't want my project to work by accident .. so can someone explain to me why not to pass or return pointers and what are the suggested solution (for example, what if I want to modify the same object (say object1) in a function in another object (function func in object2))?

also, I read in a tutorial that everything in c++ is pass by value? isn't passing pointers is called pass by reference?

Thanx everybody.

3
  • yes passing a pointer is essentially a pass by reference (for the value pointed to). But otherwise, pointers can be safely passed to and from objects, so long as there are well defined invariants they follow such that you know what can and can't happen to the pointers. Commented Oct 27, 2013 at 6:44
  • so I can use pointers safely if I know what I'm pointing to with no problems? Commented Oct 27, 2013 at 7:08
  • Yep! Be careful about memory ownership, though. You want to have clearly defined responsibilities so that it's obvious who should be deallocating the memory. You don't want two objects contending to delete it (or worse, none). Commented Oct 27, 2013 at 16:30

2 Answers 2

2

I want to ask about passing pointers between functions or between objects or returning them .. I heard that passing or returning pointers in general (whether they point to an array or an object or whatever they are pointing at) isn't safe and the output isn't guaranteed.

Where did you get that from? In general, it's not true and of course you can pass around pointers.

The tricky thing is managing ownership of (heap-allocated) objects, since somewhere you have to release the memory again. In other words: When you allocate memory with new, you will have to free it again with delete.

Example:

A* function1(A* a) {
    return a;
}

B* function2(B* b) {
    return new B(b);
}

function1 returns an existing pointer. Whoever owned the A object passed in will also own the returned one, as it is the same. This needs to be documented since this knowledge is essential for using function1!

function2 creates a new object of class B by coping its input argument. Whoever calls function2 will own the returned object and will be responsible to delete it when it's done. Again, this needs to be documented!

also, I read in a tutorial that everything in c++ is pass by value? isn't passing pointers is called pass by reference?

Technically, passing pointers is pass-by-value since the pointer itself gets copied. But since a pointer is a "reference type", you essentially get pass-by-reference with that.

Note that C++ also knows references (int&), which really is pass-by-reference.

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

Comments

1

Well, the honest answer is that people sometimes mean different things when they say "pass by reference".

But generally, when people say "pass by reference", they mean this:

void readInt(int &a) {
    cin >> a;
}

int a;
readInt(a);
cout << a;

And "pass by pointer" would be this:

void readInt(int *a) {
    cin >> *a;
}

int a;
readInt(&a);
cout << a;

Ultimately, you can use pointers for everything that references are used for (they other way around is mostly true).

Some people like references because they can use the . operator, like they normally do. Others prefer pointers because they are explicit.

Note that pointers are older than references (C does not have references), so C libraries will use pointers exclusively.

Most people (I think) use references when they can (like in your hypothetical example). The nice thing is that type safety will stop you if you confuse references and pointers.

2 Comments

thx for your explanation .. so basically there is nothing wrong of passing and returning pointers as long as I know what I'm pointing at .. I'm using a library that uses pointers pretty much so I don't want to confuse myself in my code about when I used references and when I used pointers, so is that ok?
Yes. If almost all of your functions deal with pointers, by all means go ahead and use pointers.

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.