首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Linux mmap()错误

Linux mmap()错误
EN

Stack Overflow用户
提问于 2009-07-23 14:06:57
回答 2查看 1.9K关注 0票数 0

我有一个内存映射文件,我希望从中解析缓冲区的内容。mmap()返回成功,我可以成功地使用fprintf将缓冲区内容打印到一个文件中。然而,当我试图在我的程序中以数组的形式直接访问缓冲区时,我得到了一个分段错误。为什么会发生这种情况?代码如下:

代码语言:javascript
复制
  #define PTT_DUMP "/home/dhruv/somefile"     
  .
  .
  .

  int fd_ptt_dump = open(PTT_DUMP, O_RDONLY);
  struct stat struct_ptt_dump;
  fstat(fd_ptt_dump, &struct_ptt_dump);
  printf("\n\n\t\t\t --- The size of the dump is = %d -----\n\n",    struct_ptt_dump.st_size);
  char *membuffer;
  char pid_num[100];
  char cycles[100], instr[100], cpi[100] ;
  int pid_index =0;
  int cycles_index = 0;
  int instr_index = 0;
  int cpi_index = 0 ;
  int len = (int)struct_ptt_dump.st_size;
  int newline_count = 0;
  int n = 0;
  if( (membuffer = mmap(0, struct_ptt_dump.st_size, PROT_READ, MAP_FILE | MAP_PRIVATE, fd_ptt_dump, 0)) == (caddr_t) -1)
   err_sys("mmap error");

  /* If the following is uncommented, it prints fine */

  /*
  for ( n =0; n < struct_ptt_dump.st_size ; n++)      
  fprintf(fp_logfile,"%c", membuffer[n]);
  */

  /* But, I really want to access the buffer as an array for speed if possible */
  /* Here is where everything goes haywire */

  while (newline_count != 5)
   if ( membuffer[n++] == '\n' )
    newline_count++ ;

  /* printf returns OK, and I am able to skip newlines */

  printf("\n\n newlines = %d\n\n 10 buffer characters", newline_count);

  int k =0;


  /* All code from here gives segmentation fault */

  while ( membuffer[n++] != ' ' )
pid_num[pid_index++] = membuffer[n] ;

  /* Even if I comment out everything from here on, the above assignment itself results in a segmentation fault */


  pid_num[pid_index] = '\0';

  printf("\n\n pid = %s", pid_num);



  while ( membuffer[len--] != ' ' )
if ( membuffer[len] != '\n' )
  cpi[cpi_index++] = membuffer[len];

  cpi[cpi_index] = '\0';

  for( ; membuffer[len] == ' ' ; len-- )
;

  for(n=0; membuffer[len-n] != ' '; n++)
instr[instr_index++] = membuffer[len-n] ;

  instr[instr_index] = '\0' ;
  n++;

  for( ; membuffer[len-n] != ' ' ; n++)
cycles[cycles_index++] = membuffer[len-n];

  cycles[cycles_index] = '\0';

  printf("\n\n\t\t\t\t ********** buffer values *************\n\n");
  printf("\t\t\t\tdominant pid = %s\t cycles = %s\t instructions = %s\t cpi = %s \n\n", pid_num, cycles, instr, cpi);



  fflush(STDOUT_FILENO);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-07-23 14:20:48

pid_index的规模有多大?您是否正在阅读超过100个字符而没有找到空格?

顺便说一句,这个

代码语言:javascript
复制
while ( membuffer[n++] != ' ' )
    pid_num[pid_index++] = membuffer[n] ;

应该是这样的

代码语言:javascript
复制
while ( membuffer[n] != ' ' )
    pid_num[pid_index++] = membuffer[n++] ;

不是吗?

票数 3
EN

Stack Overflow用户

发布于 2009-07-23 23:12:30

你没有检查n停留< struct_ptt_dump.st_size

  • You没有检查pid_index停留< 100
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1172014

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档