c - 结构内的指针重新分配正常,但指向结构内指针的指针无法重新分配,并出现无效指针错误

标签 c memory memory-management realloc

在处理需要频繁分配内存的程序时,我遇到了无法解释的行为。我已经实现了一个解决方法,但我很好奇为什么我以前的实现不起作用。情况是这样的:

指针的内存重新分配有效

这可能不是最佳实践(如果是,请告诉我)但我记得如果传入的指针为 NULL,realloc 可以分配新内存。下面是一个示例,我将文件数据读入临时缓冲区,然后为 *data 和 memcopy 内容分配适当的大小

我有这样的文件结构

typedef struct _my_file {
       int size;
       char *data;
}

内存重新分配和复制代码如下:

// cycle through decompressed file until end is reached
while ((read_size = gzread(fh, buf, sizeof(buf))) != 0 && read_size != -1) {
        // allocate/reallocate memory to fit newly read buffer
        if ((tmp_data = realloc(file->data, sizeof(char *)*(file->size+read_size))) == (char *)NULL) {
                printf("Memory reallocation error for requested size %d.\n", file->size+read_size);
                // if memory was previous allocated but realloc failed this time, free memory!
                if (file->size > 0)
                        free(file->data);
                return FH_REALLOC_ERROR;
        }
        // update pointer to potentially new address (man realloc)
        file->data = tmp_data;
        // copy data from temporary buffer
        memcpy(file->data + file->size, buf, read_size);

        // update total read file size
        file->size += read_size;
}

指针到指针的内存重新分配失败

然而,这就是我感到困惑的地方。使用 NULL 指针的重新分配将分配新内存的相同想法,我解析一串参数,并为每个参数分配一个指向指针的指针,然后分配一个由该指针指向指针的指针。也许代码更容易解​​释:

这是结构:

typedef struct _arguments {
        unsigned short int options;    // options bitmap
        char **regexes;                // array of regexes
        unsigned int nregexes;         // number of regexes
        char *logmatch;                // log file match pattern
        unsigned int limit;            // log match limit
        char *argv0;                   // executable name
} arguments;

以及内存分配代码:

int i = 0;
int len;
char **tmp;
while (strcmp(argv[i+regindex], "-logs") != 0) {
        len = strlen(argv[i+regindex]);

        if((tmp = realloc(args->regexes, sizeof(char **)*(i+1))) == (char **)NULL) {
                printf("Cannot allocate memory for regex patterns array.\n");
                return -1;
        }
        args->regexes = tmp;
        tmp = NULL;

        if((args->regexes[i] = (char *)malloc(sizeof(char *)*(len+1))) == (char *)NULL) {
                printf("Cannot allocate memory for regex pattern.\n");
                return -1;
        }

        strcpy(args->regexes[i], argv[i+regindex]);

        i++;
}

当我编译并运行它时,出现运行时错误“realloc: invalid pointer”

我一定是遗漏了一些明显的东西,但是在尝试调试和在线搜索解决方案 5 小时后没有完成很多工作之后,我只运行了两个循环,一个计算参数的数量和 mallocs 足够的空间,第二个循环为参数分配空间并对其进行 strcpys。

非常感谢对此行为的任何解释!我真的很想知道为什么。

最佳答案

第一个片段:

// cycle through decompressed file until end is reached
while (1) {
        char **tmp_data;
        read_size = gzread(fh, buf, sizeof buf);
        if (read_size <= 0) break;

        // allocate/reallocate memory to fit newly read buffer
        tmp_data = realloc(file->data, (file->size+read_size) * sizeof *tmp_data );
        if ( !tmp_data ) {
                printf("Memory reallocation error for requested size %d.\n"
                      , file->size+read_size);

                if (file->data) {
                        free(file->data)
                        file->data = NULL;
                        file->size = 0;
                }
                return FH_REALLOC_ERROR;
        }

        file->data = tmp_data;
        // copy data from temporary buffer
        memcpy(file->data + file->size, buf, read_size);

        // update total read file size
        file->size += read_size;
}

第二个片段:

unsigned i; // BTW this variable is already present as args->nregexes;

for(i =0; strcmp(argv[i+regindex], "-logs"); i++) {
        char **tmp;

        tmp = realloc(args->regexes, (i+1) * sizeof *tmp ); 
        if (!tmp) {
                printf("Cannot allocate memory for regex patterns array.\n");
                return -1;
        }
        args->regexes = tmp;

        args->regexes[i] = strdup( argv[i+regindex] ); 
        if ( !args->regexes[i] ) {
                printf("Cannot allocate memory for regex pattern.\n");
                return -1;
        }
...
return 0;
}

一些注意事项:

  • 语法 ptr = malloc ( CNT * sizeof *ptr);sizeof(type) 变体更健壮。
  • strdup() 与您的 malloc+strcpy() 完全一样
  • for(;;) 循环比在循环体末尾带有松散 i++; 的 while() 循环更不容易出错。 (它还明确表示从未检查过循环条件)
  • 对我来说 if ( !ptr ) {}if (ptr != NULL) {} 更容易阅读
  • 不需要强制转换,有时甚至不需要。

关于c - 结构内的指针重新分配正常,但指向结构内指针的指针无法重新分配,并出现无效指针错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14861929/

相关文章:

java - for 循环中的内存管理和速度

c# - 如何在 Windows 窗体 C# 中将大量数据保存在内存中

memory-leaks - 我有很多未使用的内存分配给 .net

memory-management - 你什么时候不想使用垃圾收集?

memory-management -/proc/[pid]/status :RssAnon and/proc/[pid]/smaps_rollup:Anonymous? 不一致是什么原因

c - 在 linux 中获取导出函数的名称和地址

c - C 中的 Windows 虚拟麦克风

c - sscanf的返回值

两个IP的比较

java - 使用 Java 存储多个对象