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

标签 c pointers segmentation-fault

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

*** 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/

相关文章:

c - 与 C99 驱动程序接口(interface)的问题

c - 带有选择但客户端 block 的linux服务器

c - 分配给多个变量的指针

c - 未知的段错误问题

c - post_menu 段错误(核心转储)

c++ - 为什么以下代码会产生段错误?

c - 使用 zend 读取 php.ini for PHP 扩展(不是 PHP 语言)

c++ - 函数参数在 64 位操作系统的寄存器中传输?

c++ - C++中整数和指针之间的转换

c++ - 指针疯狂,我只是想不通