2013-10-23 149 views
1

我有一个似乎有点随机的问题。当我运行下面的代码,有时它贯穿直到终点,有时它给了我这样的错误如下:随机“munmap_chunk()”和分段错误错误

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

} 
+1

编写一个更简单的示例来隔离问题可能是一个好主意。它也可能帮助我们有一个更小的例子 –

+0

没错;请写一个[SSCCE](http://sscce.org/)。 – AJMansfield

+0

有一件事我注意到'destroyQueue'可以执行一个空的队列甚至是免费的。这可能会破坏你的堆。 –

回答

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; 
    q->first = q->first->next; //moves first pointer to next item 
    free(temp); //deletes the old first node 
    printf("Ending dequeue\n"); 

} 

因为如果你说TEMP = Q->先Q->第一= Q->一线>接下来,还有就是你有可能把机会Q->一线>接下来而不是q->第一。通过改变顺序,你可以确信你确实释放了旧指针。