You are not logged in. Your edit will be placed in a queue until it is peer reviewed.
We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.
Required fields*
- 59The drawback of returning by pointer: now you've got to track ownership of that object and possible free it. Also, pointer indirection may be more costly than a quick copy. There are a lot of variables here, so using pointers is not universally better.amon– amon2017-10-19 14:15:22 +00:00Commented Oct 19, 2017 at 14:15
- 19Also, pointers these days are 64 bits on most desktop and server platforms. I've seen more than a few structs in my career that would fit in 64 bits. So, you can't always say that copying a pointer costs less than copying a struct.Solomon Slow– Solomon Slow2017-10-19 15:01:46 +00:00Commented Oct 19, 2017 at 15:01
- 40This is mostly a good answer, but I disagree about the part sometimes, very rarely, this is what you want, but most of the time it's not - quite the opposite. Returning a pointer allows several kinds of unwanted side effects, and several kinds of nasty ways to get the ownership of a pointer wrong. In cases where CPU time is not that important, I prefer the copy variant, if that is an option, it is much less error prone.Doc Brown– Doc Brown2017-10-19 17:28:44 +00:00Commented Oct 19, 2017 at 17:28
- 6It should be noted that this really only applies for external APIs. For internal functions every even marginally competent compiler of the last decades will rewrite a function that returns a large struct to take a pointer as an additional argument and construct the object directly in there. The arguments of immutable vs mutable have been done often enough, but I think we can all agree that the claim that immutable data structures are almost never what you want is not true.Voo– Voo2017-10-19 19:19:54 +00:00Commented Oct 19, 2017 at 19:19
- 7You could also mention compilation fire walls as a pro for pointers. In large programs with widely shared headers incomplete types with functions prevent the necessity to re-compile every time an implementation detail changes. The better compilation behavior is actually a side effect of the encapsulation which is achieved when interface and implementation are separated. Returning (and passing, assigning) by value need the implementation information.Peter - Reinstate Monica– Peter - Reinstate Monica2017-10-19 20:29:37 +00:00Commented Oct 19, 2017 at 20:29
| Show 18 more comments
How to Edit
- Correct minor typos or mistakes
- Clarify meaning without changing it
- Add related resources or links
- Always respect the author’s intent
- Don’t use edits to reply to the author
How to Format
- create code fences with backticks ` or tildes ~ ```
like so
``` - add language identifier to highlight code ```python
def function(foo):
print(foo)
``` - put returns between paragraphs
- for linebreak add 2 spaces at end
- _italic_ or **bold**
- indent code by 4 spaces
- backtick escapes
`like _so_` - quote by placing > at start of line
- to make links (use https whenever possible) <https://example.com>[example](https://example.com)<a href="https://example.com">example</a>
How to Tag
A tag is a keyword or label that categorizes your question with other, similar questions. Choose one or more (up to 5) tags that will help answerers to find and interpret your question.
- complete the sentence: my question is about...
- use tags that describe things or concepts that are essential, not incidental to your question
- favor using existing popular tags
- read the descriptions that appear below the tag
If your question is primarily about a topic for which you can't find a tag:
- combine multiple words into single-words with hyphens (e.g. design-patterns), up to a maximum of 35 characters
- creating new tags is a privilege; if you can't yet create a tag you need, then post this question without it, then ask the community to create it for you
lang-c