gpt4 book ai didi

c - 将数组传递给函数会导致段错误(核心转储)

转载 作者:行者123 更新时间:2023-11-30 20:24:49 25 4
gpt4 key购买 nike

我正在尝试构建一个快速排序函数,但它会导致段错误(核心转储)。读到这个错误后,我确信我有一个流浪指针,但我根本无法发现它。我还使用 valgrind 来帮助我调试这个问题,我将在下面的代码中注释 valgrind 告诉我的行:

#include <stdio.h>
#include <assert.h>

int partition(int *a,int l, int h, int pivot)
{
l++;
while(l <= h && a[l] < a[pivot])
{
l++;
}
while(a[h] >= a[pivot])
{ //conditional jump depends on uninitialised value here
h--;
}
if(l < h)
{
int tmp = a[l];
a[l] = a[h];
a[h] = tmp;
partition(a,l,h,pivot);
}
else
{
int tmp = a[pivot];
a[pivot] = a[h];
a[h] = tmp;
return h;
}
return -1;
}




void quicksort(int *a, int low, int high)
{
//base case.
if(low - high == 0){
return;
}
//just two elements in array to be sorted
if(low - high == -1)
{
if (a[low] < a[high])
{
int tmp = a[low];
a[low] = a[high];
a[high] = tmp;
}
else
{
return;
}
}
else
{
//the actual algorithm
int pivot = a[low];
int j = partition(a,low,high,low); //caused by this (valgrind)
int tmp = pivot;
a[low] = a[j];
a[j] = tmp;
quicksort(a,0,j-1); //and this (valgrind)
quicksort(a,j+1,high);
}
}


int main(int argc, char *argv[])
{
int a[] = {5,1,5,3,4,1};
quicksort(a,0,5);
int n = 0;
while(n < 6)
{
printf("index %d is %d\n",n,a[n]);
n++;
}
return 0;
}

任何见解将不胜感激

这是 Valgrind 的输出

==4698== Memcheck, a memory error detector
==4698== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==4698== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==4698== Command: ./quicksort
==4698==
==4698== Conditional jump or move depends on uninitialised value(s)
==4698== at 0x4005B8: partition (quicksort.c:9)
==4698== by 0x40079E: quicksort (quicksort.c:46)
==4698== by 0x400803: quicksort (quicksort.c:50)
==4698== by 0x40081A: quicksort (quicksort.c:51)
==4698== by 0x400803: quicksort (quicksort.c:50)
==4698== by 0x400803: quicksort (quicksort.c:50)
==4698== by 0x40086B: main (quicksort.c:61)
==4698==
==4698== Conditional jump or move depends on uninitialised value(s)
==4698== at 0x4005B8: partition (quicksort.c:9)
==4698== by 0x40079E: quicksort (quicksort.c:46)
==4698== by 0x400803: quicksort (quicksort.c:50)
==4698== by 0x400803: quicksort (quicksort.c:50)
==4698== by 0x40081A: quicksort (quicksort.c:51)
==4698== by 0x400803: quicksort (quicksort.c:50)
==4698== by 0x400803: quicksort (quicksort.c:50)
==4698== by 0x40086B: main (quicksort.c:61)
==4698==
==4698== Stack overflow in thread 1: can't grow stack to 0xffe801ff8
==4698==
==4698== Process terminating with default action of signal 11 (SIGSEGV)
==4698== Access not within mapped region at address 0xFFE801FF8
==4698== at 0x400535: partition (quicksort.c:4)
==4698== If you believe this happened as a result of a stack
==4698== overflow in your program's main thread (unlikely but
==4698== possible), you can try to increase the size of the
==4698== main thread stack using the --main-stacksize= flag.
==4698== The main thread stack size used in this run was 8388608.
==4698== Stack overflow in thread 1: can't grow stack to 0xffe801fd8
==4698==
==4698== Process terminating with default action of signal 11 (SIGSEGV)
==4698== Access not within mapped region at address 0xFFE801FD8
==4698== at 0x4A256A5: _vgnU_freeres (vg_preloaded.c:58)
==4698== If you believe this happened as a result of a stack
==4698== overflow in your program's main thread (unlikely but
==4698== possible), you can try to increase the size of the
==4698== main thread stack using the --main-stacksize= flag.
==4698== The main thread stack size used in this run was 8388608.
==4698==
==4698== HEAP SUMMARY:
==4698== in use at exit: 0 bytes in 0 blocks
==4698== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==4698==
==4698== All heap blocks were freed -- no leaks are possible
==4698==
==4698== For counts of detected and suppressed errors, rerun with: -v
==4698== Use --track-origins=yes to see where uninitialised values come from
==4698== ERROR SUMMARY: 14552 errors from 2 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)

最佳答案

问题在于底部的 if-else 语句决定是否进入递归或返回索引,引发了一个我无法看到的小错误。

如果条件为真,则函数进入递归,但是一旦返回,函数就会完成 if-else 语句并返回 -1,然后继续执行并放入索引中。然后抛出段错误错误

关于c - 将数组传递给函数会导致段错误(核心转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32298738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com