ownership-semantics

Why this function return an (owned) value?

旧时模样 提交于 2019-12-01 22:50:06
问题 the code from: Genie howto repeat a string N times as an string arrayGenie howto repeat a string N times as an string array def repeatwithsep (e: string, n: int, separator: string): string var elen = e.length; var slen = separator.length; var a = new StringBuilder.sized ((elen * n) + (slen * (n - 1)) + 1); for var i = 0 to (n - 1) if i != 0 a.append_len (separator, slen) a.append_len (e, elen) return (owned) a.str var a is a local variable, when a goes out of scope, it will be destroyed. why

Why this function return an (owned) value?

≯℡__Kan透↙ 提交于 2019-12-01 20:53:19
the code from: Genie howto repeat a string N times as an string array Genie howto repeat a string N times as an string array def repeatwithsep (e: string, n: int, separator: string): string var elen = e.length; var slen = separator.length; var a = new StringBuilder.sized ((elen * n) + (slen * (n - 1)) + 1); for var i = 0 to (n - 1) if i != 0 a.append_len (separator, slen) a.append_len (e, elen) return (owned) a.str var a is a local variable, when a goes out of scope, it will be destroyed. why this function return (owned) a.str what is the difference between return a.str return (owned) a.str

When is the storage reclaimed for a resource that is no longer owned?

99封情书 提交于 2019-12-01 18:14:39
There is a vector resource that is allocated in line 2 of the program below. When the program ends, the vector resource is not owned. If a resource is not owned at all, when does it get reclaimed? Is there an explanation using the terminology of Rust ownership semantics and lifetimes that could convince a programmer that this resource is indeed reclaimed? fn main() { let mut v = vec![1,2]; v = vec![3, 4]; } when does [an unowned resource] get reclaimed? In Rust terms, an item is dropped when it goes out of scope, which often (but not always) corresponds to the end of a block. When it is

Bad practice to return unique_ptr for raw pointer like ownership semantics?

心不动则不痛 提交于 2019-11-28 06:09:53
I've written a static factory method that returns a new Foobar object populated from another data object. I've recently been obsessed with ownership semantics and am wondering if I'm conveying the right message by having this factory method return a unique_ptr . class Foobar { public: static unique_ptr<Foobar> factory(DataObject data); } My intent is to tell client code that they own the pointer. Without a smart pointer, I would simply return Foobar* . I would like, however, to enforce that this memory be deleted to avoid potential bugs, so unique_ptr seemed like an appropriate solution. If

Is there a proper 'ownership-in-a-package' for 'handles' available?

不问归期 提交于 2019-11-27 16:14:26
Handles have proper semantics other than pointers. So for me an example like this (extracted from the Rule of Zero ): class module { public: explicit module(std::wstring const& name) : handle { ::LoadLibrary(name.c_str()), &::FreeLibrary } {} // other module related functions go here private: using module_handle = std::unique_ptr<void, decltype(&::FreeLibrary)>; module_handle handle; }; using unique_ptr as an 'ownership-in-a-package' for handles is a bad example. First, it makes use of internal knowledge that the handle is a pointer type, and use this to make a unique_ptr to the basic type the

Is there a proper 'ownership-in-a-package' for 'handles' available?

孤人 提交于 2019-11-26 22:26:57
问题 Handles have proper semantics other than pointers. So for me an example like this (extracted from the Rule of Zero): class module { public: explicit module(std::wstring const& name) : handle { ::LoadLibrary(name.c_str()), &::FreeLibrary } {} // other module related functions go here private: using module_handle = std::unique_ptr<void, decltype(&::FreeLibrary)>; module_handle handle; }; using unique_ptr as an 'ownership-in-a-package' for handles is a bad example. First, it makes use of

Smart Pointers: Or who owns you baby? [closed]

人盡茶涼 提交于 2019-11-26 11:27:34
C++ is all about memory ownership Aka " Ownership Semantics " It is the responsibility of the owner of a chunk of dynamically allocated memory to release that memory. So the question really becomes who owns the memory. In C++ ownership is documented by the type a RAW pointer is wrapped inside thus in a good (IMO) C++ program it is very rare [RARE not NEVER] to see RAW pointers passed around (as RAW pointers have no inferred ownership thus we can not tell who owns the memory and thus without careful reading of the documentation you can't tell who is responsible for ownership). Conversely it is

Smart pointers: who owns the object? [closed]

断了今生、忘了曾经 提交于 2019-11-26 02:26:22
问题 C++ is all about memory ownership - aka ownership semantics . It is the responsibility of the owner of a chunk of dynamically allocated memory to release that memory. So the question really becomes who owns the memory. In C++ ownership is documented by the type a raw pointer is wrapped inside thus in a good (IMO) C++ program it is very rare ( rare , not never ) to see raw pointers passed around (as raw pointers have no inferred ownership thus we can not tell who owns the memory and thus