For built-in types, pass by const pointer versus pass by value. effectiveness


IQ

When I try to find the answer, I almost only find C++ posts, not C.

For built-in types like int, char, is there a performance difference between pass-by-value and const pointer?

Is it still good programming practice to use the const keyword when passing by value?

int PassByValue(int value)
{
    return value / 2;
}

int ConstPointer(const int * value)
{
    return (*value) / 2;
}
Jonas Bertel

Passing by const pointer is never faster than passing by value as long as the value is less than or equal to the size of the pointer ( sizeof) . It's also more annoying and sometimes wrong (stack variables).

Related


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,

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

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() {

C++ view types: pass by const& or by value?

ACM This was raised recently in code review discussions, but with no satisfactory conclusion. The type in question is similar to the C++ string_view TS. They are simple non-owner wrappers around pointers and lengths, with some custom functions: #include <cstdd

C++ view types: pass by const& or by value?

DHW This was raised recently in code review discussions, but with no satisfactory conclusion. The type in question is similar to the C++ string_view TS. They are simple non-owner wrappers around pointers and lengths, with some custom functions: #include <cstdd

C++ view types: pass by const& or by value?

ACM This was raised recently in code review discussions, but with no satisfactory conclusion. The type in question is similar to the C++ string_view TS. They are simple non-owner wrappers around pointers and lengths, with some custom functions: #include <cstdd

C++ view types: pass by const& or by value?

DHW This was raised recently in code review discussions, but with no satisfactory conclusion. The type in question is similar to the C++ string_view TS. They are simple non-owner wrappers around pointers and lengths, with some custom functions: #include <cstdd

C++ view types: pass by const& or by value?

ACM This was raised recently in code review discussions, but with no satisfactory conclusion. The type in question is similar to the C++ string_view TS. They are simple non-owner wrappers around pointers and lengths, with some custom functions: #include <cstdd

Pass value by pointer error

MSD561 I don't understand why it crashes. I send a pointer to an array, allocate that array, and then modify the array. what is the problem? #include <stdio.h> #include <stdlib.h> #include <string.h> void f(unsigned char *data) { data = (unsigned char *)ma

pass the value of the pointer

kania 99 I'm trying to create a simple program to better understand how pointers work, but I'm running into a problem. I want to process 3 files main.c modul.c and modul.h. module.h typedef struct

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

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

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

Should I pass a string by value or pass a pointer to it?

Summer I have a function that looks like this int myclass::setVersion(std::string ver) { if (ver.size()>1) { version.swap(ver) return 0; } else return -1; } My question is simple, do I verpass it as is or as a pointer to a string? FYI, ve

Should I pass a string by value or pass a pointer to it?

Summer I have a function that looks like this int myclass::setVersion(std::string ver) { if (ver.size()>1) { version.swap(ver) return 0; } else return -1; } My question is simple, do I verpass it as is or as a pointer to a string? FYI, ve

Should I pass a string by value or pass a pointer to it?

Summer I have a function that looks like this int myclass::setVersion(std::string ver) { if (ver.size()>1) { version.swap(ver) return 0; } else return -1; } My question is simple, do I verpass it as is or as a pointer to a string? FYI, ve

Should I pass a string by value or pass a pointer to it?

Summer I have a function that looks like this int myclass::setVersion(std::string ver) { if (ver.size()>1) { version.swap(ver) return 0; } else return -1; } My question is simple, do I verpass it as is or as a pointer to a string? FYI, ve

pass by value to pass by pointer - perl code

Balasubramanian v I have a function call like below: Send(0x39,((rLoss>>8)&0xFF),(rLoss&0xFF) ); I want to convert this function to pass by pointer. I wrote two macros BYTE0(var) ((uint8_t *)&var) BYTE1(var) ((uint8_t)&var)+1) I want the result to be Send(

Pass pointer to const int in recursive call

Neeraj Bansal How to pass pointers const intin recursive calls . I'm calculating Fibonacci recursively using the following code format, but I'm getting an error: error: lvalue must be unary '&' operand** #include <iostream> void fun(const int *n) { fu

Pass pointer to const int in recursive call

Neeraj Bansal How to pass pointers const intin recursive calls . I'm calculating Fibonacci recursively using the following code format, but I'm getting an error: error: lvalue must be unary '&' operand** #include <iostream> void fun(const int *n) { fu