2011-04-20 5 views
0

누구든지 아래 코드를 도와주세요. 각 포인터가 내 파일의 새 행을 가리 키도록 여러 포인터 [포인터 배열]를 만들려고합니다. 아래 코드는, 내 파일 (즉, 처음 네 개의 라인이 제대로 읽기)에서 읽는 첫 번째 단어의 4 반복을 위해 잘 작동하고 밖으로 잘못한다 :파일 포인터 배열 중 세그먼트 오류 초기화

int main() { 

    int num_row = 5; 
    string line=""; 
    long len=0; 
    unsigned long *Bpos = (unsigned long *)malloc(sizeof(unsigned long)*num_row); 
    int i = 0; 

    //get the byte position for each of the row 
    ifstream infile("out.txt"); 
    Bpos[i++]=0; 
    while(getline(infile, line)){ 
      len = strlen(line.c_str()); 
      Bpos[i++] = len+1; 
      cout << i-1 << ": "; 
      cout << Bpos[i-1] << ": " ; 
      cout << line << endl; 
    } 

    char re[1000];// = char(*)malloc(sizeof([100]; 
    FILE * pFile; 
    pFile = fopen("out.txt", "r"); 
    FILE ** fRowPtr = (FILE **)malloc(sizeof(FILE *)*num_row*20); 
    int rowInd = 0; 
    long byteNum =0; 

//open and initialize the row pointers 
for(rowInd=1; rowInd < num_row; rowInd++) 
{ 
    fRowPtr[rowInd] = fopen("out.txt", "r"); 
    if(fRowPtr[rowInd]!=NULL) 
    { 
     fseek (fRowPtr[rowInd] ,byteNum , SEEK_SET); 
     byteNum += Bpos[rowInd]; 
     fgets(re,20,fRowPtr[rowInd]); 
     cout << "word read : " << atof(re) << endl; 
    } 

} 
return 0; 

} 

내가 readword과의 cout을 약간 변경 오류는 munmap_chunk() 또는 세그먼트 오류 사이에서 번갈아 나타납니다. 당신이 입력 4 개 이상의 라인을 읽을 때

*** glibc detected *** ./i: munmap_chunk(): invalid pointer: 0x09f5f020 *** 
======= Backtrace: ========= 
/lib/libc.so.6(cfree+0x188)[0x179b18] 
/lib/libc.so.6(fclose+0x136)[0x167c96] 
/usr/lib/libstdc++. 

Valgrind의이


--19892-- Reading syms from /lib/libm-2.5.so (0xd6e000) 
--19892-- Reading syms from /lib/libgcc_s-4.1.2-20080825.so.1 (0x911000) 
--19892-- object doesn't have a symbol table 
--19892-- Reading syms from /lib/libc-2.5.so (0xc14000) 
--19892-- REDIR: 0xc84540 (rindex) redirected to 0x4006550 (rindex) 
--19892-- REDIR: 0xc853e0 (memset) redirected to 0x4006b80 (memset) 
--19892-- REDIR: 0xc841a0 (strlen) redirected to 0x4006800 (strlen) 
--19892-- REDIR: 0xc7fe30 (malloc) redirected to 0x400587e (malloc) 
--19892-- REDIR: 0x4c45bd0 (operator new[](unsigned int)) redirected to 0x4005ca1 (operator new[](unsigned int)) 
--19892-- REDIR: 0xc858d0 (memcpy) redirected to 0x4007a70 (memcpy) 
--19892-- REDIR: 0xc84ee0 (memchr) redirected to 0x40069f0 (memchr) 
--19892-- REDIR: 0x4c45a90 (operator new(unsigned int)) redirected to 0x4006049 (operator new(unsigned int)) 
--19892-- REDIR: 0xc85440 (mempcpy) redirected to 0x40072d0 (mempcpy) 
1: 22: 35.499645 239.034012 
--19892-- REDIR: 0x4c44560 (operator delete(void*)) redirected to 0x40051af (operator delete(void*)) 
2: 23: 179.292328 160.118195 
3: 21: 9.101529 272.455933 
4: 23: 232.154388 135.067001 
==19892== Invalid write of size 4 
==19892== at 0x8048CF4: main (fp2.cpp:22) 
==19892== Address 0x401b03c is 0 bytes after a block of size 20 alloc'd 
==19892== at 0x4005903: malloc (vg_replace_malloc.c:195) 
==19892== by 0x8048C47: main (fp2.cpp:14) 
==19892== 
==19892== Invalid read of size 4 
==19892== at 0x8048D2F: main (fp2.cpp:24) 
==19892== Address 0x401b03c is 0 bytes after a block of size 20 alloc'd 
+0

아마도 "세그먼트 오류"를 의미 할 것입니다. 이 문맥에는 "세그먼트"또는 "세그먼트 오류"같은 것이 없습니다. _valgrind_는 무엇을 말합니까? 또한 질문의 형식을 올바르게 지정하십시오. 제출 페이지에는 미리보기 창이 있습니다. –

+0

내 "out.txt"파일은 다음과 같습니다 : – Aks

+3

왜 모든 추악한 배열과 malloc입니까? C++을 컴파일 중이므로 C++로 작성하십시오. 당신은 그것에 대해 더 행복 할거야. 의견에 대한 –

답변

0

결과, 당신은 당신이 5의 값을 할당 한 이후, Bpos의 끝을지나 갈 것입니다. 첫 번째 반복은 인덱스 1에서 작동하므로 다섯 번째 반복은 인덱스 5에서 작동하지만 해당 배열의 인덱스에서 유효한 최대 값은 4입니다.

또한이 경우에는 20 개의 파일 포인터를 할당해야합니다. 각 행 :

FILE ** fRowPtr = (FILE **)malloc(sizeof(FILE *)*num_row*20); 

그리고 이것은 첫 번째 (rowInd == 0) 항목을 건너 뜁니다 당신이 원하는 것이 무엇입니까?

for(rowInd=1; rowInd < num_row; rowInd++) 
+0

thnx를 사용하여 row_num을 파일의 행 수와 일치하도록 조정했습니다. 세그먼트 오류가 있습니다. rowind 1을 건너 뛰어도 좋습니다. 포인터가 각 행의 구걸을 가리 키도록 만들려고합니다. * 20 메모리 공간을 할당하는 것은 포인터를 할당 할 때 메모리 부족을 확인하려는 필사적 인 시도였습니다. 하지만, 난 여전히 세그먼트 오류 – Aks

+0

단어를 읽을 수 : 435.849 단어 읽기 : 372.643 단어 읽기 : 382.02 단어 읽기 : 319.558 단어 읽기 : 138.061 단어 읽기 : 472.916 분할 오류 - 내 현재 O/P – Aks

+0

당신을 감사 너무 많은 사람들 !! 내 pgm (: – Aks