- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在从事一个密码学项目,我们的密码需要能够读取和写入文件。我的一个合作伙伴实现了读取功能,现在我们遇到了一个奇怪的问题。如果用户没有提供输出文件,我们的程序运行正确且没有错误,但如果提供了输出文件,则在完成后(返回 0)会出现段错误。但是,该程序仅在我的机器上出现段错误。我的团队成员正在运行 OS X 和 Fedora,而我正在运行 Ubuntu,如果这很重要的话。我们所有人都在使用 c++11 进行编译,尽管我在技术上指定了 c++0x。
int main(int argc, char** argv)
{
std::string usageWarning = "usage: [-i/--input fileName] [-o/--output fileName]
[- k/--key 32bitKey] [-n/--nonce numEncryptions]";
int nonce = 1;
std::string inputFile;
std::string outputFile;
std::string key;
bool hasInputFile = false;
bool hasOutputFile = false;
bool hasKey = false;
std::ofstream out;
if (argc < 2) {
std::cerr << usageWarning << std::endl;
return 1;
}
//Parse command line input
for( int i = 1; i < argc; i++ ) {
if (0 == strncmp(argv[i], "-n", 2) || 0 == strncmp(argv[i], "--nonce", 7)) {
i = i + 1;
nonce = std::stof(argv[i]);
}
else if (0 == strncmp(argv[i], "-i", 2) || 0 == strncmp(argv[i], "--input", 7)) {
i = i + 1;
inputFile = argv[i];
hasInputFile = true;
}
else if (0 == strncmp(argv[i], "-o", 2) || 0 == strncmp(argv[i], "--output", 8)) {
i = i + 1;
outputFile = argv[i];
out.open(outputFile);
std::streambuf *coutbuf = std::cout.rdbuf(); //save old buf
std::cout.rdbuf(out.rdbuf());
hasOutputFile = true;
}
else if (0 == strncmp(argv[i], "-k", 2) || 0 == strncmp(argv[i], "--key", 5)) {
i = i + 1;
key = argv[i];
hasKey = true;
}
else {
std::cerr << "Unrecognized option." << std::endl;
std::cerr << usageWarning << std::endl;
return 1;
}
}
Serpent serpent;
unsigned char testKey[] = {0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00};
unsigned char plaintext[16] =
{0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00};
if (hasKey) {
unsigned char new_key[32];
int index = 0;
for (int i = 0; i < key.length() - 2; i++) {
std::stringstream ss;
ss << std::hex << key[i] << key[i+1];
int n;
ss >> n;
unsigned char x = (unsigned char)n;
new_key[index] = x;
index += 1;
i += 1;
}
memcpy(testKey, new_key, sizeof(testKey));
}
if (hasInputFile) {
unsigned char new_plaintext[16];
std::ifstream in(inputFile);
unsigned char x;
int index = 0;
std::string temp_string = "";
while (in >> std::noskipws >> x) {
std::cout << "X: " << std::hex << x << std::endl;
temp_string += x;
if (temp_string.length() == 2) {
std::stringstream ss;
ss << std::hex << temp_string;
int n = 0;
ss >> n;
unsigned char y = (unsigned char)n;
new_plaintext[index] = y;
index += 1;
temp_string = "";
}
}
// Set plaintext to be theplaintext we read in from file
memcpy(plaintext, new_plaintext, sizeof(plaintext));
}
else {
}
serpent.setKeySize(sizeof(testKey)/sizeof(*testKey));
serpent.setKey(testKey);
serpent.generateSubKeys();
std::cout << "TESTING" << std::endl;
int encryptionRound = 0;
while (encryptionRound < nonce) {
std::cout << std::dec << "================================ ROUND <<
encryptionRound << " ================================\n" << std::endl;
serpent.encrypt(plaintext);
std::cout << std::dec << "============================ END ROUND " <<
encryptionRound << " ============================\n"<< std::endl;
encryptionRound++;
}
if (hasOutputFile){
out.close();
}
return 0;
}
我运行了 Valgrind 并得到了以下输出,但我对 io 业务非常缺乏经验,不知道如何处理这些信息。
==3131== Memcheck, a memory error detector
==3131== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==3131== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==3131== Command: ./SerpentOpt -n 1 -o output.txt
==3131==
==3131== Invalid read of size 8
==3131== at 0x4EC7328: std::ostream::flush() (in /usr/lib/x86_64-
linux gnu/libstdc++.so.6.0.16)
==3131== by 0x4E97F9B: std::ios_base::Init::~Init() (in /usr/lib/x86_64-linux-
gnu/libstdc++.so.6.0.16)
==3131== by 0x5385900: __run_exit_handlers (exit.c:78)
==3131== by 0x5385984: exit (exit.c:100)
==3131== by 0x536B773: (below main) (libc-start.c:258)
==3131== Address 0x7fefffd88 is just below the stack ptr. To suppress, use: --
workaround-gcc296-bugs=yes
==3131==
==3131==
==3131== Process terminating with default action of signal 11 (SIGSEGV)
==3131== Bad permissions for mapped region at address 0x4224580
==3131== at 0x4224580: ??? (in /lib/x86_64-linux-gnu/ld-2.15.so)
==3131== by 0x4EC732D: std::ostream::flush() (in /usr/lib/x86_64-linux-
gnu/libstdc++.so.6.0.16)
==3131== by 0x4E97F9B: std::ios_base::Init::~Init() (in /usr/lib/x86_64-linux-
gnu/libstdc++.so.6.0.16)
==3131== by 0x5385900: __run_exit_handlers (exit.c:78)
==3131== by 0x5385984: exit (exit.c:100)
==3131== by 0x536B773: (below main) (libc-start.c:258)
==3131==
==3131== HEAP SUMMARY:
==3131== in use at exit: 0 bytes in 0 blocks
==3131== total heap usage: 10,745 allocs, 10,745 frees, 493,629 bytes allocated
==3131==
==3131== All heap blocks were freed -- no leaks are possible
==3131==
==3131== For counts of detected and suppressed errors, rerun with: -v
==3131== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)
感谢所有帮助!
最佳答案
这就是你给自己带来悲伤的地方:
out.open(outputFile);
std::streambuf *coutbuf = std::cout.rdbuf(); //save old buf
std::cout.rdbuf(out.rdbuf());
现在有两个对象都指向同一个buf。猜猜一个对象被销毁并清理其资源后会发生什么,然后第二个对象开始执行其析构函数。
关于c++ - 与文件 i/o 相关的 c++ 程序末尾的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20382863/
在使用 requests 库中的状态代码时,我遇到了一些奇怪的事情。每个 HTTP 状态代码都有一个常量,有些具有别名(例如,包括 200 的复选标记): url = 'https://httpbin
这是我得到的代码,但我不知道这两行是什么意思: o[arr[i]] = o[arr[i]] || {}; o = o[arr[i]]; 完整代码: var GLOBAL={}; GLOBAL.name
所以这个问题的答案What is the difference between Θ(n) and O(n)? 指出“基本上,当我们说算法是 O(n) 时,它也是 O(n2)、O(n1000000)、O
这是一个快速的想法;有人会说 O(∞) 实际上是 O(1) 吗? 我的意思是它不依赖于输入大小? 所以在某种程度上它是恒定的,尽管它是无限的。 或者是唯一“正确”的表达方式 O(∞)? 最佳答案 无穷
这是真的: log(A) + log(B) = log(A * B) [0] 这也是真的吗? O(log(A)) + O(log(B)) = O(log(A * B)) [1] 据我了解 O(f
我正在解决面试练习的问题,但我似乎无法找出以下问题的时间和空间复杂度的答案: Given two sorted Linked Lists, merge them into a third list i
我了解 Big-Oh 表示法。但是我该如何解释 O(O(f(n))) 是什么意思呢?是指增长率的增长率吗? 最佳答案 x = O(n)基本上意味着 x <= kn对于一些常量 k . 因此 x = O
我正在编写一个函数,该函数需要一个对象和一个投影来了解它必须在哪个字段上工作。 我想知道是否应该使用这样的字符串: const o = { a: 'Hello There' }; funct
直觉上,我认为这三个表达式是等价的。 例如,如果一个算法在 O(nlogn) + O(n) 或 O(nlogn + n) 中运行(我很困惑),我可以假设这是一个O(nlogn) 算法? 什么是真相?
根据 O'Reilly 的 Python in a Nutshell 中的 Alex Martelli,复杂度类 O(n) + O(n) = O(n)。所以我相信。但是我很困惑。他解释说:“N 的两个
O(n^2)有什么区别和 O(n.log(n)) ? 最佳答案 n^2 的复杂性增长得更快。 关于big-o - 大 O 符号 : differences between O(n^2) and O(n
每当我收到来自 MS outlook 的电子邮件时,我都会收到此标记 & nbsp ; (没有空格)哪个显示为?在 <>. 当我将其更改为 ISO-8859-1 时,浏览器页面字符集编码为 UTF-8
我很难理解 Algorithms by S. Dasgupta, C.H. Papadimitriou, and U.V. Vazirani - page 24 中的以下陈述它们将 O(n) 的总和表
我在面试蛋糕上练习了一些问题,并在问题 2给出的解决方案使用两个单独的 for 循环(非嵌套),解决方案提供者声称他/她在 O(n) 时间内解决了它。据我了解,这将是 O(2n) 时间。是我想错了吗,
关于 Java 语法的幼稚问题。什么 T accept(ObjectVisitorEx visitor); 是什么意思? C# 的等价物是什么? 最佳答案 在 C# 中它可能是: O Accept(
假设我有一个长度为 n 的数组,我使用时间为 nlogn 的排序算法对它进行了排序。得到这个排序后的数组后,我遍历它以找到任何具有线性时间的重复元素。我的理解是,由于操作是分开发生的,所以时间是 O(
总和 O(1)+O(2)+ .... +O(n) 的计算结果是什么? 我在某处看到它的解决方案: O(n(n+1) / 2) = O(n^2) 但我对此并不满意,因为 O(1) = O(2) = co
这个问题在这里已经有了答案: 11 年前关闭。 Possible Duplicate: Plain english explanation of Big O 我想这可能是类里面教的东西,但作为一个自学
假设我有两种算法: for (int i = 0; i 2)更长的时间给定的一些n - 其中n这种情况的发生实际上取决于所涉及的算法 - 对于您的具体示例, n 2)分别时间,您可能会看到: Θ(n)
这个问题在这里已经有了答案: Example of a factorial time algorithm O( n! ) (4 个回答) 6年前关闭。 我见过表示为 O(X!) 的 big-o 示例但
我是一名优秀的程序员,十分优秀!