简体   繁体   English

无法在第一个地址之外访问malloc的内存

[英]cannot access malloc'ed memory beyond first address

Reading in a file, memory is dynamically allocated for a string where the file contents will be put. 读取文件时,将为放置文件内容的字符串动态分配内存。 This is done inside of a function, the string is passed as char **str . 这是在函数内部完成的,字符串作为char **str传递。

Using gdb I find that a seg fault is produced at the line **(str+i) = fgetc(aFile); 使用gdb我发现在行**(str+i) = fgetc(aFile);生成了一个seg错误**(str+i) = fgetc(aFile);

Here is the output of $ gdb a.out core along with the values of some variables: 这是$ gdb a.out core的输出以及一些变量的值:

Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x0000000000400bd3 in readFile (aFile=0x994010, str=0x7ffd8b1a9338) at src/morse.c:59
59      **(str + i) = fgetc(aFile);
(gdb) print i
$1 = 1
(gdb) print **(str + 0)
$2 = 65 'A'
(gdb) print *(str + 0)
$3 = 0x994250 "A"
(gdb) print (str + 0)
$4 = (char **) 0x7ffd8b1a9338
(gdb) print **(str + 1)
Cannot access memory at address 0x0
(gdb) print *(str + 1)
$5 = 0x0
(gdb) print (str + 1)
$6 = (char **) 0x7ffd8b1a9340

Here is the relevant function: 这是相关功能:

int readFile(FILE *aFile, char **str) // puts a file into a string
{
  int fileSize = 0;
  int i = 0;

  // count the length of the file string
  while(fgetc(aFile) != EOF)
    fileSize++;

  // malloc enough space for the string
  *str = (char *)malloc(sizeof(char) * (fileSize + 1 + 1)); // one for null, one for extra space
  if(!(*(str)))
    printf("ERROR: *str == NULL\n");

  // rewind() to the start of the file
  rewind(aFile);

  // put the file into a string
  for(i = 0; i < fileSize; i++)
    **(str + i) = fgetc(aFile);
  **(str + i - 1) = '\0';

  return 0;
}

Why is it that it's possible to access the beginning of the memory (for lack of a better term) but not more? 为什么可以访问内存的开头(因为没有更好的术语)而不是更多? What is the difference between the seemingly contiguous memory at the ** level and the non-contiguous memory at the * level? **级看似连续的内存和*级别的非连续内存有什么区别?

The rest of the code can be seen on GitHub here . 其余的代码可以在GitHub上看到

It should be *(*str+i) instead of **(str+i) . 它应该是*(*str+i)而不是**(str+i) You have allocated memory to *str pointer. 您已将内存分配给*str指针。

In your code, the pointer to the allocated memory block is *str , not **str . 在您的代码中,指向已分配内存块的指针是*str ,而不是**str Therefore, you want to set values of *((*str) + i) , or, equivalently, (*str)[i] . 因此,您要设置*((*str) + i) ,或等效地设置(*str)[i]

should be *(*str+i) , and you should be shot by the style police for using anything like this coding style in the first place. 应该是*(* str + i),并且你应该被风格警察开除,因为它首先使用这种编码风格。

use something mode like 使用某种模式

char *myStr = *str = (char *) malloc .... char * myStr = * str =(char *)malloc ....

*myStr[i] = byte * myStr [i] =字节

.... ....

@ B. Shankar already gave you an answer, but I think I should point out some imporvements. @ B. Shankar已经给你一个答案,但我想我应该指出一些改进。

Unless you need to do it with pointer arithmetic, there is absolutely no need for all the loops in your code, you could just do it like this 除非您需要使用指针算法,否则您的代码中绝对不需要所有循环,您可以这样做

int readFile(FILE *aFile, char **str) // puts a file into a string
{
  int fileSize;

  // count the length of the file string
  fseek(aFile, 0L, SEEK_END);
  fileSize = ftell(aFile);
  fseek(aFile, 0L, SEEK_SET);

  // malloc enough space for the string
  *str = malloc(fileSize + 1 + 1)); // one for null, one for extra space
  if (*str == NULL)
      return -1;    
  if (fread(*str, 1, fileSize, aFile) != fileSize)
      return -1;
  return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM