2013-03-26 58 views
2

我嘗試在iOS設備上使用mmap用下面的代碼MMAP iOS上有時會返回爲0xffffffff

struct stat s; 
    int status; 
    size_t size; 
    int fd; 

    fd = open ([dataFile cStringUsingEncoding:NSUTF8StringEncoding], O_RDONLY); 
    fcntl(fd, F_NOCACHE); 
    status = fstat (fd, & s); 
    if (status < 0) 
    { 
     // error handling 
    } 
    size = s.st_size; 
    FuncFileLog(@"%@", @"before read"); 
    off_t offset = 0; 
    char* data = (char *) mmap(NULL, size, PROT_READ, MAP_SHARED, fd, offset); 

    char *pch; 
    int lastPosInString = 0; 

    pch=strchr(data,'\n'); 

long lineCounter = 0; 

    while (pch != NULL) 
    { 
     size_t lineLength = 0; 

     int posInString = pch - data + 1; 
     lineLength = posInString - lastPosInString; 

     char *out = calloc(lineLength, sizeof(char)); 

     memcpy(out, data + lastPosInString, lineLength); 
     out[lineLength - 1] = '\0'; 

     if (lineCounter > 0) 
     { 
      // do something 
     } 

     lastPosInString = posInString; 
     pch=strchr(pch+1,'\n'); 

     lineCounter = lineCounter + 1; 
    } 

    munmap((void *)data, size); 
    close(fd); 

它的工作原理,但有時沒有理由,當我讀到一個文件約350萬桶我得到的,因爲我的數據的EXC_BAD_ACCESS來自mmap的指針是0xffffffff這是我認爲的錯誤。 它似乎只發生在模擬器中。

我得到 errno:12 Cannot allocate memory 目前僅在

回答

2

與 後衛的malloc,邊後衛,塗鴉和殭屍模擬器我建議閱讀mmap手冊頁(打開Terminal.app並鍵入man mmap

RETURN VALUES 
    Upon successful completion, mmap() returns a pointer to the mapped region. 
    Otherwise, a value of MAP_FAILED is returned and errno is set to indicate 
    the error. 

還有一部分描述了可能設置的errno

+0

謝謝,現在我看到它的errno:12不能分配內存 它現在發生只有當我在模擬器中運行與警衛malloc和塗鴉 – Sebastian 2013-03-26 19:45:41

相關問題