Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 尝试工作证明读取/写入Graphviz DAG值会导致SEG故障_C++_Linux_Graphviz_Directed Acyclic Graphs - Fatal编程技术网

C++ 尝试工作证明读取/写入Graphviz DAG值会导致SEG故障

C++ 尝试工作证明读取/写入Graphviz DAG值会导致SEG故障,c++,linux,graphviz,directed-acyclic-graphs,C++,Linux,Graphviz,Directed Acyclic Graphs,更新:在使用-g编译并检查每个有问题的行之后,它现在已成功地在系统级头文件中设置故障: Program received signal SIGSEGV, Segmentation fault. __gnu_cxx::new_allocator<char*>::construct<char*, char* const&> (this=0x7fffffffdec0, __p=0x5555557702f0, __args#0=<error reading var

更新:在使用-g编译并检查每个有问题的行之后,它现在已成功地在系统级头文件中设置故障:

Program received signal SIGSEGV, Segmentation fault.
__gnu_cxx::new_allocator<char*>::construct<char*, char* const&> (this=0x7fffffffdec0, 
__p=0x5555557702f0, __args#0=<error reading variable>)
at /usr/include/c++/8.1.1/ext/new_allocator.h:136
136             { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
这是gdb输出―它说它在readfile函数中,但没有具体说明它在该函数中的位置:

[realkstrawn93@archlinux Desktop]$ gdb gendag
GNU gdb (GDB) 8.1
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
<http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from gendag...(no debugging symbols found)...done.
(gdb) r 10 10 10 10 0.1 test.gv
Starting program: /home/realkstrawn93/Desktop/gendag 10 10 10 10 0.1 test.gv

Program received signal SIGSEGV, Segmentation fault.
0x0000555555555675 in readfile(std::__cxx11::basic_string<char, 
std::char_traits<char>, std::allocator<char> >) ()
[realkstrawn93@archlinux桌面]$gdb gendag
GNU gdb(gdb)8.1
版权所有(C)2018免费软件基金会。
许可证GPLv3+:GNU GPL版本3或更高版本
这是自由软件:您可以自由更改和重新发布它。
在法律允许的范围内,不存在任何担保。键入“显示复制”
和“显示保修”了解详细信息。
此GDB配置为“x86_64-pc-linux-gnu”。
键入“显示配置”以获取配置详细信息。
有关错误报告说明,请参阅:
.
在线查找GDB手册和其他文档资源,网址为:
.
要获得帮助,请键入“帮助”。
键入“apropos word”以搜索与“word”相关的命令。。。
正在从gendag读取符号…(未找到调试符号)…已完成。
(gdb)r 10 0.1试验gv
启动程序:/home/realkstrawn93/Desktop/gendag 10 0.1 test.gv
程序接收信号SIGSEGV,分段故障。
读取文件中的0x00005555675(标准::基本字符串)()
我需要知道segfault在函数中的确切位置。

//计算行数并创建行数组
//count lines and create array of lines
int linecount = 0;
std::vector<char*> lines;  <--
std::ifstream dag(name);
std::string ul;

while (std::getline(dag, ul)) {

    //convert to c_str immediately to avoid errors with atoi
    std::istringstream thisline;
    thisline.str(ul);
    lines[linecount] = const_cast<char*>(thisline.str().c_str()); <--
    ++linecount;

}
int linecount=0;
std::矢量线 事实证明,在继续之前,我必须检查数组的大小是否为0。否则,它是一个空指针解引用:

if (lines.size() == 0) {

    dagout << "   " << j << " -> " << k+nodes << ";" << std::endl;

} else {

    //each new member must be a nonce
    unsigned long long pree = lines.at(l).pre_edge;
    unsigned long long poste = lines.at(l).post_edge;
    if (std::rand() % 100 != pree) {

        if (std::rand() % 100 != poste) {

            dagout << "   " << j << " -> " << k+nodes << ";" << std::endl;

        }

    }

}
if(lines.size()==0){

dagout你是用
-g
编译它的吗?还有
bt
用于回溯(gdb命令)。尝试这两种方法来准确查看,谢谢(所有segfault代码的问题都是一样的。你只需做任何segfault,不一定会混淆你的“真实代码”),原因是一样的,你访问
可读的行[i]
没有初始化它。当我在跟踪你的程序时,我只注意到这个问题的第一次出现。很抱歉延迟,我不得不构建你的代码来验证。
thisline.str().c_str()
的类型为
const char*
,而向量的类型为
char*
。这使得它无法赋值,因为它找不到有效的构造函数。只需将向量更改为
std::vector lines
,它应该可以工作。但是,请注意
c_str()如果要返回结果的
char*
,则不可用。
方法返回的指针将在创建它的字符串超出范围时立即失效。最后一件事是使用
sizeof(行)
获取向量长度是错误的。
sizeof
返回以字节为单位的大小,这不是您想要的。您想要的是
行。size()
。别忘了更改另一个实例,在
std::vector readable_lines;
我可以使用g++和msbuild生成并运行代码,在这些更改之后不会出现任何错误。
//count lines and create array of lines
int linecount = 0;
std::vector<char*> lines;  <--
std::ifstream dag(name);
std::string ul;

while (std::getline(dag, ul)) {

    //convert to c_str immediately to avoid errors with atoi
    std::istringstream thisline;
    thisline.str(ul);
    lines[linecount] = const_cast<char*>(thisline.str().c_str()); <--
    ++linecount;

}
if (lines.size() == 0) {

    dagout << "   " << j << " -> " << k+nodes << ";" << std::endl;

} else {

    //each new member must be a nonce
    unsigned long long pree = lines.at(l).pre_edge;
    unsigned long long poste = lines.at(l).post_edge;
    if (std::rand() % 100 != pree) {

        if (std::rand() % 100 != poste) {

            dagout << "   " << j << " -> " << k+nodes << ";" << std::endl;

        }

    }

}