Coming soon - Get a detailed view of why an account is flagged as spam!
view details

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.

5
Class construct arg lifetime/ownership assumptions
Post Body

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.

Author
Account Strength
90%
Account Age
13 years
Verified Email
Yes
Verified Flair
No
Total Karma
2,295
Link Karma
366
Comment Karma
1,909
Profile updated: 3 days ago
Posts updated: 1 year ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
2 years ago