初始化文件指针数组时出现段错误

发布于 2024-11-02 14:21:31 字数 3183 浏览 0 评论 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;

}

我对 cout 进行了微小的更改readword 和错误在 munmap_chunk() 或段错误之间交替。

*** 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

Can anyone please help me with the code below. I'm trying to create a bunch of pointers [pointer array] so that each would point to a new row in my file. Below is the code,it works fine for 4 iterations of reading first word from my file (i.e. first four lines are read correctly)and then errs out:

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;

}

I make minor changes to the cout for readword and the error alternates between the munmap_chunk() or the segment error.

*** 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 Results


--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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栖竹 2024-11-09 14:21:32

当您读取超过 4 行输入时,您将超出 Bpos 的末尾,因为您只分配了 5 个值。第一次迭代对索引 1 进行操作,因此第五次迭代对索引 5 进行操作,但该数组中索引中最大的有效值为 4。

另外,这没有意义,您为每行分配 20 个文件指针:

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

并且这将跳过第一个 (rowInd == 0) 项目,这是您想要的吗?

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

When you read more than 4 lines of input, you'll go past the end of Bpos, since you've only allocated 5 values. The first iteration operated on index 1, so the 5th iteration operates on index 5, but the largest valid in index in that array is 4.

Also, this doesn't make sense, you're allocating 20 file pointers for each row:

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

And this will skip the first (rowInd == 0) item, is that what you want?

for(rowInd=1; rowInd < num_row; rowInd++)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文