- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写代码来查找合适的输入,为 SHA-1 哈希函数生成特定的输出。
我遇到的问题是我的代码引发了段错误,但是 gdb
发现它在输入 main()
和执行之前引发了以下错误任何其他代码:
Program received signal SIGSEGV, Segmentation fault.
__strncpy_sse2_unaligned () at ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S:636
636 ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S: No such file or directory.
这是我的代码:
#include <iostream>
#include <cstdlib>
#include <cstring>
#include "sha1.hpp"
int main() {
char *prefix = "SHA1sha1";
char *suffix = "chicken and beer";
std::string hashvalue = "nonzero";
char *line = "just some dummy string";
int loop_number = 0;
while (hashvalue.c_str()[0] != '0' || hashvalue.c_str()[1] != '0') {
// change prefix
strncpy(prefix, hashvalue.c_str(), 8);
// hash the concatenated string of prefix and suffix
strncpy(line, prefix, 8);
strncat(line, suffix, strlen(suffix));
hashvalue = sha1(line);
loop_number++;
if (loop_number % 1000 == 0) std::cout << loop_number << "th loop, hash value: " << hashvalue << std::endl;
}
std::cout << "Found prefix: " << prefix << " with hash value: " << hashvalue << std::endl;
return 0;
}
sha1.hpp
不是我实现的,而是从这里获取的:http://www.zedwood.com/article/cpp-sha1-function
不过,我已将 sha1.h
更改为 sha1.hpp
,但这可能不是导致段错误的原因。
现在我已经尝试使用错误消息以及关键字“main之前的段错误”来搜索此问题的解决方案,并且这篇文章似乎遇到了类似的问题:Segmentation Fault before main
但是,我研究了两个建议的解决方案,但找不到适合我的解决方案。
我认为我的代码堆栈中没有太多变量。事实上,为了以防万一,我尝试过使用函数 sha1()
进行注释,但还是出现了同样的问题。
在使用之前,我已经初始化了代码中的所有 char*
和 std::string
。
仅供引用,我正在使用 g++
编译我的 C++ 代码。
任何帮助或插入正确的方向将不胜感激。
最佳答案
您正在修改不可变内容。
// change prefix
strncpy(prefix, hashvalue.c_str(), 8);
// hash the concatenated string of prefix and suffix
strncpy(line, prefix, 8);
strncat(line, suffix, strlen(suffix));
尝试按如下方式更改声明。
char prefix[100] = "SHA1sha1";
char suffix[200] = "chicken and beer";
char line[200] = "just some dummy string
<小时/>
另外,我想
while (hashvalue.c_str()[0] != '0' || hashvalue.c_str()[1] != '0') {
应该是
while (hashvalue.c_str()[0] != '0' && hashvalue.c_str()[1] != '0') {
更新:
德摩根定律规定,
not (A and B) = not A or not B
同样,使用他们想要的任何形式都是个人的选择
关于c++ - 到达主程序之前出现段错误 - C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58325178/
我有一个主程序X,它从我的网络摄像头获取数据。 我想在执行时实时配置 X。 我知道这样做的常见方法之一是使用IPC,如命名管道/Unix套接字/互联网套接字等。但我想避免每个调用者必须单独打开套接字/
信号可以在任何线程或主程序本身中接收。我从主程序创建了一个辅助线程。所以我的程序中有两个线程 1. 主线程(进程本身) 2. 辅助线程。我只希望每当信号到达我的辅助线程时,它应该向我的主线程(程序)发
我在 java 中有一个主类,它调用 JFrame Two。 但在调用 JFrame Two 之前,我的主要检查一个条件,如果为真,则调用 JFrame One。 所以,我的 main 不扩展 JFr
我正在尝试编译一个 C++ 程序,但它不起作用。首先,我应该说 c++ 不是我真正了解的语言,我使用 Fortran。无论如何,主要的 c++ 程序调用一个 fortran 子例程。我可以编译这个子例
我正在尝试将 Rust 绑定(bind)到 nbdkit,但运气不佳。我需要制作一个 .so 文件,这很简单。 .so 文件必须有一个名为 plugin_init 的公共(public)函数,这也很简
QT 似乎是最好的跨平台 GUI 工具包。不幸的是,它是在 C++ 中,它与许多有趣的语言(例如 *nix 上的 D、Rust、Julia 和 Mono)的绑定(bind)要么不可用,要么没有维护。
我有一个 Maven 项目,它在 src 文件夹中的一个包中有 2 个主程序(MyTestApp_A 和 MyTestApp_B)。 如果我打开它们并单击运行按钮,我可以在 Eclipse 中运行这些
我是一名优秀的程序员,十分优秀!