C / ObjC - parameter size. Using vs value pointer

at what point should I pass a pointer to data in my functions / methods, and not just pass a value?

Obviously, there are cases when I want the function to work on the given data, but what if I just pass the value for information / copy purposes?

For example, foo as the base type:

void setFoo(int foo); ... int foo = 1; setFoo(foo); 

Now foo is like a simple structure:

 typedef struct { int x; int y; } Foo; void setFoo(Foo foo); ... Foo foo = {1, 2}; setFoo(foo); // Apple code does this kind of thing with CGSize, CGPoint... 

But what if foo is a big structure ...

 typedef struct { int x; int y; int z; char str[256]; } Foo; void setFoo(Foo *foo); // Now taking a pointer instead. ... Foo foo = {1, 2, 3, etc ... }; setFoo(&foo); 

Q. At what point should I start using a pointer when providing data to a function?

thanks

+4
source share
2 answers

When on embedded systems, I find it good practice to pass pointers (or references) to anything that is not a primitive type. Thus, your structure can grow and add participants as needed, without affecting the amount of data that is copied. Incorrect copying is a good way to slow down your system, so avoid it whenever you can. To get into this habit will help in the long run, I think.

+1
source

If you use references and / or pointers to objects [const], when necessary, "when" becomes "all the time"

0
source

All Articles