Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Realloc()在内存可用时返回NULL-C 操作系统:Windows Vista(x86) 编译器:代码::块_C_Directory_Realloc - Fatal编程技术网

Realloc()在内存可用时返回NULL-C 操作系统:Windows Vista(x86) 编译器:代码::块

Realloc()在内存可用时返回NULL-C 操作系统:Windows Vista(x86) 编译器:代码::块,c,directory,realloc,C,Directory,Realloc,我目前正在编写一个程序,打开一个指定的目录并读取其中的内容。而不是使用printf()在找到文件名后立即显示它们。我将它们保存在内存中,稍后再显示。我使用下面的if语句触发内存重新分配。我还包括了相关变量的声明 //Represents what the new index will be after the current file name is added //to 'stack.ptr' #define NEW_INDEX (stack.index+(strlen(ptr_dirent-

我目前正在编写一个程序,打开一个指定的目录并读取其中的内容。而不是使用printf()在找到文件名后立即显示它们。我将它们保存在内存中,稍后再显示。我使用下面的if语句触发内存重新分配。我还包括了相关变量的声明

//Represents what the new index will be after the current file name is added
//to 'stack.ptr'
#define NEW_INDEX (stack.index+(strlen(ptr_dirent->d_name)))

//Contains the pointer that points to the directory's contents 'stack.ptr',
//the size of 'stack.ptr' which is 'stack.size', and the current index
//'stack.index'
struct stack
{
  int index;
  char *ptr;
  int size;
};struct stack stack;

//Sets the index to 0 and allocates 256 bytes of memory for 'stack.ptr'
stack.index = 0; stack.size = 256;
stack.ptr = malloc(sizeof(char)*stack.size);

if(NEW_INDEX > stack.size)
{
  char *temp; stack.size *= 2;
  temp = realloc(stack.ptr, sizeof(char)*stack.size);
  if (temp == NULL)
  {
    printf("ERROR: %i Bytes of memory could not be allocated.\n", stack.size);
    free(stack.ptr); closedir(dirp); return '\000';
  }
  else {stack.ptr = temp;}
}
在我将“stack.size”(即数组大小)的初始值设置为2而不是256(这样程序就必须重新分配内存)之前,程序工作得非常好。我的程序崩溃了,因为realloc()返回NULL,但我有足够的可用内存。我知道realloc()确实工作过几次,因为
“stack.size”崩溃时为16(“stack.size”在每次重新分配内存时都会翻倍)。我尝试将“stack.size”设置为两个不同的值,发现将“stack.size”设置为1或2会导致崩溃,并且当“stack.size”达到16时总是会发生这种情况。谁能给我解释一下吗?我担心,即使我将“stack.size”设置为256,如果目录足够大,足以触发内存重新分配,我的程序也可能崩溃。同样在一个无关的注释中,我读到了openddir(“.”);将打开当前目录,我发现它确实打开了,但由于某些原因,当前目录中的所有文件都不在“stack.ptr”和a中。和。。当我将“stack.ptr”的内容输出到stdout时显示。

您没有向我们显示包含错误的行。它可能看起来像:

strcpy(stack.ptr+stack.index, ptr_dirent->d_Name);

这里的问题是
strcpy()
复制
strlen()+1
字节。您正在将终止NUL char写入已分配数组的末尾。

旁注,您可以稍微清理一下代码,因为
sizeof(char)
将始终是1有人会说:需要一个最小的可编译示例,以便我们能够尝试它。。。显然,如果temp为NULL,您的代码就不会崩溃(而且您是故意抓住这个案例的,不是吗?)。不管怎样,调试器可能会有所帮助。谢谢你的建议,迈克,但我更喜欢写出字符@ShinTakezou我不想发布不必要的代码,因为我可以合理地确定问题在上面的代码片段中。你是对的,我的程序在技术上没有崩溃。但是,如果realloc()无法在其可用空间足够大的情况下重新分配内存,则其仍然是一个问题。问题可能就在那里,但我们不会很容易发现:我们需要想象一下。此外,您也没有展示如何实际填充
stack.ptr
,如何递增
stack.index
,等等。可能您的代码中有一个逻辑错误,影响了对
stack.ptr
的管理。当你问这样的问题时,你需要知道你在做什么。谢谢你的回答,是的,这可能会导致问题,但我使用(I=stack.index,j=0;I d_name[j];}将文件名复制到stack.ptr,因为新的_index使用strlen()查找dirent->d_name的长度。终止NULL应该没有问题。但是我不是专家,如果我错了,请纠正我。