C++删除未使用的类属性会导致 std::logic_error
C++ Removing Unused Class Attribute Causes std::logic_error
我有一个类,它有一些属性,如下所示,我的问题是当我在std::atomic<char*> atomic_input
之前删除或放置string s
属性时,程序会异常终止:
"标准::logic_error">
what((: basic_string::_M_construct 空无效
已中止(核心已转储(
#include <string>
#include <atomic>
// In ui.cpp
class UI
{
private:
std::atomic<char*> atomic_input;
std::string s; /* this can be renamed, but removing or placing it
before the above field crashes the program */
};
// In main.cpp
#include "ui.cpp"
int main()
{
srand (time(NULL));
initscr(); /* start the curses mode */
UI* ui = new UI();
return 0;
}
字符串属性不会以任何方式在程序中访问,可以重命名它。我有一个atomic
字段的原因是该值在多个线程之间共享。
我尝试将string
字段放在类属性中的不同行中,仅当声明在atomic_input
之前时,程序才会崩溃。
可能导致问题的原因是什么?这与如何定义C++中的类有关吗?
看起来我已经找到了解决方案。
std::atomic<char*> atomic_input
未初始化(如下所示(导致问题。我仍然不知道string
变量是如何干扰它的。
我的猜测是编译器以某种方式将string
解释为atomic_input
的构造函数。仅当在运行时而不是在编译中访问atomic_input
时,才会发生此错误。
#include <string>
#include <atomic>
// In ui.cpp
class UI
{
private:
std::atomic<char*> atomic_input{(char*)""};
// std::string s; /* Initializing the atomic char like above solved the problem */
};
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从持续时间构造std::chrono::system_clock::time_point
- std::具有相同基类的类的变体
- std::向量与传递值的动态数组
- 使用std::vector的OpenCL矩阵乘法
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- std::condition_variable::wait()如何评估给定的谓词
- 如何获取std::result_of函数的返回类型
- std::原子加载和存储都需要吗
- 将对象移动到std::shared_ptr
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 使用一个考虑到std::map中键值的滚动或换行的键
- 如何从 std::atomic 中提取指针 T<T>?
- 为什么 std::unique 不调用 std::sort?
- 使用std::函数映射对象方法
- 可组合的lambda/std::函数与std::可选