在使用指针算法时offsetof,是否采用了良好定义的行为来获取结构的地址,向其添加成员的偏移量,然后取消引用该地址以获取基础成员?
请考虑以下示例:
#include <stddef.h>
#include <stdio.h>
typedef struct {
const char* a;
const char* b;
} A;
int main() {
A test[3] = {
{.a = "Hello", .b = "there."},
{.a = "How are", .b = "you?"},
{.a = "I\'m", .b = "fine."}};
for (size_t i = 0; i < 3; ++i) {
char* ptr = (char*) &test[i];
ptr += offsetof(A, b);
printf("%s\n", *(char**)ptr);
}
}
Run Code Online (Sandbox Code Playgroud)
这应该打印"那里","你呢?" 并且"很好".连续三行,它目前与clang和gcc一起使用,因为你可以在wandbox上验证自己.但是,我不确定这些指针强制转换和算术是否违反某些规则会导致行为变为未定义.
鉴于此类是enable_shared_from_this:
class connection : public std::enable_shared_from_this<connection>
{
//...
};
Run Code Online (Sandbox Code Playgroud)
假设我创建的两个实例std::shared_ptr来自同一 connection*如下:
std::shared_ptr<connection> rc(new connection);
std::shared_ptr<connection> fc(rc.get(), [](connection const * c) {
std::cout << "fake delete" << std::endl;
});
Run Code Online (Sandbox Code Playgroud)
到目前为止它的好处,因为资源{ connection*}由一个单独 拥有shared_ptr- rc确切地说,并且fc只有一个假的删除器.
在那之后,我这样做:
auto sc = fc->shared_from_this();
//OR auto sc = rc->shared_from_this(); //does not make any difference!
Run Code Online (Sandbox Code Playgroud)
现在哪个shared_ptr- rc或fc- 将sc 与其分享其引用计数?换一种说法,
std::cout << rc->use_count() << std::endl;
std::cout << fc->use_count() …Run Code Online (Sandbox Code Playgroud) c++ reference-counting shared-ptr weak-ptr enable-shared-from-this
当不存在这样的重载时,是否允许重载运算符,例如operator+标准库类型和内置类型的组合?
例如,在默认命名空间或用户定义的命名空间中实现以下运算符是否合法:
std::string operator+(const std::string& s, int right) { ... }
Run Code Online (Sandbox Code Playgroud)
我知道在命名空间中实现事物有各种限制std::,但我不清楚是否有任何规则违反上述规定(当然这是否是一个好主意是完全不同的事情!)。
我知道在线程2中的发布存储操作和线程1中的获取加载操作之间将发生同步关系,即使该加载操作不直接读取线程2存储的值,前提是存在发布存储操作与实际读取的存储之间的"释放顺序",只要:
但是,我没有看到任何理由为什么在实际读取的存储位于不同的线程中时也不可能同步,前提是发布存储操作仍然发生 - 在存储之前实际上正在阅读.标准明确不允许这样做吗?如果是这样,那么标准是否可能不完整是因为它有意义并且所有现有硬件都会有这样的同步?
考虑以下示例,其中a,x和y是使用0初始化的原子int.
线程1:
k = y.load(memory_order_acquire);
x.store(1, memory_order_relaxed);
Run Code Online (Sandbox Code Playgroud)
线程2:
m = x.load(memory_order_relaxed);
y.store(2, memory_order_release);
a.store(2, memory_order_release);
Run Code Online (Sandbox Code Playgroud)
线程3:
n = a.load(memory_order_acquire);
y.store(3, memory_order_relaxed);
Run Code Online (Sandbox Code Playgroud)
问题是,有可能我们最终得到k = 3,m = 1和n = 2?
如果线程2中的存储到y和线程3中的存储到y之间没有释放序列,那么在线程2中的释放存储到y之间没有同步 - 并且在线程1中没有与y的获取读取,因此线程2中的x的负载不必在线程1中存储到x之前发生,使得k,m和n的期望结果成为可能.
但是,如果有是商店至y在线程2和存储到y在螺纹3之间的释放顺序则是一个进行同步-与释放存储到y在线程2和获取在线程1读出用Y之间因此,线程2中x的负载需要在线程1中存储到x之前发生,从而无法获得所需的k,m和n结果.请注意,如果a的存储/加载不在那里,我们只是在线程2的末尾做了值为3到y的松弛存储,那么情况就是如此(所以k = 3和m =永远不会发生1).
在这种情况下,值3到y的存储发生在线程3中,但是存在使用原子变量a的释放 - 获取同步; 因此,如果n = 2,那么在值2到y的释放存储和值3到y的松弛存储之间存在先发生关系.并不意味着存在是一个释放顺序和结果,其中k = 3,M = 1且n = 2永远不会发生?
编辑
请注意,运行以下代码段:
int main()
{
atomic_int a = 0;
atomic_int x = 0;
atomic_int y = 0; …Run Code Online (Sandbox Code Playgroud) 我想知道这个代码有什么区别:
#include <type_traits>
#include <iostream>
template<typename T> using is_ref = std::enable_if_t<std::is_reference_v<T>, bool>;
template<typename T> using is_not_ref = std::enable_if_t<!std::is_reference_v<T>, bool>;
template<typename T, is_ref<T> = true>
void foo(T&&) {
std::cout << "ref" << std::endl;
}
template<typename T, is_not_ref<T> = true>
void foo(T&&) {
std::cout << "not ref" << std::endl;
}
int main() {
int a = 0;
foo(a);
foo(5);
}
Run Code Online (Sandbox Code Playgroud)
而这个不起作用:
#include <type_traits>
#include <iostream>
template<typename T> using is_ref = std::enable_if_t<std::is_reference_v<T>, bool>;
template<typename T> using is_not_ref = std::enable_if_t<!std::is_reference_v<T>, bool>;
template<typename T, typename …Run Code Online (Sandbox Code Playgroud) 我有一个启动许多客户端进程的bash脚本.这些是人工智能游戏玩家,我用它来测试与许多玩家的游戏,大约有400个连接.
我遇到的问题是AI播放器使用的问题
srand( time(nullptr) );
Run Code Online (Sandbox Code Playgroud)
但是如果所有玩家大约在同一时间开始,他们将经常收到相同的time()值,这意味着他们都在同一个rand()序列中.
测试过程的一部分是确保如果大量客户端几乎同时尝试连接,服务器就可以处理它.
我考虑过使用类似的东西
srand( (int) this );
Run Code Online (Sandbox Code Playgroud)
或类似的,基于每个实例都有唯一内存地址的想法.
还有另一种更好的方法吗?
传递std::array<char, N>给这样的函数的方法是什么:
template<size_t N>
void safe_func(char (&dest)[N]);
Run Code Online (Sandbox Code Playgroud)
?
我试试这个:
#include <array>
template <size_t N> using SafeArray = char[N];
template <size_t N> void safe_func(char (&dest)[N]) {}
int main() {
SafeArray<10> a1;
safe_func(a1);
std::array<char, 10> a2;
safe_func(*static_cast<SafeArray<10> *>(static_cast<void *>(a2.data())));
}
Run Code Online (Sandbox Code Playgroud)
它有效,但我怀疑,我的演员可能有问题,而在其他编译器或平台上(我使用gcc/linux/amd64),我遇到了错误的引用?
以下代码是否有效,例如是否带来未定义的行为?
struct S
{
int i = s.i;
static S s;
};
S S::s;
int main()
{
S a; // a.i = 0
S::s.i = 42;
S b; // b.i = 42
}
Run Code Online (Sandbox Code Playgroud)
据我所知,所有具有静态存储持续时间的变量都是零初始化.因此s.i是0在S::s创造,一切都是好的.但也许我错过了一些东西.
c++ initialization static-members language-lawyer in-class-initialization
我有一个问题,请通过以下简单的C++程序,
int main( )
{
shared_ptr<int> sptr1( new int );
shared_ptr<int> sptr2 = sptr1;
shared_ptr<int> sptr3;
shared_ptr<int> sptr4;
sptr3 = sptr2;
cout<<sptr1.use_count()<<endl;
cout<<sptr2.use_count()<<endl;
cout<<sptr3.use_count()<<endl;
sptr4 = sptr2;
cout<<sptr1.use_count()<<endl;
cout<<sptr2.use_count()<<endl;
cout<<sptr3.use_count()<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
3
3
3
4
4
4
Run Code Online (Sandbox Code Playgroud)
如何sptr1和sptr3对象知道引用计数在打印4时递增.
据我所知,引用计数是每个shared_ptr对象中的变量.
该标准定义了两种类型的布局兼容性.但是,我没有看到标准中的任何地方,当两种类型的布局兼容时后果是什么.似乎布局兼容是一种在任何地方都不使用的定义.
布局兼容的目的是什么?
注意:据说,它可能意味着类型具有相同的布局(offsetof对于每个相应的成员是相同的),因此,例如,对于简单的可复制类型,可以在它们之间复制基础字节.但是我没有在标准中看到这样的东西.
c++ ×9
c++17 ×2
shared-ptr ×2
arrays ×1
c ×1
c++11 ×1
c++14 ×1
char-pointer ×1
concurrency ×1
memory-model ×1
offsetof ×1
random ×1
random-seed ×1
sfinae ×1
stdarray ×1
stdatomic ×1
struct ×1
weak-ptr ×1