例如,如果将一个文本文件加载到std :: string中,做了我需要做的事情,然后在其上调用clear(),这会释放保存文本的内存吗?或者我最好只是将它声明为指针,在需要时调用new,并在完成后删除它?
我知道标准中明确允许以下内容:
int n = 0;
char *ptr = (char *) &n;
cout << *ptr;
Run Code Online (Sandbox Code Playgroud)
那这个呢?
alignas(int) char storage[sizeof(int)];
int *ptr = (int *) &storage[0];
*ptr = 0;
cout << *ptr;
Run Code Online (Sandbox Code Playgroud)
本质上,我问的是,别名规则是否允许通过指向另一种类型的指针访问一系列字符.我想参考标准的部分,如果可能的话,表明这种或那种方式.
标准的某些部分让我感到矛盾; (3.10.10)似乎表明在假设动态类型storage不是的情况下它将是未定义的行为int.然而,动态类型的定义对我来说并不清楚,而且存在std::aligned_storage会让我相信这是可能的.
我理解随机访问迭代器如何对连续容器起作用std::vector:迭代器只是维护一个指向当前元素的指针,并且任何加法/减法都应用于指针.
但是,我对于如何为非连续容器实现类似的功能感到困惑.我对如何std::deque:iterator工作的第一个猜测是,它维护了一个指向它包含的连续内存组的表的指针,但我不确定.
典型的标准库如何实现这一点?
是否可以想象C++编译器会优化对只设置类变量的类成员函数的函数调用?例:
class A
{
private:
int foo;
public:
void bar(int foo_in)
{
foo = foo_in;
}
}
Run Code Online (Sandbox Code Playgroud)
所以,如果我这样做
A test;
A.bar(5);
Run Code Online (Sandbox Code Playgroud)
编译器可以优化它来直接访问成员并设置它吗?
我正在尝试将 GL 调用包装在外部函数和类中。因此,这提出了为着色器设置制服的问题:glGetUniformLocation 是一个缓慢的操作吗?如果是这样,使用 std::map 存储由字符串中的制服名称索引的制服索引会更慢还是更快?我试图避免在制服中静态编码(即使用宏等)
在我的编译器和mergesort的实现上比较我的quicksort实现和std :: sort时,我注意到大数据集上有一个奇怪的模式:当在64位整数上运行时,quicksort始终比mergesort快; 但是,在较小的int大小上,quicksort变慢,mergesort变得更快.
这是测试代码:
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <utility>
#include <random>
#include <chrono>
#include <limits>
#include <functional>
#include <cstdint>
template <typename Iterator>
void insertion_sort(Iterator first, Iterator last)
{
using namespace std;
Iterator head = first;
Iterator new_position;
while(head != last)
{
new_position = head;
while(new_position != first && *new_position < *prev(new_position))
{
swap(*new_position, *prev(new_position));
--new_position;
}
++head;
}
}
template <typename Iterator>
void recursive_mergesort_impl(Iterator first, Iterator last, std::vector<typename Iterator::value_type>& temp)
{
if(last - first …Run Code Online (Sandbox Code Playgroud) 我理解LL递归下降解析器如何处理这种形式的规则:
A = B*;
Run Code Online (Sandbox Code Playgroud)
根据前瞻标记是否与第一组B中的终端匹配,使用一个简单的循环检查是否继续循环.但是,我对基于表的LL解析器感到好奇:这种形式的规则如何在那里工作?据我所知,在一个中处理重复的唯一方法是通过右递归,但是在不需要右关联解析树的情况下会混淆相关性.
我想知道,因为我正在尝试编写一个LL(1)基于表的解析器生成器,我不知道如何在不改变预期的解析树形状的情况下处理这样的情况.
编译Boost序列化的简单测试时:
class Test
{
protected:
int Num;
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(Num);
}
public:
Test(): Num(0) {}
~Test() {}
};
Run Code Online (Sandbox Code Playgroud)
使用xml_oarchive输出,我遇到以下GCC错误:
C:\Development\Libraries\boost_1_55_0\boost\mpl\assert.hpp|289|error: no matching function for call to 'assertion_failed(mpl_::failed************ boost::serialization::is_wrapper<Test>::************)'|
Run Code Online (Sandbox Code Playgroud)
使用其他oarchive类型时,它编译并运行正常.我所做的所有研究都指出我使用BOOST_SERIALIZATION_NVP宏来解决错误,但我已经这样做了,我仍然得到这个错误.
有没有人遇到过同样的问题?
我在使用 Spirit Qi 编写解析器时遇到了一个奇怪的问题:我在某个地方有一个错误,该错误导致-O优化时崩溃,但没有优化则不会。它在语法的构造函数中崩溃:
template <typename Iterator>
struct math_expression_grammar : qi::grammar<Iterator, std::string()>
{
qi::rule<Iterator, std::string()>
expression,
term,
factorial,
factor,
pexpression,
pfactor,
nfactor,
number;
math_expression_grammar():
math_expression_grammar::base_type(expression)
{
using namespace boost::spirit;
using namespace boost::spirit::ascii;
namespace sp = boost::spirit;
namespace ph = boost::phoenix;
auto sum = term[_val = sp::_1] >> lit('+') >> term[_val += sp::_1, _val += "+ "];
auto difference = term[_val = sp::_1] >> lit('-') >> term[_val += sp::_1, _val += "- "];
auto product = factor[_val = sp::_1] …Run Code Online (Sandbox Code Playgroud) 这样对吗?我假设在 上应用内存排序std::atomic_flag不为通用锁提供同步是否正确?
#include <atomic>
class Spinlock
{
public:
Spinlock(): f(ATOMIC_FLAG_INIT) {}
void lock()
{
while(f.test_and_set(std::memory_order_relaxed));
std::atomic_thread_fence(std::memory_order_acquire);
}
void unlock()
{
std::atomic_thread_fence(std::memory_order_release);
f.clear(std::memory_order_relaxed);
}
private:
std::atomic_flag f;
};
Run Code Online (Sandbox Code Playgroud)
如果这是一个愚蠢的问题,我很抱歉,但我觉得std::atmoic_thread_fence通用锁需要一个IS,并且memory_order_acquire在test_and_set和memory_order_release上应用clear是不够的,但我也不确定。
手册页上epoll_ctl(2)有关于该EPOLLONESHOT标志的说明:
Sets the one-shot behavior for the associated file descriptor.
This means that after an event is pulled out with
epoll_wait(2) the associated file descriptor is internally
disabled and no other events will be reported by the epoll
interface. The user must call epoll_ctl() with EPOLL_CTL_MOD
to rearm the file descriptor with a new event mask.
Run Code Online (Sandbox Code Playgroud)
但是,尚不清楚是在epoll_wait插入事件数组之后还是在所有事件返回之后禁用该事件。
我正在尝试使用SSE内在函数的8个浮点数组的每个元素,只是为了学习如何使用它们.但是,当我尝试这样写时:
alignas(16) float Numbers[8] =
{0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f};
__m128 Group1 = _mm_load_ps(Numbers);
__m128 Group2 = _mm_load_ps(Numbers + 4*sizeof(float));
__m128 Zero = _mm_setzero_ps();
__m128 Sum1 = _mm_add_ps(Group1, Group2); // Sum1 = Group1 + Group2
__m128 Sum2 = _mm_hadd_ps(Sum1, Zero); // Sum2[31:0] = Sum1[31:0] + Sum1[63:32]
// Sum2[63:32] = Sum1[95:64] + Sum1[127:96]
__m128 Sum3 = _mm_hadd_ps(Sum2, Zero); // Sum3[31:0] = Sum2[31:0] + Sum2[63:32]
float Result;
_mm_store_ss(&Result, Sum3);
Run Code Online (Sandbox Code Playgroud)
Result当它应该是28时,它出现了6.我一直在指这些内在函数的参考,但我无法找出我的逻辑在这里出了什么问题.有什么建议?