gpt4 book ai didi

c - 随机 "munmap_chunk()"和段错误

转载 作者:太空宇宙 更新时间:2023-11-04 04:47:28 28 4
gpt4 key购买 nike

我有一个问题似乎是随机的。当我运行下面的代码时,有时它会一直运行到结束,有时它会给我如下错误:

*** glibc detected *** ./Alg: munmap_chunk(): invalid pointer: 0x0000000000eba0c0 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x7ff38230ab96]
./Alg[0x40084a]
./Alg[0x400bae]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7ff3822ad76d]
./Alg[0x4005e9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 00:16 1078 /home/Students/jb2100/Desktop/Alg
00601000-00602000 r--p 00001000 00:16 1078 /home/Students/jb2100/Desktop/Alg
00602000-00603000 rw-p 00002000 00:16 1078 /home/Students/jb2100/Desktop/Alg
00eba000-00edb000 rw-p 00000000 00:00 0 [heap]
7ff382076000-7ff38208b000 r-xp 00000000 2b:00 22376 /rofs/lib/x86_64-linux-gnu/libgcc_s.so.1
7ff38208b000-7ff38228a000 ---p 00015000 2b:00 22376 /rofs/lib/x86_64-linux-gnu/libgcc_s.so.1
7ff38228a000-7ff38228b000 r--p 00014000 2b:00 22376 /rofs/lib/x86_64-linux-gnu/libgcc_s.so.1
7ff38228b000-7ff38228c000 rw-p 00015000 2b:00 22376 /rofs/lib/x86_64-linux-gnu/libgcc_s.so.1
7ff38228c000-7ff382441000 r-xp 00000000 2b:00 22378 /rofs/lib/x86_64-linux-gnu/libc-2.15.so
7ff382441000-7ff382640000 ---p 001b5000 2b:00 22378 /rofs/lib/x86_64-linux-gnu/libc-2.15.so
7ff382640000-7ff382644000 r--p 001b4000 2b:00 22378 /rofs/lib/x86_64-linux-gnu/libc-2.15.so
7ff382644000-7ff382646000 rw-p 001b8000 2b:00 22378 /rofs/lib/x86_64-linux-gnu/libc-2.15.so
7ff382646000-7ff38264b000 rw-p 00000000 00:00 0
7ff38264b000-7ff38266d000 r-xp 00000000 2b:00 22391 /rofs/lib/x86_64-linux-gnu/ld-2.15.soAborted (core dumped)

我真的不确定发生了什么,为什么有时有效有时无效。如果你们能提供一些见解,我将不胜感激。谢谢!以下是我的代码:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define SIZE 40
#define BUFSIZE 10

struct queueNode
{
int data;
struct queueNode *next;

};

struct queue
{
struct queueNode *first; //pointer to first item
struct queueNode *last; //pointer to last item


};

float calcBaseline(struct queue *q)
{
printf("Starting baseline calc\n");
struct queueNode *temp = q->first;
float base = 0;
int i, total = 0;
while(temp != NULL)
{
total += temp->data;
temp = temp->next;
}
base = total/BUFSIZE;
printf("ending baseline calc\n");
return base;
}

void enqueue(struct queue *q, int value)
{
printf("Starting enqueue\n");
struct queueNode *newNode = malloc(sizeof(struct queueNode));
newNode->data = value;
newNode->next = NULL;
if(q->first == NULL)//if queue is empty
{
q->first = q->last=newNode; //both first and last point to the new node
}
else
{
q->last->next = newNode; //append newNode after last element
q->last = q->last->next; //point "last" pointer to the new node
}
printf("ending enqueue\n");
}

void dequeue(struct queue *q)
{
printf("Starting dequeue\n");
struct queueNode *temp = q->first;
q->first = q->first->next; //moves first pointer to next item
free(temp); //deletes the old first node
printf("Ending dequeue\n");

}

void destroyQueue(struct queue *q)
{
printf("Starting destroyQueue\n");
struct queueNode *temp1 = q->first;
struct queueNode *temp = q->first->next;
while(temp != NULL)
{
free(temp1);
temp1 = temp;
temp = temp->next;
}
printf("ending destroyQueue\n");

}

int main()
{

int temp, i, j, TEST = 10;
float baseline = 0.0;
int *myArray;
myArray = malloc(SIZE * sizeof(int));
myArray[0] = 0;
srand((unsigned)time(NULL));
struct queue q;
q.first = NULL;

//initialize the queue
for(i = 0; i < BUFSIZE; i++)
{
myArray[i] = rand()%TEST;
enqueue(&q, myArray[i]);
}
baseline = calcBaseline(&q);
printf("%.2f\n",baseline);

//After baseline is established generate spikes and baseline numbers
for (i = BUFSIZE; i < SIZE; i++)
{
temp = rand()%100;
if(temp <= 90)
{
myArray[i] = rand()%TEST;
dequeue(&q);
enqueue(&q, myArray[i]);
baseline = calcBaseline(&q);
printf("%.2f\n",baseline);
}
else
{
//Assume minimum spike rise time is 10 samples
for(j = i; j < i+10; j++)
{
myArray[j] = myArray[j-1]+1;
if(myArray[j] <TEST)
{
dequeue(&q);
enqueue(&q, myArray[j]);
baseline = calcBaseline(&q);
printf("%.2f\n",baseline);
}

}
for(j = i+10; j < i+20; j++)
{
myArray[j] = myArray[j-1]-1;
if(myArray[j] <TEST)
{
dequeue(&q);
enqueue(&q, myArray[j]);
baseline = calcBaseline(&q);
printf("%.2f\n",baseline);
}
}
i+=19;
}
if(temp < 99)
{
TEST++;
}
}

for(i = 0; i < SIZE; i++)
{
printf("myArray[%d] = %d\n",i, myArray[i]);
}
destroyQueue(&q);


return 0;

}

最佳答案

首先,您没有释放以下行:

myArray = malloc(SIZE * sizeof(int));

struct queueNode *newNode = malloc(sizeof(struct queueNode));

分别是第111行和第42行。但是你的主要问题是你的函数出队,你应该有:

void dequeue(struct queue *q)
{
printf("Starting dequeue\n");
struct queueNode *temp = q->first->next;
free(q->first); //deletes the old first node
q->first = temp; //moves first pointer to next item
printf("Ending dequeue\n");
}

代替

void dequeue(struct queue *q)
{
printf("Starting dequeue\n");
struct queueNode *temp = q->first;
q->first = q->first->next; //moves first pointer to next item
free(temp); //deletes the old first node
printf("Ending dequeue\n");

}

因为如果你说 temp = q->first 然后 q->first = q->first->next,你可能会释​​放 q->first->next 而不是 q->first。通过更改顺序,您可以确保自己确实释放了旧指针。

关于c - 随机 "munmap_chunk()"和段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19550102/

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