0

If I have a member function that returns a pointer that I'm using for accessing other objects...

What's the best way to optimize my code? Should I return const int* or const int*& ?

EDIT. What I mean is: does returning a pointer involve a copy? Would be returning a reference to the pointer easy to optimize for the compiler? I don't assign the returned value to any variable:

my_object.get_ptr()->function();
jbgs
  • 2,795
  • 2
  • 21
  • 28
  • It is just integer. What is the overhead you see which you want to optimize? `const int *` is just fine and simple – theAlias Nov 14 '13 at 19:43
  • Those are extremely different types. In one case, it is a pointer to an `int` that you don't give the rights to modify. In the other case, it is the right to read or modify a pointer stored somewhere else that points at an `int` that the storage elsewhere does not have rights to modify. There are very few situations where the difference between those choices is "which is faster". It would be like asking "what is better place to get my fingernails trimmed, a rocket ship or a submarine?" – Yakk - Adam Nevraumont Nov 14 '13 at 19:46
  • Forget optimizing, emphasize on robustness, correctness and quality. Your kind of optimization is a "micro-optimization" and the effects from the optimization will be negligible compared to a design or larger optimization. Use a profiler before worrying about these kinds of optimizations. – Thomas Matthews Nov 14 '13 at 19:52

2 Answers2

1

I don't see how it would be an optimization either way. I would choose the way that makes your code easier to understand and correct. Take a look at the following. http://markgodwin.blogspot.com/2009/08/c-reference-to-pointer.html

Clearly there is a time where a reference to a pointer must be used (or vice versa) in order for the program to be correct! However, choosing between pass by value and pass by reference is in this case not really an optimization question. Which way will result in the behavior that you expect?

shawn1874
  • 1,288
  • 1
  • 10
  • 26
  • +1 for mentioning correctness and readability. Too many people spend too much time with micro-optimizations which has negligible impact on performance and return on value. – Thomas Matthews Nov 14 '13 at 19:54
  • My object is similar (but not exactly) to an iterator. That's why optimizing away unnecessary copies is relevant. – jbgs Nov 14 '13 at 19:59
  • In the example within the website that I linked, you should make note that passing a pointer by reference can also cause a memory leak. The first example is in fact going to leak memory. You need to think about what a reference to a pointer actually is. A pointer and a reference to a pointer are different kinds of things. Therefore, thinking about this as an optimization is incorrect. – shawn1874 Nov 14 '13 at 20:08
  • I know they are different things... but I would use them in the same way, i.e. I don't inted to modify the returned value. – jbgs Nov 14 '13 at 20:14
0

pointers and references are the same size so it should't matter.(It's also ,in my opinion, to small of something to worry about)

heres the link I got that information from What are the differences between a pointer variable and a reference variable in C++?

Community
  • 1
  • 1
XenoZergNid
  • 56
  • 1
  • 6