2013-10-23 4 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) 

, 왜 가끔 가끔 작동 그것은 doesnt한다. 너희들이 통찰력을 줄 수 있다면 크게 감사하겠다. 감사! 모든

#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'가 빈 큐를 가지고도 free를 수행 할 수 있다는 것을 알았습니다. 그건 너의 힙을 망칠 수있어. –

답변

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"); 

} 

때문에 당신이 말하는 경우 온도 = Q-> 먼저 다음 Q-> 첫번째 = Q -> 1 세대> 다음, 당신은 무료로 할 수있는 기회가 Q -> 1 세대 > 대신 q-> first를 입력하십시오. 순서를 변경하면 실제로 오래된 포인터를 비우는 것이 좋습니다.