This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
I'm a Rust programmer who has need to write some C again. It has been a few years. I'm currently wrapping some C code and already have some questions. For example, when I see:
class MyClass {
public:
MyClass(MyThing& thing, MyThing2* thing2);
// *** Opaque ***
}
What assumptions can I make, if any, on the lifetime and ownership of thing
and thing2
? Does pointer vs ref make a diff in that assumption? Whose job is it to deallocate those (assuming they even are heap allocated)? Should I assume this class is taking ownership? Just borrowing for duration of constructor? Or copying?
If the docs say that would of course be best, but if they don't (and they don't in some of my cases), and I can't look through the source, what assumptions would the typical programmer make here? Even if there is no on right answer what is typical C convention?
UPDATE: Thinking on this more, I don't think there is a way for it to take ownership of a ref, as any new
allocated type would be a pointer, not a ref, right? So a ref must be to stack allocation or a field member and thus only choice here is for constructor to copy (or borrow for duration of constructor call)? (yes, my C is very rusty - no pun intended)
UPDATE 2: I may not have been clear. I'm not writing new C here (or at least not much), I'm wrapping existing C libraries. I'm trying to understand what assumptions I should be making when looking at undocumented code from others.
Post Details
- Posted
- 2 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/cpp/comment...