Pass pointer by value or const ref?


Barry

In short, is it better to pass pointers by value or const reference?

I wrote a simple function to explore:

void foo(int* v) { std::cout << *v << endl; }

and

void foo(int* const& v) { std::cout << *v << endl; }

I tried looking at the generated assembly, but I'm not particularly good at assemblies so I'm not sure I understand the difference. But there is a difference. In the first case, we get:

pushq   %rbp
pushq   %rbx
subq    $8, %rsp
movl    (%rdi), %esi
movl    std::cout, %edi
call    std::basic_ostream<char, std::char_traits<char> >::operator<<(int)

In this const&case, we can do it one more time:

pushq   %rbp
pushq   %rbx
subq    $8, %rsp
movq    (%rdi), %rax
movl    std::cout, %edi
movl    (%rax), %esi
call    std::basic_ostream<char, std::char_traits<char> >::operator<<(int)

What does this difference mean (three movinstead of two) and what does it mean? (Basically, I have a struct Foo<T>where the struct Tmight not be a raw pointer and wonder if my function bar(const T&)should be bar(typename boost::call_traits<T>::type).).

Mike Seymour

Pass pointers by value unless you need reference semantics.

Passing by reference almost certainly works by passing a pointer. You're passing the same amount of data as the pointer itself, but it introduces an extra level of indirection, so it's probably not as efficient as passing by value.

Having said that, the difference may be small. So for generic code, passing by const reference is easier, unless you really need micro-optimizations at the individual instruction level. If you do, you can do as you say boost::call_traits.

Related


cannot pass 'const pointer const' to const ref

Thomas B. Suppose you have a set of pointers (yes...): std::set<SomeType*> myTypeContainer; Then suppose you want to search this collection from a const method of SomeType: bool SomeType::IsContainered() const { return myTypeContainer.find(this) != myType

cannot pass 'const pointer const' to const ref

Thomas B. Suppose you have a set of pointers (yes...): std::set<SomeType*> myTypeContainer; Then suppose you want to search this collection from a const method of SomeType: bool SomeType::IsContainered() const { return myTypeContainer.find(this) != myType

cannot pass 'const pointer const' to const ref

Thomas B. Suppose you have a set of pointers (yes...): std::set<SomeType*> myTypeContainer; Then suppose you want to search this collection from a const method of SomeType: bool SomeType::IsContainered() const { return myTypeContainer.find(this) != myType

Pass pointer to const ref function

Dean Why does the following work? #include <iostream> using namespace std; class PolyLine { public: PolyLine() = default; PolyLine(PolyLine * ptr) { std::cout << "Ctor called" << std::endl; } }; void function(const PolyLine& pt) { } int main() {

Eigen::Ref pass pointer

install Similar to the question difference between pointers and references when passing trait objects as parameters Suppose we have foo1 and matrix mat2by2: void foo1(MatrixXd& container){ //...container matrix is modified here } and Matrix33d mat2by2; ma

Pass pointer by const reference

csguy Isn't passing pointers by const reference for optimization? predecessor. bool testHelper(const TreeNode*& p, const TreeNode*& q) { return false; } TreeNode* test(TreeNode* root, TreeNode* p, TreeNode* q) { recursiveHelper(p, q); } mistake: Line

pass a const pointer

catch_0x16 When using 3D/2D graphics libraries, I often find myself passing a pointer to the render window to each drawable class that draws itself onto the canvas. However, every time I do this, I worry that by passing a pointer to the render window, the obje

Pass literal as const ref parameter

innocent bystander Imagine the following simplified code: #include <iostream> void foo(const int& x) { do_something_with(x); } int main() { foo(42); return 0; } (1) Besides optimization, what happens when 42 is passed to foo? Does the compiler stick 42 somew

Pass literal as const ref parameter

innocent bystander Imagine the following simplified code: #include <iostream> void foo(const int& x) { do_something_with(x); } int main() { foo(42); return 0; } (1) Besides optimization, what happens when 42 is passed to foo? Does the compiler stick 42 somew

Pass literal as const ref parameter

innocent bystander Imagine the following simplified code: #include <iostream> void foo(const int& x) { do_something_with(x); } int main() { foo(42); return 0; } (1) Besides optimization, what happens when 42 is passed to foo? Does the compiler stick 42 somew

Pass literal as const ref parameter

innocent bystander Imagine the following simplified code: #include <iostream> void foo(const int& x) { do_something_with(x); } int main() { foo(42); return 0; } (1) Besides optimization, what happens when 42 is passed to foo? Does the compiler stick 42 somew

Pass literal as const ref parameter

innocent bystander Imagine the following simplified code: #include <iostream> void foo(const int& x) { do_something_with(x); } int main() { foo(42); return 0; } (1) Besides optimization, what happens when 42 is passed to foo? Does the compiler stick 42 somew

const pointer to address or pointer to value

username Are int const *aand const int *amean "a is an integer pointer to a constant value" and "a is a pointer to a constant address" respectively, or vice versa? This was asked me in an interview. After reading a lot about the same thing, I'm still very conf

Change the value of a const pointer

Izrin Recently, I stumbled across a comparison between Rust and C, they use the following code: bool f(int* a, const int* b) { *a = 2; int ret = *b; *a = 3; return ret != 0; } In Rust (same code, but with Rust syntax) it would generate the following a

Change the value of a const pointer

Izrin Recently, I stumbled across a comparison between Rust and C, they use the following code: bool f(int* a, const int* b) { *a = 2; int ret = *b; *a = 3; return ret != 0; } In Rust (same code, but with Rust syntax) it would generate the following a

Change the value of a const pointer

Izrin Recently, I stumbled across a comparison between Rust and C, they use the following code: bool f(int* a, const int* b) { *a = 2; int ret = *b; *a = 3; return ret != 0; } In Rust (same code, but with Rust syntax) it would generate the following a

Change the value of a const pointer

Izrin Recently, I stumbled across a comparison between Rust and C, they use the following code: bool f(int* a, const int* b) { *a = 2; int ret = *b; *a = 3; return ret != 0; } In Rust (same code, but with Rust syntax) it would generate the following a

Change the value of a const pointer

Izrin Recently, I stumbled across a comparison between Rust and C, they use the following code: bool f(int* a, const int* b) { *a = 2; int ret = *b; *a = 3; return ret != 0; } In Rust (same code, but with Rust syntax) it would generate the following a

Pass const pointer to const by reference in function

Jon I'm playing with arrays and passing pointers by reference functions. Take the following code as an example: #include<iostream> void test_fn(const int* const &i){ std::cout<<*i<<std::endl; } int main(){ int arr_1[5] {1, 3, 6, 4, 5}; int *int_p

Pass const pointer to const by reference in function

Jon I'm playing with arrays and passing pointers by reference functions. Take the following code as an example: #include<iostream> void test_fn(const int* const &i){ std::cout<<*i<<std::endl; } int main(){ int arr_1[5] {1, 3, 6, 4, 5}; int *int_p

Pass const pointer to const by reference in function

Jon I'm playing with arrays and passing pointers by reference functions. Take the following code as an example: #include<iostream> void test_fn(const int* const &i){ std::cout<<*i<<std::endl; } int main(){ int arr_1[5] {1, 3, 6, 4, 5}; int *int_p

Pass const pointer to const by reference in function

Jon I'm playing with arrays and passing pointers by reference functions. Take the following code as an example: #include<iostream> void test_fn(const int* const &i){ std::cout<<*i<<std::endl; } int main(){ int arr_1[5] {1, 3, 6, 4, 5}; int *int_p

C++ pass by const pointer or by reference to const pointer

Taoist group I'm learning c++ and recently I read a book that recommends that you use references to const when possible (if the underlying object doesn't change). I have a question, you should pass a reference to a const pointer instead of a const pointer if p