我已经编写了一个程序来在C ++中对数组执行mergesort。程序编译很好,但是当我运行它时,它会说 - “分段错误(核心转储)”然后退出。我一直试图调试它但无济于事。我确定我在某个地方误用了指针(因此出现了分段错误),但不能放在哪里。有人可以帮帮我吗?感谢。
#include<iostream>
#include<queue>
#include<algorithm>
#include<math.h>
using namespace std;
void merge(int a[],int start, int middle, int end)
{
int i;
queue<int> buffer1;
queue<int> buffer2;
for(i = start; i<=middle; i++)
buffer1.push(a[i]);
for(i = middle+1; i<=end; i++)
buffer2.push(a[i]);
i = start;
while((!buffer1.empty()) || (!buffer2.empty()))
{
if(buffer1.front() < buffer2.front())
{
a[i] = buffer1.front();
buffer1.pop();
i++;
}
else
{
a[i] = buffer2.front();
buffer2.pop();
i++;
}
}
while(!buffer1.empty())
{
a[i] = buffer1.front();
buffer1.pop();
i++;
}
while(!buffer2.empty())
{
a[i] = buffer2.front();
buffer2.pop();
i++;
}
}
void mergeSort(int a[], int start, int end)
{
int middle;
if(start<end)
{
middle = (start+end)/2;
mergeSort(a,start,middle);
mergeSort(a,middle+1,end);
merge(a, start, middle, end);
}
}
int main()
{
int b[6] = {7,8,0,4,5,1},s = 0, e = 5;
mergeSort(b,s,e);
for(int i = 0; i<6; i++)
cout<<b[i]<<endl;
}
以下是调试器输出的内容 -
execve("./q1", ["./q1"], [/* 63 vars */]) = 0
brk(0) = 0x1cde000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fee90911000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=116033, ...}) = 0
mmap(NULL, 116033, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fee908f4000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/usr/lib/x86_64-linux-gnu/libstdc++.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0 \240\10\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=1566568, ...}) = 0
mmap(NULL, 3675328, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fee9036e000
mprotect(0x7fee904e1000, 2093056, PROT_NONE) = 0
mmap(0x7fee906e0000, 49152, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x172000) = 0x7fee906e0000
mmap(0x7fee906ec000, 13504, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fee906ec000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libgcc_s.so.1", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\360*\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=92504, ...}) = 0
mmap(NULL, 2188352, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fee90157000
mprotect(0x7fee9016d000, 2093056, PROT_NONE) = 0
mmap(0x7fee9036c000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x15000) = 0x7fee9036c000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0`\v\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1869392, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fee908f3000
mmap(NULL, 3972864, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fee8fd8d000
mprotect(0x7fee8ff4d000, 2097152, PROT_NONE) = 0
mmap(0x7fee9014d000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1c0000) = 0x7fee9014d000
mmap(0x7fee90153000, 16128, PROT_READ|PROT_WRITE, `enter code here`MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fee90153000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libm.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\240U\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=1084840, ...}) = 0
mmap(NULL, 3174760, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fee8fa85000
mprotect(0x7fee8fb8c000, 2093056, PROT_NONE) = 0
mmap(0x7fee8fd8b000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x106000) = 0x7fee8fd8b000
close(3) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fee908f2000
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fee908f0000
arch_prctl(ARCH_SET_FS, 0x7fee908f0740) = 0
mprotect(0x7fee9014d000, 16384, PROT_READ) = 0
mprotect(0x7fee8fd8b000, 4096, PROT_READ) = 0
mprotect(0x7fee9036c000, 4096, PROT_READ) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fee908ef000
mprotect(0x7fee906e0000, 40960, PROT_READ) = 0
mprotect(0x604000, 4096, PROT_READ) = 0
mprotect(0x7fee90913000, 4096, PROT_READ) = 0
munmap(0x7fee908f4000, 116033) = 0
brk(0) = 0x1cde000
brk(0x1d10000) = 0x1d10000
--- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=0} ---
+++ killed by SIGSEGV (core dumped) +++
Segmentation fault (core dumped)
答案 0 :(得分:3)
您的while
条件似乎有误。只要两个容器中的一个不为空,然后随后调用它们上的front()
成员函数,就会循环。您应该循环,直到它们都包含元素,即在第21行使用&&
而不是||
。
所以这一行
while((!buffer1.empty()) || (!buffer2.empty())) // change to &&
我不确定这是否会破坏您的代码的逻辑正确性,但这是segfault的原因(它来自第25行)
答案 1 :(得分:1)
段错误发生在:if (buffer1.front() < buffer2.front())
queue
通常只是作为deque
或list
的包装器实现的,您可以在其方法中找到更加描述性的警告,禁止调用front
queue
的方法:
如果您在Visual Studio中的Debug下运行,当您遇到段错误时,它会弹出此对话框,您可以点击“重试”进行调试,保留所有状态:
你问题在于这条线:
while ((!buffer1.empty()) || (!buffer2.empty()))
这意味着如果 <{strong> while
或 buffer1
非空,则会执行buffer2
- 循环。您需要将该条件更改为:
while (!empty(buffer1) && !empty(buffer2))