蓝桥ROS机器人之现代C++学习笔记 第 5 章 智能指针与内存管理

举报
zhangrelay 发表于 2022/04/10 23:36:44 2022/04/10
【摘要】   为何一定要学习现代C++,因为在ROS中应用非常广,不学基础程序都看不懂的。 举例说明: 其实描述并不准确的,ROS1/2中使用的指针包括: std::shared_ptrstd::unique_ptrstd::weak_ptr  例如: virtual void publish(std...

 

为何一定要学习现代C++,因为在ROS中应用非常广,不学基础程序都看不懂的。

举例说明:

其实描述并不准确的,ROS1/2中使用的指针包括:

  1. std::shared_ptr
  2. std::unique_ptr
  3. std::weak_ptr 

例如:


  
  1. virtual void
  2. publish(std::unique_ptr<MessageT, MessageDeleter> & msg)
  3. {
  4. this->do_inter_process_publish(msg.get());
  5. if (store_intra_process_message_) {
  6. // Take the pointer from the unique_msg, release it and pass as a void *
  7. // to the ipm. The ipm should then capture it again as a unique_ptr of
  8. // the correct type.
  9. // TODO(wjwwood):
  10. // investigate how to transfer the custom deleter (if there is one)
  11. // from the incoming unique_ptr through to the ipm's unique_ptr.
  12. // See: http://stackoverflow.com/questions/11002641/dynamic-casting-for-unique-ptr
  13. MessageT * msg_ptr = msg.get();
  14. msg.release();
  15. uint64_t message_seq =
  16. store_intra_process_message_(intra_process_publisher_id_, msg_ptr, typeid(MessageT));
  17. rcl_interfaces::msg::IntraProcessMessage ipm;
  18. ipm.publisher_id = intra_process_publisher_id_;
  19. ipm.message_sequence = message_seq;
  20. auto status = rcl_publish(&intra_process_publisher_handle_, &ipm);
  21. if (RCL_RET_PUBLISHER_INVALID == status) {
  22. rcl_reset_error(); // next call will reset error message if not context
  23. if (rcl_publisher_is_valid_except_context(&intra_process_publisher_handle_)) {
  24. rcl_context_t * context = rcl_publisher_get_context(&intra_process_publisher_handle_);
  25. if (nullptr != context && !rcl_context_is_valid(context)) {
  26. // publisher is invalid due to context being shutdown
  27. return;
  28. }
  29. }
  30. }
  31. if (RCL_RET_OK != status) {
  32. rclcpp::exceptions::throw_from_rcl_error(status, "failed to publish intra process message");
  33. }
  34. } else {
  35. // Always destroy the message, even if we don't consume it, for consistency.
  36. msg.reset();
  37. }
  38. }

以及:


  
  1. #include <memory>
  2. void unique_func_ref(std::unique_ptr<int> & msg) {}
  3. void unique_func_value(std::unique_ptr<int> msg) {}
  4. int main(int argc, char ** argv)
  5. {
  6. std::unique_ptr<int> foo(new int);
  7. std::unique_ptr<int> bar(new int);
  8. unique_func_ref(foo);
  9. // does not compile
  10. // unique_func_value(bar);
  11. unique_func_value(std::move(bar));
  12. return 0;
  13. }

 


不学习现代C++,ROS1/2机器人编程连门都入不了啊………………

我自己从写C++的第一个hello world,到今天已经24年过去了,但还是水平菜如小白。

只能继续努力学习了。


智能指针C++11就已经引入了,让程序员不再需要关心手动释放内存。

(克服了传统C++,使用 newdelete 去 『手工操作(不能忘会内存泄漏的?)』对资源进行释放。)



  
  1. #include <iostream>
  2. #include <memory>
  3. void foo(std::shared_ptr<int> i)
  4. {
  5. (*i)++;
  6. }
  7. int main()
  8. {
  9. // auto pointer = new int(10); // illegal, no direct assignment
  10. // std::shared_ptr construction
  11. auto pointer = std::make_shared<int>(10);
  12. auto pointer2 = pointer; // reference count + 1
  13. auto pointer3 = pointer; // reference count + 1
  14. foo(pointer);
  15. std::cout << *pointer << std::endl; // 11
  16. int *p = pointer.get(); // does not increase reference count
  17. std::cout << "pointer.use_count() = " << pointer.use_count() << std::endl;
  18. std::cout << "pointer2.use_count() = " << pointer2.use_count() << std::endl;
  19. std::cout << "pointer3.use_count() = " << pointer3.use_count() << std::endl;
  20. pointer2.reset();
  21. std::cout << "reset pointer2:" << std::endl;
  22. std::cout << "pointer.use_count() = " << pointer.use_count() << std::endl;
  23. std::cout << "pointer2.use_count() = " << pointer2.use_count() << std::endl;
  24. std::cout << "pointer3.use_count() = " << pointer3.use_count() << std::endl;
  25. pointer3.reset();
  26. std::cout << "reset pointer3:" << std::endl;
  27. std::cout << "pointer.use_count() = " << pointer.use_count() << std::endl;
  28. std::cout << "pointer2.use_count() = " << pointer2.use_count() << std::endl;
  29. std::cout << "pointer3.use_count() = " << pointer3.use_count() << std::endl;
  30. // std::cout << *pointer << std::endl; // reference count equals 0, illegal access
  31. // Before leaving the scope, the pointer is destructed and
  32. // the reference count is reduced to 0
  33. return 0;
  34. }

 



  
  1. #include <iostream>
  2. #include <memory>
  3. struct Foo {
  4. Foo() { std::cout << "Foo::Foo" << std::endl; }
  5. ~Foo() { std::cout << "Foo::~Foo" << std::endl; }
  6. void foo() { std::cout << "Foo::foo" << std::endl; }
  7. };
  8. void f(const Foo &) {
  9. std::cout << "f(const Foo&)" << std::endl;
  10. }
  11. int main() {
  12. std::unique_ptr<Foo> p1(std::make_unique<Foo>());
  13. // p1 is not empty, prints
  14. if (p1) p1->foo();
  15. {
  16. std::unique_ptr<Foo> p2(std::move(p1));
  17. // p2 is not empty, prints
  18. f(*p2);
  19. // p2 is not empty, prints
  20. if(p2) p2->foo();
  21. // p1 is empty, no prints
  22. if(p1) p1->foo();
  23. p1 = std::move(p2);
  24. // p2 is empty, no prints
  25. if(p2) p2->foo();
  26. std::cout << "p2 was destroyed" << std::endl;
  27. }
  28. // p1 is not empty, prints
  29. if (p1) p1->foo();
  30. // Foo instance will be destroyed when leaving the scope
  31. }

 

 



  
  1. #include <iostream>
  2. #include <memory>
  3. class A;
  4. class B;
  5. class A {
  6. public:
  7. std::shared_ptr<B> pointer;
  8. ~A() {
  9. std::cout << "A was destroyed" << std::endl;
  10. }
  11. };
  12. class B {
  13. public:
  14. std::shared_ptr<A> pointer;
  15. ~B() {
  16. std::cout << "B was destroyed" << std::endl;
  17. }
  18. };
  19. int main() {
  20. std::shared_ptr<A> a = std::make_shared<A>();
  21. std::shared_ptr<B> b = std::make_shared<B>();
  22. a->pointer = b;
  23. b->pointer = a;
  24. return 0;
  25. }


 

 

 

文章来源: zhangrelay.blog.csdn.net,作者:zhangrelay,版权归原作者所有,如需转载,请联系作者。

原文链接:zhangrelay.blog.csdn.net/article/details/124067964

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。