1

我有一个似乎有点随机的问题。当我运行以下代码时,有时它会一直运行到完成,有时它会给我如下错误:

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

}
4

1 回答 1

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。通过更改顺序,您可以确定您确实释放了旧指针。

于 2013-12-29T16:32:13.170 回答