2011-04-26 139 views
1

我寫過一個使用向量和地圖的程序。
當我運行它時,我收到以下錯誤消息:
lru:malloc.c:3552:munmap_chunk:斷言`ret == 0'失敗。 中止munmap_chunk:斷言`ret == 0'失敗

此錯誤消息的含義是什麼?

P.S.
當我用valgrind運行我的程序 - 它通過,沒有'中止'。

這裏是我的代碼:

#include <stdio.h> 
#include <stdlib.h> 
#include <iostream> 
#include <vector> 
#include <map> 
#include "byutr.h" 

using namespace std; 

/////////////////////////////////////////// 
/* DEFINE ZONE */ 
/////////////////////////////////////////// 
#define NUM_OF_ARGS 4 
#define NUM_OF_DIFF_PAGES 100000 

/////////////////////////////////////////// 
/* GLOBAL VARIABLES */ 
/////////////////////////////////////////// 

p2AddrTr tr;//a pre-defined struct 
vector<uint32_t> stack; 
vector<int> depths; 
map<uint32_t, int> pages; 
map<uint32_t, int>::iterator it; 

int main(int argc, char **argv) 
{ 
stack.reserve(NUM_OF_DIFF_PAGES); 

FILE *ifp;//TODO remove! 
// unsigned long i;//TODO int OR unsigned long?? 
int i; 
unsigned long pCnt =0; 

if(argc != NUM_OF_ARGS) 
{ 
    fprintf(stderr,"usage: lru <pageSize> <startAt> <numAccesses>\n"); 
    exit(1); 
} 

int pageSize = atoi(argv[1]); 
int startAt = atoi(argv[2]); 
int numAccesses = atoi(argv[3]); 

int k; 
//Skip some entries if needed 
for(k=0;k< startAt;k++){ 
    fread(&tr, sizeof(p2AddrTr), 1, stdin); 
} 

//size_t bytes = fread(&tr, sizeof(p2AddrTr),1, stdin); 
//fread(&tr, sizeof(p2AddrTr),1, stdin); TODO here?? 

i = 0; 

while((!feof(stdin)) && (i<numAccesses)){ 

    fread(&tr, sizeof(p2AddrTr),1, stdin); 

    //prints the address of the memory access 
    printf("%08lx ", tr.addr); 
    cout<<endl; 
    int currAddr = (tr.addr)/pageSize; 

    if(pages.find(currAddr) == pages.end()){//New page 
     pCnt++; 

     //insert the new page to the map 
     pages.insert(pair<uint32_t, int>(currAddr,pCnt)); 

     //insert the new page to the 'stack' 

     stack.push_back(currAddr); 

    } 
    else{//page already exists 

     size_t j; 
     //find the page in the stack 
     for(j=0;j<stack.size();j++){ 
      if(stack[j] == currAddr){ 
       cout << "passed stack[j]"<<endl; 
       depths.push_back(stack.size() - j); 
       break; 
      } 
     } 
     //move the page to the top of the stack 
     stack.erase(stack.begin() + (j-1)); 
     stack.push_back(currAddr); 
    } 

    i++; 
} 

return (0); 
} 
+0

請告訴我們的代碼。 – 2011-04-26 15:29:21

回答

4

我看到至少有一個錯誤:

stack.erase(stack.begin() + (j-1)); 

如果j是0,這個嘗試列表開始之前刪除的元素,導致崩潰。