누구든지 아래 코드를 도와주세요. 각 포인터가 내 파일의 새 행을 가리 키도록 여러 포인터 [포인터 배열]를 만들려고합니다. 아래 코드는, 내 파일 (즉, 처음 네 개의 라인이 제대로 읽기)에서 읽는 첫 번째 단어의 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
아마도 "세그먼트 오류"를 의미 할 것입니다. 이 문맥에는 "세그먼트"또는 "세그먼트 오류"같은 것이 없습니다. _valgrind_는 무엇을 말합니까? 또한 질문의 형식을 올바르게 지정하십시오. 제출 페이지에는 미리보기 창이 있습니다. –
내 "out.txt"파일은 다음과 같습니다 : – Aks
왜 모든 추악한 배열과 malloc입니까? C++을 컴파일 중이므로 C++로 작성하십시오. 당신은 그것에 대해 더 행복 할거야. 의견에 대한 –