ownership

Transfer ownership of storage in Splint

孤者浪人 提交于 2019-12-13 00:42:52
问题 Using a simple linked list implementation in C, how do I tell Splint that I am transfer ownership of data ? typedef struct { void* data; /*@null@*/ void* next; } list; static /*@null@*/ list* new_list(/*@notnull@*/ void* data) { list* l; l = malloc(sizeof(list)); if (l == NULL) return NULL; l->next = NULL; l->data = data; return l; } I get this error message: Implicitly temp storage data assigned to implicitly only: list->data = data Temp storage (associated with a formal parameter) is

Why did compiler not error on this mutable borrow when there is an immutable borrowed string slice reference still in scope?

本小妞迷上赌 提交于 2019-12-12 10:54:36
问题 I am learning Rust from The Rust Programming Language book available from No Starch Press but ran into an issue where the compiler did not behave as explained in the book in chapter 4 on p. 77. Chapter 4 of the book is discussing ownership, and the example on p. 77 is similar to this without the final println!() in main() (I've also added comments and the function from p. 76 to create an MCVE). I also created a playground. fn main() { let mut s = String::from("Hello world!"); let word = first

How to use a file with a BufReader and still be able to write to it?

◇◆丶佛笑我妖孽 提交于 2019-12-12 07:16:30
问题 I want to open a file and read its contents as a BufReader using lines() . I also want to be able to seek to the end of the file and write some new lines. Using let mut file lets me write to the file, but once I've given the file to the BufReader I can no longer write to it, as the main function no longer owns file : fn main() { let filename = "tt.txt"; // open a tt.txt file in the local directory let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(filename) .unwrap();

About ownership of heap objects and C++ & pass-by-reference parameters

故事扮演 提交于 2019-12-12 04:47:48
问题 I would like my class to be: class NumberedString : public Object { public: String newName; short nameID; NumberedString(String &newName, short nameID) : newName(newName), nameID(nameID) {} }; HashMap uniqueStrs;//For later. An instantiation of this will be passed to a HashMap which takes over ownership of its the heap allocation: In HashMap.h (say): virtual result Add(const Object& key, const Object& value); Now this is where I get confused. I was allocating the String in the line that

New to Objective C: Need help understanding strong reference vs assign

隐身守侯 提交于 2019-12-11 11:09:14
问题 first post. Sorry if I screwed up the code rules. I'm trying to learn Objective C from the Big Nerd Ranch Guide. I'll post the example dealing with strong references. #import <Foundation/Foundation.h> @interface Asset : NSObject { NSSTRING *label; unsigned int resaleValue; } @property (strong) NSString *label; @property unsigned int resaleValue; @end So basically the NSString needs a strong reference whereas the int does not. I'm aware that NSString is an object, and I've read that if nothing

How can one force Rust to take ownership of memory allocated other than by its safe methods?

断了今生、忘了曾经 提交于 2019-12-11 00:49:00
问题 In his February 2018 note titled "Memory Safety in Rust: A Case Study with C", Will Crichton wrote: Rust provides the ability to take ownership of raw pointers, which we do using slice::from_raw_parts_mut and Box::from_raw which tells Rust to treat the memory pointer as a heap-allocated array. After transferring ownership, assuming the memory is valid and of the right size/type, Rust applies its usual memory safety and containment checks. The relevant part of his code to which the above

Passing a variable to a function (which alters said variable) repeatedly

倖福魔咒の 提交于 2019-12-11 00:16:20
问题 I'm having a play with Rust and, probably biting off far more than I can chew, am trying to write a module that will encapsulate my database traffic for the rest of the application. The code I'm struggling with is the following: pub fn create_statement(cypher: &str, params: &HashMap<&str, &str>) -> rusted_cypher::Statement { let mut statement = rusted_cypher::Statement::new(cypher); for (field, value) in params.iter() { statement.with_param(field.to_owned(), value.to_owned()); } return

How to take file ownership to the current user using win32 api

穿精又带淫゛_ 提交于 2019-12-10 19:05:00
问题 I want to take file ownership using win32 api, and I want my code to work on both xp and win7 anyway, here is what i came up with Function that changes the ownership of the file int ChangeFileOwner() { HANDLE token; char *filename = "c:\\file1.txt"; //(not owned by the current user) DWORD len; PSECURITY_DESCRIPTOR security = NULL; int retValue = 1; PSID sid; // Get the privileges you need if (OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &token)) { if(!SetPrivilege(

What does “borrowed data cannot be stored outside of its closure” mean?

﹥>﹥吖頭↗ 提交于 2019-12-10 17:39:04
问题 When compiling the following code: fn main() { let mut fields = Vec::new(); let pusher = &mut |a: &str| { fields.push(a); }; } The compiler gives me the following error: error: borrowed data cannot be stored outside of its closure --> src/main.rs:4:21 | 3 | let pusher = &mut |a: &str| { | ------ --------- ...because it cannot outlive this closure | | | borrowed data cannot be stored into here... 4 | fields.push(a); | ^ cannot be stored outside of its closure What does this error mean, and how

How can a nested loop with mutations on a HashMap be achieved in Rust?

半腔热情 提交于 2019-12-10 17:32:06
问题 I have the following (trimmed down) Rust code: use std::collections::HashMap; struct Node { weight: f64, outbound: f64, } struct Graph { edges: HashMap<u32, HashMap<u32, f64>>, nodes: HashMap<u32, Node>, } impl Graph { fn mutate(&mut self) { for (key, value) in self.nodes.iter() { if self.edges.contains_key(key) { for (target, weight) in self.edges[key].iter() { self.nodes.entry(*target).or_insert(Node::new()).weight; } } } } } However, I cannot get the code to compile due to Rust ownership