我第一次尝试实现堆排序算法,但是使用heapify函数时我得到了一个错误。
Unhandled exception at 0x0005369A in heapify.exe: Stack cookie instrumentation code detected a stack-based buffer overrun.
控制台是打开的,输出是999 10 5 11 1012398875 2 0 1。
有人能帮我弄清楚这里出了什么问题吗?谢谢。
#include <iostream>
// given the address of element 0 of an array, and a non-zero index k, heapify assumes that the L/R subtrees
// of node k are max heaps. But the subtrees combined with node k do not necesarily form
// a max heap. heapify interchanges the value at node k with the value of one of its children,
// and then calls itself on the subtree in question
int heapify(int* n, int k, int sizeOfHeap)
{
// terminate the function if the input "node" is not actually a node
if (k > sizeOfHeap)
{
return 0;
}
int root = *(n + k); // value of kth node
int leftChild = *(n + 2 * k); // value of left chold
int rightChild = *(n + 2 * k + 1); // value of right child
if (root < leftChild)
{
// swap value of kth node with value of its left child
int temp = root;
*(n + k) = leftChild;
*(n + 2 * k) = root;
// call heapify on the left child
heapify(n, 2 * k, sizeOfHeap);
}
else
{
// swap value of kth node with value of its right child
int temp = root;
*(n + k) = rightChild;
*(n + 2 * k + 1) = root;
// call heapify on right child
heapify(n, 2 * k + 1, sizeOfHeap);
}
}
int main()
{
// arr is the array we will heapify. 999 is just a placeholder.
// The actual (almost) heap occupies indices 1 - 7
int arr[8] = {999, 3, 10, 11, 5, 2, 0, 1};
int sizeOfHeap = 8;
heapify(arr, 1, sizeOfHeap);
// print out arr
int i;
for (i = 0; i <= 7; i++)
{
std::cout << arr[i] << std::endl;
}
}发布于 2021-07-12 19:57:02
Unhandled exception at 0x0005369A in heapify.exe: Stack cookie instrumentation code detected a stack-based buffer overrun.控制台是打开的,输出是999 10 5 11 1012398875 2 0 1。 有人能帮我弄清楚这里出了什么问题吗?谢谢。
进程的堆栈 ( 堆栈数据结构,文件队列的实际使用之一)是内存中静态分配的位置。对于所有进程来说,总是很小,而且大部分都是相同的大小。在堆栈上,编译器仍然保存局部变量,即静态分配的小缓冲区(在Linux上,堆栈指针被移动以扩展堆栈大小,编译器在堆栈上计算偏移量)。它们(缓冲区)无法正确处理(不安全的库函数,如strcpy()),因此它们可能会被溢出(溢出),从而导致缓冲区溢出漏洞。
堆栈cookie AKA堆栈金丝雀是一种缓解技术,用于在堆栈上写入顺序数据,而攻击者试图利用像堆栈缓冲区溢出这样的漏洞,但不限于(如果您要将堆栈从堆转回堆,但严重重写保存的指令指针.(无心;)如果检测到溢出,则会引发SegFault。示例链接与利用示例。
这回答了你的直接问题(了解出了什么问题)。
现在,您应该调试它,然后缩小问题范围。特别是询问next问题,而不是再次编辑。
https://stackoverflow.com/questions/68353051
复制相似问题