gpt4 book ai didi

c - 双重释放或损坏(出)中止(核心转储)

转载 作者:行者123 更新时间:2023-11-30 16:15:11 28 4
gpt4 key购买 nike

当我执行 popAll 函数时,出现以下错误:

双重释放或腐败(出)中止(核心转储)

我想我已经将错误来源缩小到了这个函数。 IntegerStack 是我制作的一个简单的 ADT,其中包含一个名为 item 的数组和一个名为 numItems 的 int。 popAll 位于一个单独的 .c 文件中,该文件与 main 位于同一目录中,两个 .c 文件都包含给定的头文件和所有函数声明。在浏览此论坛寻找答案后,我看到人们在释放堆内存后尝试释放堆内存时遇到此错误,这是我正在做的吗?感谢任何帮助,谢谢。

下面是我的头文件,后面是我的实现文件,后面是我的测试文件。

   //-----------------------------------------------------------------------------
// IntegerStack.h
// Header file for the IntegerStack ADT
//-----------------------------------------------------------------------------
#include<stdlib.h>
#include<stdio.h>

#ifndef _Stack_H_INCLUDE_
#define _Stack_H_INCLUDE_


// Exported type --------------------------------------------------------------

// IntegerStack
// Exported reference type
typedef struct IntegerStackObj* IntegerStack;


// Constructors-Destructors ---------------------------------------------------

// newIntegerStack()
// Constructor for the IntegerStack ADT
IntegerStack newIntegerStack();

// freeIntegerStack()
// Destructor for the Stack ADT
void freeIntegerStack(IntegerStack* pS);


// ADT operations -------------------------------------------------------------

// isEmpty()
// Returns true (1) if S is empty, false (0) otherwise.
int isEmpty(IntegerStack S);

// push()
// Pushes x on top of S.
void push(IntegerStack S, int x);

// pop()
// Deletes and returns integer at top of S.
// Pre: !isEmpty(S)
int pop(IntegerStack S);

// peek()
// Returns integer at top of S.
// Pre: !isEmpty(S)
int peek(IntegerStack S);

// popAll()
// Resets S to the empty state.
void popAll(IntegerStack S);


// Other Operations -----------------------------------------------------------

// printIntegerStack()
// Prints a space separated list of integers in S, from top to bottom, to the
// filestream out.
void printIntegerStack(FILE* out, IntegerStack S);

// equals()
// Returns true (1) if S and T are matching sequences of integers, false (0)
// otherwise.
int equals(IntegerStack S, IntegerStack T);


#endif


//-----------------------------------------------------------------------------
// IntegerStack.c
// implementation file for the IntegerStack ADT
//-----------------------------------------------------------------------------
#include<stdlib.h>
#include<stdio.h>
#include"IntegerStack.h"

void doubleItemArray(IntegerStack S);
int arrayIndex(int StackIndex);
// InitialSize
static const int InitialSize = 1;


// Exported type --------------------------------------------------------------

// IntegerStack
// Exported reference type
typedef struct IntegerStackObj {
int* item; // array of IntegerStack items
int numItems; // number of items in this integerStack
int physicalSize; // current length of underlying array
}IntegerStackObj;


// Constructors-Destructors ---------------------------------------------------

// newIntegerStack()
// Constructor for the IntegerStack ADT
IntegerStack newIntegerStack() {
IntegerStack S = (IntegerStackObj*)malloc(sizeof(IntegerStackObj));
S-> item = (int*)calloc(InitialSize, sizeof(int));
S->numItems = 0;
S->physicalSize = InitialSize;
return S;
}

// freeIntegerStack()
// Destructor for the Stack ADT
void freeIntegerStack(IntegerStack* pS) {
if (pS != NULL && *pS != NULL) {
free((*pS)->item);
free(*pS);
*pS = NULL;
}
}


// ADT operations -------------------------------------------------------------

// isEmpty()
// Returns true (1) if S is empty, false (0) otherwise.
int isEmpty(IntegerStack S) {
if (S == NULL) {
fprintf(stderr, "IntegerList Error: isEmpty() called on NULL
IntegerStack reference");
exit(EXIT_FAILURE);
}
return (S->numItems == 0);
}

// push()
// Pushes x on top of S.
void push(IntegerStack S, int x) {
int i;
if (S == NULL) {
fprintf(stderr, "IntegerList Error: push() called on NULL IntegerStack reference");
exit(EXIT_FAILURE);
}
// increase physical size of array if necessary
if ((S->numItems) == (S->physicalSize)) {
doubleItemArray(S);
}

// shift elements in stack to the right to add 1
for (i = (S->numItems); i >= 0; i--) {
S->item[arrayIndex(i + 1)] = S->item[arrayIndex(i)];
}
S->item[arrayIndex(1)] = x;
S->numItems++;
}

// pop()
// Deletes and returns integer at top of S.
// Pre: !isEmpty(S)
int pop(IntegerStack S) {
int num;
int i;
// check !isEmpty(S)
if (S == NULL) {
fprintf(stderr, "IntegerList Error: pop() called on NULL IntegerStack reference");
exit(EXIT_FAILURE);
}

// set num to the returned int before it is overwritten
num = S->item[arrayIndex(1)];
for (i = 1; i <= (S->numItems); i++) { //check the i............................
S->item[arrayIndex(i - 1)] = S->item[arrayIndex(i)];
}
S->numItems--;
return num;
}

// peek()
// Returns integer at top of S.
// Pre: !isEmpty(S)
int peek(IntegerStack S) {
// check if S is empty first
if (S == NULL) {
fprintf(stderr, "IntegerList Error: peek() called on NULL IntegerStack reference");
exit(EXIT_FAILURE);
}
return S->item[arrayIndex(1)];
}

// popAll()
// Resets S to the empty state.
void popAll(IntegerStack S) {
// check if S is empty first to prevent potential seg fault
if (S == NULL) {
fprintf(stderr, "IntegerList Error: popAll() called on NULL IntegerStack reference");
exit(EXIT_FAILURE);
}

// free the item array, and set numItems to 0
free((S->item));
S->numItems = 0;
}


// Other Operations -----------------------------------------------------------

// printIntegerStack()
// Prints a space separated list of integers in S, from top to bottom, to the
// filestream out.
void printIntegerStack(FILE* out, IntegerStack S) {
// check if S is empty first
if (S == NULL) {
fprintf(stderr, "IntegerList Error: printIntegerStack() called on NULL IntegerStack reference");
exit(EXIT_FAILURE);
}

for (int i = 1; i <= (S->numItems); i++) {
fprintf(out, "%d ", (S->item[arrayIndex(i)]));
}
fprintf(out, " \n"); // sets it up to print on the next line when the annother thing needs to be printed
}

// equals()
// Returns true (1) if S and T are matching sequences of integers, false (0)
// otherwise.
int equals(IntegerStack S, IntegerStack T) {
int i, eq;
if (S == NULL || T == NULL) {
fprintf(stderr, "IntegerList Error: equals() called on NULL IntegerStack reference");
exit(EXIT_FAILURE);
}

eq = ((S->numItems) == (T->numItems));
for (i = 1; eq && i <= (S->numItems); i++) {
eq = ((S->item[arrayIndex(i)]) == (T->item[arrayIndex(i)]));
}
return eq;
}

// doubleItemArray
// Doubles the physical size of the underlying array L->item.
void doubleItemArray(IntegerStack S) {
int i;
int* newArray;
int* oldArray = S->item;
S->physicalSize *= 2;
newArray = (int*)calloc(S->physicalSize, sizeof(int));
for (i = 0; i < (S->numItems); i++) {
newArray[i] = oldArray[i];
}
S->item = newArray;
free(oldArray);
}

// arrayIndex()
// transforms an IntegerList index to an Array index
int arrayIndex(int StackIndex) {
return StackIndex - 1;
}







//-----------------------------------------------------------------------------
//
// IntegerStackTest.c
// Test client for the IntegerStack ADT
//-----------------------------------------------------------------------------
#include<stdlib.h>
#include<stdio.h>
#include"IntegerStack.h"
int main() {
int i, n = 45;
IntegerStack A = newIntegerStack();
IntegerStack B = newIntegerStack();

for (i = 1; i <= n; i++) {
push(A, i);
push(B, i);
}

printIntegerStack(stdout, A);
printIntegerStack(stdout, B);
printf("%s\n", equals(A, B) ? "true" : "false");

for (i = 1; i <= n / 2; i++) {
printf("%d ", pop(B));
}
printf("\n");
printIntegerStack(stdout, B);
printf("%s\n", equals(A, B) ? "true" : "false");

for (i = 1; i <= n / 2; i++) {
printf("%d ", peek(A));
pop(A);
}
printf("\n");
printIntegerStack(stdout, A);
printf("%s\n", equals(A, B) ? "true" : "false");

popAll(A);
printf("%s\n", equals(A, B) ? "true" : "false");
popAll(B);
printf("%s\n", equals(A, B) ? "true" : "false");

freeIntegerStack(&A);
freeIntegerStack(&B);

return EXIT_SUCCESS;
}

该函数应该将 S 重置为空状态。相反,它给我带来了溃疡。

最佳答案

您仅测试 SNULL ,但你有空S->item 。另请检查是否 S->itemNULL (或者如果你做得正确的话,如果 S->numItems==0 )。

正如 SamiKuhmonen 所说,你应该调试你的代码。如果您使用的是 Linux,请查看 valgrindGDB 。 Valgrind 帮助您查找内存管理错误和内存泄漏。而 GDB 可以帮助您查看代码执行过程中发生的情况。

关于c - 双重释放或损坏(出)中止(核心转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57247872/

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