据我所知,char在C++中只是一个整数类型,它将ASCII符号存储为0到127之间的数字.斯堪的纳维亚字母'æ','ø'和'å'不在ASCII表中的128个符号中.
所以当我尝试自然会char ch1 = 'ø'得到一个编译器错误,但是string str = "øæå"工作正常,即使字符串使用了chars 吗?
是否string以某种方式切换到Unicode?
我正在尝试使用std :: atomic和clang.但是,每当我尝试包含头文件atomic(#include <atomic>)时,我都会收到"未找到原子"的消息.请注意,我包括 - std=c++11 -stdlib=libc++在编译时.我错过了什么?
我使用的clang版本是3.2.
宽松排序标记 std::memory_order_relaxed 的原子操作不是同步操作,它们不排序内存。它们只保证原子性和修改顺序的一致性。例如,x 和 y 最初为零,
// Thread 1:
r1 = y.load(memory_order_relaxed); // A
x.store(r1, memory_order_relaxed); // B
// Thread 2:
r2 = x.load(memory_order_relaxed); // C
y.store(42, memory_order_relaxed); // D
Run Code Online (Sandbox Code Playgroud)
允许产生 r1 == r2 == 42 因为,虽然 A 在 B 之前被排序并且 C 在 D 之前被排序,但是没有什么可以阻止 D 在 y 的修改顺序中出现在 A 之前,并且 B 在修改中出现在 C 之前x 的顺序。
问题:是什么赋予上述代码属性A 在 B 之前排序并且C 在 D 之前排序?
编辑:
int A, B;
void foo()
{
A = …Run Code Online (Sandbox Code Playgroud) c++ multithreading memory-model stdatomic instruction-reordering
根据这个https://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html,已发布的商店MOV在x86(包括x86-64)上实现为(进入内存).
根据他的http://en.cppreference.com/w/cpp/atomic/memory_order
memory_order_release:
具有此内存顺序的存储操作将执行释放操作:在此存储之后,不能对当前线程中的内存访问进行重新排序.这确保了当前线程中的所有写入在获取或相同原子变量的其他线程中可见,并且带有依赖关系到原子变量的写入在消耗相同原子的其他线程中变得可见.
我知道当使用memory_order_release时,之前完成的所有内存存储应该在此之前完成.
int a;
a = 10;
std::atomic<int> b;
b.store(50, std::memory_order_release); // i can be sure that 'a' is already 10, so processor can't reorder the stores to 'a' and 'b'
Run Code Online (Sandbox Code Playgroud)
问题:MOV对于这种行为,裸指令(没有明确的内存栅栏)是否足够?如何MOV告诉处理器完成以前的所有商店?
我只是坚持以下问题:这是否会导致未定义的行为,为什么?
std::map<int, int> m;
m[10] += 1;
Run Code Online (Sandbox Code Playgroud)
它编译和运行完美,但它没有任何证据.它类似于一个普通的UB例子,i = ++i + i++;因为operator[]它确实有副作用,但另一方面假设任何评估顺序(从左到右和从右到左)使我进入地图的相同最终状态
PS可能相关:http://en.cppreference.com/w/cpp/language/eval_order
编辑
对不起,我应该写的
m[10] = m[10] + 1;
Run Code Online (Sandbox Code Playgroud) 考虑以下代码:
#include<iostream>
using namespace std;
class A
{
public:
A() {cout << "1";}
A(const A &obj) {cout << "2";}
};
class B: virtual A
{
public:
B() {cout << "3";}
B(const B & obj) {cout<< "4";}
};
class C: virtual A
{
public:
C() {cout << "5";}
C(const C & obj) {cout << "6";}
};
class D:B,C
{
public:
D() {cout << "7";}
D(const D & obj) {cout << "8";}
};
int main()
{
D d1;
cout << "\n";
D …Run Code Online (Sandbox Code Playgroud) c++ copy-constructor virtual-inheritance private-inheritance ctor-initializer
如果我理解正确,从C++ 17开始,此代码现在要求不进行任何复制:
Foo myfunc(void) {
return Foo();
}
auto foo = myfunc(); // no copy
Run Code Online (Sandbox Code Playgroud)
函数参数也是如此吗?是否会在以下代码中优化副本?
Foo myfunc(Foo foo) {
return foo;
}
auto foo = myfunc(Foo()); // will there be copies?
Run Code Online (Sandbox Code Playgroud) 以下程序的行为是否未定义?
#include <stdio.h>
int main(void)
{
int arr[2][3] = { { 1, 2, 3 },
{ 4, 5, 6 }
};
int *ptr1 = &arr[0][0]; // pointer to first elem of { 1, 2, 3 }
int *ptr3 = ptr1 + 2; // pointer to last elem of { 1, 2, 3 }
int *ptr3_plus_1 = ptr3 + 1; // pointer to one past last elem of { 1, 2, 3 }
int *ptr4 = &arr[1][0]; // pointer to first …Run Code Online (Sandbox Code Playgroud) 我正在研究这个网站:https://gcc.gnu.org/wiki/Atomic/GCCMM/AtomicSync,这对了解有关原子类的主题非常有帮助.
但这个关于放松模式的例子很难理解:
/*Thread 1:*/
y.store (20, memory_order_relaxed)
x.store (10, memory_order_relaxed)
/*Thread 2*/
if (x.load (memory_order_relaxed) == 10)
{
assert (y.load(memory_order_relaxed) == 20) /* assert A */
y.store (10, memory_order_relaxed)
}
/*Thread 3*/
if (y.load (memory_order_relaxed) == 10)
assert (x.load(memory_order_relaxed) == 10) /* assert B */
Run Code Online (Sandbox Code Playgroud)
对我来说断言B应该永远不会失败,因为x必须是10且y = 10,因为线程2已经以此为条件.
但网站上说这个例子中的断言实际上可能是失败的.
以下显然有效的代码使用UndefinedBehaviorSanitizer sanitiser产生错位的地址运行时错误.
#include <memory>
#include <functional>
struct A{
std::function<void()> data; // seems to occur only if data is a std::function
} ;
struct B{
char data; // occurs only if B contains a member variable
};
struct C:public virtual A,public B{
};
struct D:public virtual C{
};
void test(){
std::make_shared<D>();
}
int main(){
test();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在macbook上编译和执行
clang++ -fsanitize=undefined --std=c++11 ./test.cpp && ./a.out
产生输出
runtime error: constructor call on misaligned address 0x7fe584500028 for type 'C', which requires 16 byte …