Libra Light
  • Home
  • Blog
  • Coffee
  • Rust
  • Author
  • Admin
Subscribe
Tagged

C/C++

A collection of 2 posts

Tips

為何在 C 語言中,char ** 無法安全轉換為 const char **?

最近在寫程式時,發現當我把一個 char ** 型態的變數,傳到一個 const char ** 型態的參數時,出現了以下的警告: passing 'char *[257]' to parameter of type 'const char **' discards qualifiers in nested pointer types將 char * 型態轉換為 const char * 是安全的,那為何會這樣呢? 那是因為 char ** 會含有 char * ,因此有可能可以透過其他 char * 去修改 const char * 中的值 以下是一個簡單的範例: const char *bin = "/bin/sh"; char *argv[

  • Leo Chen
Leo Chen 2021年7月6日 • 1 min read
Tips

在C++的const function存取mutex

在 C++ 中寫 Multi-thread program的時候,為了 Thread-Safety 經常會在裡面塞 std::mutex 。 class ThreadSafe { public: int get() const; void set(int value); private: std::mutex _lock; int data; }; 但這時問題就來了, mutex::lock()/ mutex::unlock() 並不是 const member function,因此在 get() 中 lock mutex 的時候,想必會編譯失敗。 難道我們就得因此犧牲 const member function 的使用嗎? 其實 C++ 有一個鮮為人知的關鍵字

  • Leo Chen
Leo Chen 2021年2月16日 • 1 min read
Libra Light © 2025
  • Blog
  • Coffee
  • Rust
Powered by Ghost