In C++, why is & needed for some parameters? [duplicate] - python

Is it better in C++ to pass by value or pass by reference-to-const?
I am wondering which is better practice. I realize that pass by reference-to-const should provide for better performance in the program because you are not making a copy of the variable.

It used to be generally recommended best practice1 to use pass by const ref for all types, except for builtin types (char, int, double, etc.), for iterators and for function objects (lambdas, classes deriving from std::*_function).
This was especially true before the existence of move semantics. The reason is simple: if you passed by value, a copy of the object had to be made and, except for very small objects, this is always more expensive than passing a reference.
With C++11, we have gained move semantics. In a nutshell, move semantics permit that, in some cases, an object can be passed “by value” without copying it. In particular, this is the case when the object that you are passing is an rvalue.
In itself, moving an object is still at least as expensive as passing by reference. However, in many cases a function will internally copy an object anyway — i.e. it will take ownership of the argument.2
In these situations we have the following (simplified) trade-off:
We can pass the object by reference, then copy internally.
We can pass the object by value.
“Pass by value” still causes the object to be copied, unless the object is an rvalue. In the case of an rvalue, the object can be moved instead, so that the second case is suddenly no longer “copy, then move” but “move, then (potentially) move again”.
For large objects that implement proper move constructors (such as vectors, strings …), the second case is then vastly more efficient than the first. Therefore, it is recommended to use pass by value if the function takes ownership of the argument, and if the object type supports efficient moving.
A historical note:
In fact, any modern compiler should be able to figure out when passing by value is expensive, and implicitly convert the call to use a const ref if possible.
In theory. In practice, compilers can’t always change this without breaking the function’s binary interface. In some special cases (when the function is inlined) the copy will actually be elided if the compiler can figure out that the original object won’t be changed through the actions in the function.
But in general the compiler can’t determine this, and the advent of move semantics in C++ has made this optimisation much less relevant.
1 E.g. in Scott Meyers, Effective C++.
2 This is especially often true for object constructors, which may take arguments and store them internally to be part of the constructed object’s state.

Edit: New article by Dave Abrahams on cpp-next: Want speed? Pass by value.
Pass by value for structs where the copying is cheap has the additional advantage that the compiler may assume that the objects don't alias (are not the same objects). Using pass-by-reference the compiler cannot assume that always. Simple example:
foo * f;
void bar(foo g) {
g.i = 10;
f->i = 2;
g.i += 5;
}
the compiler can optimize it into
g.i = 15;
f->i = 2;
since it knows that f and g doesn't share the same location. if g was a reference (foo &), the compiler couldn't have assumed that. since g.i could then be aliased by f->i and have to have a value of 7. so the compiler would have to re-fetch the new value of g.i from memory.
For more pratical rules, here is a good set of rules found in Move Constructors article (highly recommended reading).
If the function intends to change the argument as a side effect, take it by non-const reference.
If the function doesn't modify its argument and the argument is of primitive type, take it by value.
Otherwise take it by const reference, except in the following cases
If the function would then need to make a copy of the const reference anyway, take it by value.
"Primitive" above means basically small data types that are a few bytes long and aren't polymorphic (iterators, function objects, etc...) or expensive to copy. In that paper, there is one other rule. The idea is that sometimes one wants to make a copy (in case the argument can't be modified), and sometimes one doesn't want (in case one wants to use the argument itself in the function if the argument was a temporary anyway, for example). The paper explains in detail how that can be done. In C++1x that technique can be used natively with language support. Until then, i would go with the above rules.
Examples: To make a string uppercase and return the uppercase version, one should always pass by value: One has to take a copy of it anyway (one couldn't change the const reference directly) - so better make it as transparent as possible to the caller and make that copy early so that the caller can optimize as much as possible - as detailed in that paper:
my::string uppercase(my::string s) { /* change s and return it */ }
However, if you don't need to change the parameter anyway, take it by reference to const:
bool all_uppercase(my::string const& s) {
/* check to see whether any character is uppercase */
}
However, if you the purpose of the parameter is to write something into the argument, then pass it by non-const reference
bool try_parse(T text, my::string &out) {
/* try to parse, write result into out */
}

Depends on the type. You are adding the small overhead of having to make a reference and dereference. For types with a size equal or smaller than pointers that are using the default copy ctor, it would probably be faster to pass by value.

As it has been pointed out, it depends on the type. For built-in data types, it is best to pass by value. Even some very small structures, such as a pair of ints can perform better by passing by value.
Here is an example, assume you have an integer value and you want pass it to another routine. If that value has been optimized to be stored in a register, then if you want to pass it be reference, it first must be stored in memory and then a pointer to that memory placed on the stack to perform the call. If it was being passed by value, all that is required is the register pushed onto the stack. (The details are a bit more complicated than that given different calling systems and CPUs).
If you are doing template programming, you are usually forced to always pass by const ref since you don't know the types being passed in. Passing penalties for passing something bad by value are much worse than the penalties of passing a built-in type by const ref.

This is what i normally work by when designing the interface of a non-template function:
Pass by value if the function does not want to modify the parameter and the
value is cheap to copy (int, double, float, char, bool, etc... Notice that std::string, std::vector, and the rest of the containers in the standard library are NOT)
Pass by const pointer if the value is expensive to copy and the function does
not want to modify the value pointed to and NULL is a value that the function handles.
Pass by non-const pointer if the value is expensive to copy and the function
wants to modify the value pointed to and NULL is a value that the function handles.
Pass by const reference when the value is expensive to copy and the function does not want to modify the value referred to and NULL would not be a valid value if a pointer was used instead.
Pass by non-const reference when the value is expensive to copy and the function wants to modify the value referred to and NULL would not be a valid value if a pointer was used instead.

Sounds like you got your answer. Passing by value is expensive, but gives you a copy to work with if you need it.

As a rule passing by const reference is better.
But if you need to modify you function argument locally you should better use passing by value.
For some basic types the performance in general the same both for passing by value and by reference. Actually reference internally represented by pointer, that is why you can expect for instance that for pointer both passing are the same in terms of performance, or even passing by value can be faster because of needless dereference.

Pass by value for small types.
Pass by const references for big types (the definition of big can vary between machines) BUT, in C++11, pass by value if you are going to consume the data, since you can exploit move semantics. For example:
class Person {
public:
Person(std::string name) : name_(std::move(name)) {}
private:
std::string name_;
};
Now the calling code would do:
Person p(std::string("Albert"));
And only one object would be created and moved directly into member name_ in class Person. If you pass by const reference, a copy will have to be made for putting it into name_.

As a rule of thumb, value for non-class types and const reference for classes.
If a class is really small it's probably better to pass by value, but the difference is minimal. What you really want to avoid is passing some gigantic class by value and having it all duplicated - this will make a huge difference if you're passing, say, a std::vector with quite a few elements in it.

Pass by referece is better than pass by value. I was solving the longest common subsequence problem on Leetcode. It was showing TLE for pass by value but accepted the code for pass by reference. Took me 30 mins to figure this out.

Simple difference :- In function we have input and output parameter , so if your passing input and out parameter is same then use call by reference else if input and output parameter are different then better to use call by value .
example void amount(int account , int deposit , int total )
input parameter : account , deposit
output paramteter: total
input and out is different use call by vaule
void amount(int total , int deposit )
input total deposit
output total

Related

How to check the type of an operation in a statement?

I want to be able to check if the type of the return value is the same as the type of a method in ANTLR. (i.e int processOperation() should return an int like return (3-1*4))
My grammar is the following: https://github.com/RodrigoZea/Lab00DDC/blob/fda787998e5ed1cc5e5d94e6506ed6ca08dbd955/Decaf/Decaf.g4
I'm using the python implementation of ANTLR4, but I'm unsure as to how to check the type of an operation in a return statment, for example (1+3*4) should return an int. I'm using a Listener, so my logic is as follows:
First check the value if its a primitive (i.e. return "random", return 1)
Check if the value is an operation or a single variable.
For a single variable, searching it up in the symbol table would be enough, but for an operation I'm unsure on how to approach it. I've read about using a ParseTreeProperty<> but I don't think there's an implementation of that in the Python version of ANTLR4, that seemed to be the best approach from what I've read in the ANTLR4 definitive reference since it will save the nodes' (and the operation subtree) data type and I can easily check its type and compare it to my method type. I'm guessing I would need to check when I'm entering an operator rule, but I'm unsure on what to do with that data or if there's a way to implement a ParseTreeProperty in Python. Thanks.
So, basing myself on what Mike and kaby answered, I came up with a solution. It's incredibly simple but very functional.
The best way to replicate a ParseTreeProperty in Python is to create a dictionary, the ctx Object will be the key and the value is set manually, depending on what you want the value to be (this is where Mike's answer comes in handy). To update the dictionary values, you will do this on the *Exit() methods, just as Mike said as well.
For example, if you're exiting an int literal, char literal, or whatever (you can take my grammar as reference) you can add an entry to your dictionary as follows:
def exitType_literal(self, ctx: DecafParser.Type_literalContext):
self.nodeTypes[ctx] = 'type'
So for example, if I wanted to save a node as an int value, I would do something like...
def exitInt_literal(self, ctx: DecafParser.Int_literalContext):
self.parseTreePropertyDictionary[ctx] = 'int'
If you want to get the value of a variable however, you would have to search it up on your implementation of a Symbol Table. That was my approach on getting the type value.
So once every node is setup, you can simply setup how you want to process your operations. For example, if you want a "+" operator to be with ints, you would check the type of the first and second operator on your dictionary, check if both are ints, and if thats the case, then save it on your dictionary as an 'int' type node where you are processing your "+" operator.
Then, to get the type of the operation, you will simply access your dictionary on that node and it will return 'int' or whatever the type you set it up to be.
ParseTreeProperty is a convenience for "attaching" properties to nodes of your parse tree, and could be a useful way to keep track of the type of each node in your tree. However, as the comments mention, there are other data structures you can you to track the type of each node and map back to it. (Note: if you use this approach with a listener, as your question implies, you'd need to implement it in the *Exit() method, as you would want all the children to have been "listened to" and their types assigned, so that you can determine the type of the parent expression.)
Using a listener, you can also just have a stack of types. When you exit each expression, it pops the types of all of its children, evaluates the expression type for itself, and pushes that type on the stack. You, of course, have to take care to properly manage to pushing and popping (look out for exceptions), but it can be a reasonably clean implementation.
You could also implement an expression type validation visitor. With this approach, you write an expression visitor that returns it's type. With each overriden visit*() you can just call visit() on each child to get it's type, and then decide what you want to resulting type to be (and probably whether it's even a valid expression). Notice that ```visit``ing a node return a result with visitors, this is one of the key differences between visitors and listeners (the other being, that, with visitors, you ave to explicitly choose how to navigate your child nodes).
So far as "what to do with this data", at this point you're making design decisions about how you want your language to behave, what's valid, etc.
For example:
7 * "string"
Maybe you decide 7 is an Int type and "string" is a String type. In your listener/visitor for for multiplication expressions, it's up to you to decide if this is an error (and the resulting "type" is InvalidType, perhaps), or maybe, like Ruby, it's a cute way of getting "stringstringstringstringstringstringstring", in which case you'd return a type of String. For functions you have decisions to make about the return type of the function. Do you require them to be explicitly defined? Must the be defined before they're referenced (if not, you'll need to make a pass of you parse tree creating a symbol table of functions and return types to reference, before you can navigate your tree evaluating expression types). Maybe, you have a dynamic language where different input types (or even values) might result in different return types from your function.
Clearly, this gets pretty deep into language design choices, and languages have made many different decisions about how to handle them. ANTLR is just your parsing technology and (other than providing convenience classes like listeners and visitors) has nothing to say about how you make these decisions or how you implement them. And, there's not a way to codify them in your grammar as they ares semantic concerns that have no impact on parsing or the construction of your parse tree.

Python-like Coding in C for Pointers

I am transitioning from Python to C, so my question might appear naive. I am reading tutorial on Python-C bindings and it is mentioned that:
In C, all parameters are pass-by-value. If you want to allow a function to change a variable in the caller, then you need to pass a pointer to that variable.
Question: Why cant we simply re-assign the values inside the function and be free from pointers?
The following code uses pointers:
#include <stdio.h>
int i = 24;
int increment(int *j){
(*j)++;
return *j;
}
void main() {
increment(&i);
printf("i = %d", i);
}
Now this can be replaced with the following code that doesn't use pointers:
int i = 24;
int increment(int j){
j++;
return j;
}
void main() {
i=increment(i);
printf("i = %d", i);
}
You can only return one thing from a function. If you need to update multiple parameters, or you need to use the return value for something other than the updated variable (such as an error code), you need to pass a pointer to the variable.
Getting this out of the way first - pointers are fundamental to C programming. You cannot be “free” of pointers when writing C. You might as well try to never use if statements, arrays, or any of the arithmetic operators. You cannot use a substantial chunk of the standard library without using pointers.
“Pass by value” means, among other things, that the formal parameter j in increment and the actual parameter i in main are separate objects in memory, and changing one has absolutely no effect on the other. The value of i is copied to j when the function is called, but any changes to i are not reflected in j and vice-versa.
We work around this in C by using pointers. Instead of passing the value of i to increment, we pass its address (by value), and then dereference that address with the unary * operator.
This is one of the cases where we have to use pointers. The other case is when we track dynamically-allocated memory. Pointers are also useful (if not strictly required) for building containers (lists, trees, queues, stacks, etc.).
Passing a value as a parameter and returning its updated value works, but only for a single parameter. Passing multiple parameters and returning their updated values in a struct type can work, but is not good style if you’re doing it just to avoid using pointers. It’s also not possible if the function must update parameters and return some kind of status (such as the scanf library function, for example).
Similarly, using file-scope variables does not scale and creates maintenance headaches. There are times when it’s not the wrong answer, but in general it’s not a good idea.
So, imagine you need to pass large arrays or other data structures that need modification. If you apply the way you use to increment an integer, then you create a copy of that large array for each call to that function. Obviously, it is not memory-friendly to create a copy, instead, we pass pointers to functions and do the updates on a single array or whatever it is.
Plus, as the other answer mentioned, if you need to update many parameters then it is impossible to return in the way you declared.

Methods that do return a value ones that make change to the orignal data type

After I learned about different data types I learned that once an object from a given type is created it has innate methods that can do 'things'.
Playing around, I noticed that, while some methods return a value, others make change to the original data stored.
Is there any specific term for these two types of methods and is there any intuition or logic as to which methods return a value and which make changes?
For example:
abc= "something"
defg= [12,34,11,45,132,1]
abc.capitalise() #this returns a value
defg.sort() #this changes the orignal list
Is there any specific term for these two types of methods
A method that changes an object's state (ie list.sort()) is usually called a "mutator" (it "mutates" the object). There's no general name for methods that return values - they could be "getters" (methods that take no arguments and return part of the object's state), alternative constructors (methods that are called on the class itself and provide an alternative way to construct an instance of the class), or just methods that take some arguments, do some computations based on both the arguments and the object's state and return a result, or actually just do anything (do some computation AND change the object's state AND return a value).
is there any intuition or logic as to which methods return a value and which make changes?
Some Python objects are immutable (strings, numerics, tuples etc) so when you're working on one of those types you know you won't have any mutator. Except for this special case, nope, you will have to check the doc. The only naming convention here is that methods whose name starts with "set_" and take one argument will change the object's state based on their argument (and most often return nothing) and that methods whose name starts with "get_" and take no arguments will return informations on the object's state and change nothing (you'll often see the formers named "setters" and the laters named "getters"), but like any convention it's only followed by those who follow it, IOW don't assume that because a method name starts with "get_" or "set_" it will indeed behave as expected.
Strings are immutable, so all libraries that do string manipulation will return a new string.
For the other types, you will have to refer to the library documentation.

Why can Python functions return locally declared arrays but C can't? [duplicate]

This question already has an answer here:
CPython memory allocation
(1 answer)
Closed 6 years ago.
I was reading this question: Cannot return int array because I ran into the same problem.
It seems that data structures (because C can obviously return a locally declared variable) declared locally within a function cannot be returned, in this case an array.
However Python doesn't suffer from the same problem; as far as I can remember, it's possible to declare an array within a function and to return that array without having to pass it as an argument.
What is the difference "under the hood"? Is Python using pointers implicitly (using malloc within the function)?
For the record, Python's built-in mutable sequence type is called a list, not an array, but it behaves similarly (it's just dynamically resizable, like C++'s std::vector).
In any event, you're correct that all Python objects are implicitly dynamically allocated; only the references (roughly, pointers) to them are on the "stack" (that said, the Python interpreter stack and the C level stack are not the same thing to start with). Comparable C code would dynamically allocate the array and return a pointer to it (with the caller freeing it when done; different Python interpreters handle this differently, but the list would be garbage collected when no longer referenced in one way or another).
Python has no real concept of "stack arrays" (it always returns a single object, though that object could be a tuple to simulate multiple return values), so returns are always ultimately a single "pointer" value (the reference to the returned object).
It seems that data structures (because C can obviously return a locally declared variable) declared locally within a function cannot be returned, in this case an array.
You already have a good Python answer; I wanted to look at the C side a little more closely.
Yes, a C function returns a value. That value may be primitive C type, or a struct or union type. Or, it may be a pointer type.
The C language syntax makes arrays and pointers seem very similar, which makes arrays special. Because the name of the array is the same as the address of the first element, it can't be something else. In particular, an array name does not refer to the whole array (except in the case of the sizeof operator). Because any other use of an array name refers to the address of the first element, attempting to return an array results in returning only that address.
Because it's a C function, that address is returned by value: namely, a value of a pointer type. So, when we say,
char *s = strdup("hello");
s is a pointer type whose value is not "hello", but the value of address of the first element of the array that strdup allocates.
Python doesn't suffer from the same problem
When Y is a property of X, Y is a problem only if that property is, in the eyes of the beholder, undesirable. You can be sure the way C treats arrays is not accidental, and is often convenient.

Trying to cast one object type into another in Python

I have this bit of code:
const ON_Curve* curve = ...;
const ON_NurbsCurve* nurb = ON_NurbsCurve::Cast( curve );
if( nurb )
{
ON_Ellipse ellipse;
double tolerance = model.m_settings.m_ModelUnitsAndTolerances.m_absolute_tolerance;
bool rc = nurb->IsEllipse( 0, &ellipse, tolerance );
It casts a ON_NurbsCurve object to ON_Curve object. I am not quite sure if that's even possible in Python. I know i can take a string and cast it into an integer like: int("1"). I am not sure what is the right way to do so with other object types that are not built in.
thank you
You can't exactly cast objects in Python, nor do you generally need to because Python doesn't have strong type-checking.
Technically, casting is interpreting an existing object's data as if it were another type, essentially treating the object as a liquid metal that is being cast into a new shape (the origin of the term). In Python, what you can do is try to convert an object to another format. Usually an object that can take another object as input will accept it in its constructor (e.g. int() can take strings as well as numbers, and will call __int__() on other types if such a method exists, to let other types define how they are converted). Some types even have alternate constructors if their main constructor can't accept a given kind of object (for example, an XML parser might accept a filename in its main constructor, and have from_string() and from_file() class methods that accept strings and file-like objects, respectively).
In the example you give, of converting one type of Curve object into another, in Python you probably wouldn't even need to do any conversion. The NurbsCurve object probably supports the methods and attributes of Curve that any function that accepts a Curve would expect to see, even if it isn't a strict subclass. And if it is a strict subclass, then there's definitely no problem and no need to convert!
Python doesn't check argument types unless there is explicit code to do so, and most functions don't have such code. Instead, they just assume the caller is not a doofus and has passed in an object they can use. This is commonly called "duck typing."
If a given function doesn't accept the object you want to pass in, you could write a conversion function, a multiple-inheritance subclass, or a wrapper class to make what you have behave enough like the type that's needed to get the function to work. But this is usually not needed, because people who design APIs are not idiots and will be generous in what they accept when possible.

Categories