• Method of returning pointer values
    21 replies, posted
So, I direct this toward the c/c++ developers of Facepunch: > How do you return your pointer values and which do you think is better? [b]Example A:[/b][code] void GetSync( CSync** pSync ) { if( pSync ) *pSync = m_pSync; } [/code] [b]Example B:[/b][code] CSync* GetSync() { return m_pSync; } [/code]
Neither is better. Use the former whenever it's not the main return value of the function, or when it's optional (I've never really seen a justified use of this in C++, though).
[QUOTE=jA_cOp;20398754]Neither is better. Use the former whenever it's not the main return value of the function, or when it's optional (I've never really seen a justified use of this in C++, though).[/QUOTE] I noticed DirectX does it a lot, I figured neither were actually better, but I like your argument.
[QUOTE=TehBigA;20398768]I noticed DirectX does it a lot, I figured neither were actually better, but I like your argument.[/QUOTE] DirectX is really old and doesn't use many features of C++, the API design is absolutely horrid. It's like the Windows API had a baby with C++. I hear the recent versions have improved it, though? edit: I just looked at DX10, still using COM... edit2: And apparently DX11 is a superset of DX10.
I didn't read much into it, hasn't been hard to work with though so I'm satisfied for now.
I use the former. I use the differences between A and B to help me quickly establish which function is changing a pointer and which is returning a copy value to use.
I only do the first method if I'm already using the return value for something else.
[QUOTE=PvtCupcakes;20404340]I only do the first method if I'm already using the return value for something else.[/QUOTE] In C++ you should be using a reference instead, I literally can't think of a justified use of a pointer for out parameters in C++. He said C/C++ though, so I'm not sure which language he's talking about.
Unless you're VALVe, then you've never heard of the standard library and references.
VALVes code is mostly just C with classes. Though in some places they actually use references and templates 'n stuff. They were probably lazy and copied much of their old code.
The fact that they started with VC6 probably didn't help.
[QUOTE=jA_cOp;20404629]In C++ you should be using a reference instead, I literally can't think of a justified use of a pointer for out parameters in C++. He said C/C++ though, so I'm not sure which language he's talking about.[/QUOTE] What is the difference between using pointers and references as parameters? I mainly do C, so I don't know.
A reference must be initialized upon creation, thus can also not be NULL. I think that are all the differences.
[QUOTE=PvtCupcakes;20407595]What is the difference between using pointers and references as parameters?[/QUOTE] Copying from an answer I recently posted on StackOverflow: [quote]A pointer is a distinct value, independent of the data it points to. (The number 0x12345678 is meaningful as a pointer value even if there's no meaningful data at address 0x12345678 in memory.) Because it's a distinct value, it can be manipulated on its own: you can increment or decrement it, compare it against other pointers, and print its value to the screen, regardless of whether it actually "points to anything." You can't do any of those things with a reference because it's not a distinct value. It's an alias, an alternate name (possibly in a different scope) for some existing value. This makes references easier to use (since they act just like the object they refer to, no dereferencing needed), and also safer (since, if used properly, they always refer to an object; there's no such thing as a dangling or null reference). It may be true that references typically get translated into pointers in the compiled machine code, but you should think of that as a private implementation detail of the compiler, not a guarantee. References are their own concept with their own use-cases, not just a different way of working with pointers. When you need a distinct pointer-like value that you can manipulate and examine independently of the data it points to, use a pointer (or, preferably, a smart-pointer class). When you just need a way to take a value that exists in one scope and make it available in another scope (e.g. passing an object into a function without copying it), use a reference.[/quote] If you have a pointer variable and you want to make it accessible within a function so that the function can modify it, passing a reference to the pointer is appropriate. Passing a pointer to the pointer is less appropriate since the function doesn't need to do any pointer-ish things with it; all it would do is dereference it.
[QUOTE=jA_cOp;20404629]In C++ you should be using a reference instead, I literally can't think of a justified use of a pointer for out parameters in C++. He said C/C++ though, so I'm not sure which language he's talking about.[/QUOTE] Null values, maybe?
[QUOTE=noctune9;20411053]Null values, maybe?[/QUOTE] bool isValid; It's useful for optional stuff inside some class. e.g. [cpp]class someClass{ hugeClass *instance; //... };[/cpp] If hugeClass is stored as a pointer and does not have to be initialized, then you can save the memory. I can imagine that this is very rarely the case though.
[QUOTE=noctune9;20411053]Null values, maybe?[/QUOTE] Allowing a parameter to be null in C++ is indicative of bad design. You should be using default parameters and overloading instead, and when that doesn't fix it, the problem is probably deeper.
[QUOTE=jA_cOp;20412476]Allowing a parameter to be null in C++ is indicative of bad design. You should be using default parameters and overloading instead, and when that doesn't fix it, the problem is probably deeper.[/QUOTE] What? [cpp] void sendStuff(SystemAddress *who = NULL) { sendData(someStuff, who ? who : mainConnection); } [/cpp] Perfectly justified.
[QUOTE=nullsquared;20412713]What? [cpp] void sendStuff(SystemAddress *who = NULL) { sendData(someStuff, who ? who : mainConnection); } [/cpp] Perfectly justified.[/QUOTE] [cpp] void sendStuff(SystemAddress& who) { sendData(someStuff, who); } void sendStuff() { sendStuff(mainConnection); } [/cpp] edit: And this is C++, use 0 not NULL. edit2: With GCC (didn't test any other compiler), you can actually do: [cpp] void sendStuff(SystemAddress& = mainConnection) { } [/cpp] When mainConnection is in static memory.
I just think NULL makes it more obvious in the code that I'm assigning a null pointer. Either way, std::nullptr_t will have to be used eventually to fix some issues so meanwhile it doesn't really matter what you get used to.
NULL is defined in the <cstddef> header, so if you use it without including that header, you're relying on implementation-specific behavior (one of the other headers probably pulls it in as a side effect). 0 works without any headers. (The same is true in C, btw -- you should #include <stddef.h>.)
[QUOTE=Wyzard;20414086]NULL is defined in the <cstddef> header, so if you use it without including that header, you're relying on implementation-specific behavior (one of the other headers probably pulls it in as a side effect). 0 works without any headers. (The same is true in C, btw -- you should #include <stddef.h>.)[/QUOTE] Yes, I do include it. I'm well aware of header-pulling issues.
Sorry, you need to Log In to post a reply to this thread.