当我对valgrind运行我的程序时,我遇到了以下警告.
Warning: set address range perms: large range [0x4d59d040, 0x6159d040) (undefined)
Warning: set address range perms: large range [0x194f7030, 0x2d4f7050) (noaccess)
Warning: set address range perms: large range [0x3959d030, 0x6159d050) (noaccess)
Run Code Online (Sandbox Code Playgroud)
经过一些谷歌搜索后,我在这里发现它是一个Diagnostic message, mostly for benefit of the Valgrind developers, to do with memory permissions
,这并没有告诉我多少.
我的程序确实在堆上分配了大量内存.(一堆之后可以达到2-3 GB的ram realloc
)
但是,尽管没有任何分配失败,但警告仍然出现.
那么,我想知道这个消息到底意味着什么?我没有某种内存许可?(但分配成功)
这是我在Rust文档中看到的两个函数签名:
fn modify_foo(mut foo: Box<i32>) { *foo += 1; *foo }
fn modify_foo(foo: &mut i32) { *foo += 1; *foo }
Run Code Online (Sandbox Code Playgroud)
为什么不同的位置mut
?
似乎第一个函数也可以声明为
fn modify_foo(foo: mut Box<i32>) { /* ... */ }
Run Code Online (Sandbox Code Playgroud) 编者注:这个问题是指在Rust 1.0之前的Rust部分,但是一般概念在Rust 1.0中仍然有效.
我打算制作一个标记器.我需要阅读用户键入的每一行,并在用户按下后停止阅读ctrl- D.
我四处搜索,只在Rust IO上找到了一个甚至没有编译的例子.我查看了io
模块的文档,发现该read_line()
函数是ReaderUtil
界面的一部分,但stdin()
返回了一个Reader
.
我想要的代码在C++中基本上如下所示
vector<string> readLines () {
vector<string> allLines;
string line;
while (cin >> line) {
allLines.push_back(line);
}
return allLines;
}
Run Code Online (Sandbox Code Playgroud) 我想使用constexpr填充一个枚举数组.阵列的内容遵循某种模式.
我有一个枚举将ASCII字符集分为四类.
enum Type {
Alphabet,
Number,
Symbol,
Other,
};
constexpr Type table[128] = /* blah blah */;
Run Code Online (Sandbox Code Playgroud)
我想有一个128的数组Type
.它们可以是一个结构.数组的索引将对应于ASCII字符,值将是Type
每个字符的值.
所以我可以查询这个数组,找出ASCII字符属于哪个类别.就像是
char c = RandomFunction();
if (table[c] == Alphabet)
DoSomething();
Run Code Online (Sandbox Code Playgroud)
我想知道如果没有一些冗长的宏观黑客,这是否可行.
目前,我通过执行以下操作来初始化表.
constexpr bool IsAlphabet (char c) {
return ((c >= 0x41 && c <= 0x5A) ||
(c >= 0x61 && c <= 0x7A));
}
constexpr bool IsNumber (char c) { /* blah blah */ }
constexpr bool IsSymbol (char c) { /* blah blah */ } …
Run Code Online (Sandbox Code Playgroud) 所以,假设我有一些函数来处理文件的打开/关闭.
是否更好地创建一个具有静态声明的所有这些函数的类,或者只是将"public"函数放在命名空间"file"的头文件中,并将其余的"实现细节"放在.cc文件中?
以下是代码示例.
命名空间一有点长,因为我想让它尽可能清晰.
谢谢!!
类实现
标题:
#ifndef FILE_H
#define FILE_H
#include <iostream>
#include <fstream>
include "common.h"
enum Errorcode {
FILE_CANNOT_OPEN,
FILE_CANNOT_CLOSE
};
class file {
public:
static common::Lines toLines(std::string filename);
private:
static void err(Errorcode e, std::string msg);
static void toLines(std::ifstream &ifs, common::Lines &lines);
};
#endif
Run Code Online (Sandbox Code Playgroud)
.cc文件:
/*just the implementation details of above class.*/
Run Code Online (Sandbox Code Playgroud)
命名空间实现
标题:
#ifndef FILE_H
#define FILE_H
#include <iostream>
#include <fstream>
#include "common.h"
namespace file {
common::Lines toLines(std::string filename);
}
#endif
Run Code Online (Sandbox Code Playgroud)
.cc文件:
namespace file {
enum Errorcode …
Run Code Online (Sandbox Code Playgroud) 标题有点模糊,因为我真的不知道如何定义这个问题.
它与以下代码有关:
for (match = root,
m_matchBase = match->requestedBase,
m_matchLength = match->length;
match != NULL;
match = match->next,
m_matchBase = match->requestedBase,
m_matchLength = match->length)
{
if (m_matchBase <= m_base && m_matchBase + m_matchLength > m_base)
break;
// other stuff...
}
Run Code Online (Sandbox Code Playgroud)
for循环中的语句是否保证按顺序运行?
例如,m_matchBase = match->requestedBase
保证运行之后match = match->next
?
这是源代码,类似于我在帖子"C++中的隐藏功能"中读到的代理调用函数
困扰我的唯一部分是运算符重载函数.他们是什么类型的运营商?(它们当然不像普通的operator()那样,为什么即使没有指定返回类型,它也会返回一个函数指针?
谢谢!
template <typename Fcn1, typename Fcn2>
class Surrogate {
public:
Surrogate(Fcn1 *f1, Fcn2 *f2) : f1_(f1), f2_(f2) {}
// Overloaded operators.
// But what does this do? What kind of operators are they?
operator Fcn1*() { return f1_; }
operator Fcn2*() { return f2_; }
private:
Fcn1 *f1_;
Fcn2 *f2_;
};
void foo (int i)
{
std::cout << "foo: " << i << std::endl;
}
void bar (double i)
{
std::cout << "bar: " << i << std::endl; …
Run Code Online (Sandbox Code Playgroud) Linux平台是Ubuntu 12.04
我的源代码中包含以下标头:
#include <unistd.h>
#include <signal.h>
#include <ucontext.h>
...
Run Code Online (Sandbox Code Playgroud)
然而,当我编译它时,它会抱怨 /usr/include/x86_64-linux-gnu/sys/ucontext.h:139:5: error: unknown type name 'stack_t'
我用Google搜索并发现stack_t
应该定义signal.h
,但这里似乎没有定义?
我知道下面的代码不正确,因为我正在为任意地址赋值.
#include <iostream>
using namespace std;
int main()
{
int *i;
*i = 12; // Not right.... i is not initialized.
cout << *i << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这段代码在Linux上给出了分段错误.但是,在Windows上输出12 ...
为什么它可以在Windows上运行?我没有将12分配到某个任意位置我的程序没有特权吗?