Last active
September 5, 2018 08:42
-
-
Save akehoyayoi/90cfa245f62f8e5d89ba8d71b369e26d to your computer and use it in GitHub Desktop.
[Rust]所有権、所有権、所有けんけんけけんけん ref: https://qiita.com/akehoyayoi/items/abe58c8d7be198587a34
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string> | |
#include <iostream> | |
#include <vector> | |
int main(){ | |
auto s = new std::vector<std::string>();s->push_back("udon");s->push_back("ramen");s->push_back("soba"); | |
auto t = s; s = NULL; | |
auto u = s; s = NULL; | |
std::cout << (*s)[0] << std::endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string> | |
#include <iostream> | |
#include <vector> | |
int main(){ | |
auto s = new std::vector<std::string>();s->push_back("udon");s->push_back("ramen");s->push_back("soba"); | |
auto t = s; s = NULL; | |
auto u = s; s = NULL; | |
std::cout << (*s)[0] << std::endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let s = vec!["udon".to_string(),"ramen".to_string(),"soba".to_string()]; | |
let t = s; // sはここで所有権を失う | |
let u = t; | |
println!("s[0] is: {}", s[0]); // 所有権のないsにアクセス! | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ g++ -std=c++11 main.cpp | |
$ ./a.out | |
Segmentation fault: 11 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment