"下一个尺寸无效"重新分配内存时出现异常

时间:2016-03-06 14:27:53

标签: c memory memory-management realloc

P.S。: 我几乎所有的问题都是"无效的下一个尺码"但是他们没有帮助我,因为我没有另外一段代码可以使用malloc或realloc,所以这是排除在外的。另外,我没有超出内存空间的限制,所以我也很好。

我有以下代码给我 - *** Error in ./ server_issue':realloc():下一个大小无效:0x08249170 ***`。

我无法识别问题,我一次只分配64个字节,所以我没有内存地址空间,还有其他内存分配可能会损坏我的堆内存。

错误说,下一个大小无效但是因为我的上一个日志告诉Reallocating 64 bytes, Aborted (core dumped)所以这意味着我仍然在尝试分配64个字节。那么,为什么会出错?

对于HTML文件,任何超过256个字节的文件。

代码:(重现问题的最低代码)

#include<stdio.h>
#include<stdlib.h>
#include <stdbool.h>
#include <string.h>

typedef char BYTE;
bool load(FILE*, BYTE**, size_t*);

int main(void)
{
    FILE* file = fopen("/home/university/psets/pset6/pset6_working/public/hello.html", "r");
    BYTE* content;
    size_t length;
    load(file, &content, &length);
}

bool load(FILE* file, BYTE** content, size_t* length)
{
    int totalLength = 0;
    int readBytes = 0;
    *content = NULL;

    BYTE* fileContentTemp[64]; // working with 222222
    while ((readBytes = fread(fileContentTemp, 1, 64, file)) > 0)
    {
        printf("Reallocating %d bytes, ", readBytes);
        *content = realloc(*content, readBytes);
        printf("%p\n", *content);
        if(totalLength != 0)
        {
            memcpy(*content + totalLength + 1, fileContentTemp, readBytes);        
        } else{
            memcpy(*content + totalLength, fileContentTemp, readBytes);        
        }
        totalLength = totalLength + readBytes;
    }

    *length = totalLength;

    printf("CC image: %s\n", *content);
    printf("length is %d\n", *length);
    printf("fileContent %p\n", *content);

    return true;
}

输出:

Reallocating 64 bytes, 0x8249170
Reallocating 64 bytes, 0x8249170
*** Error in `./server_issue': realloc(): invalid next size: 0x08249170 ***
Reallocating 64 bytes, Aborted (core dumped)

<小时/> 的更新 即使我在readBytes分配64个字节而不是realloc,我也会得到相同的错误。由于这一行,我收到了错误 - *content = realloc(*content, readBytes);

1 个答案:

答案 0 :(得分:5)

  

另外,我没有超出内存空间的限制,所以我也很好。

过度确定代码是一种使调试更加困难的好方法。怀疑一切是错误的。

您没有分配足够的内存,因为您错误地使用了realloc

p = realloc(p, n)没有 n字节添加到分配中,它会将总分配大小更改为您告诉它的大小。

每次循环都会传递readBytes,这是您要复制的附加字节的数量。所以你的缓冲区从未增长。但是接下来你要写完它的结尾:

memcpy(*content + totalLength, ...

totalLength在整个循环中不断增长。

在这样的循环中调用realloc在性能方面通常是一个坏主意。您应该预先分配足够的总空间,然后在循环中读入它。

获取文件总大小:

size_t get_file_size(FILE *f)
{
    long oldpos;
    size_t result;

    // Save the original file position
    oldpos = ftell(f);

    // Seek to the first byte past the end of the file
    fseek(f, 0, SEEK_END);

    // Get the file position - the number of bytes in the file
    result = ftell(f);

    // Return the file position to where it was
    fseek(f, oldpos, SEEK_SET);

    return result;
}

这是一个测试,以阐述我所说的realloc。请注意,这通常使用malloc_usable_size这是不好的做法。

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

int main(void)
{
    int i;
    char *p = NULL;

    for (i = 0; i < 10; i++) {
        p = realloc(p, 16);
        printf("[%d] size: %zd\n", i, malloc_usable_size(p));
    }

    return 0;
}

<强>输出:

$ gcc -Wall -Werror -o realloc realloc.c 
$ ./realloc 
[0] size: 24
[1] size: 24
[2] size: 24
[3] size: 24
[4] size: 24
[5] size: 24
[6] size: 24
[7] size: 24
[8] size: 24
[9] size: 24

请注意malloc_usable_size()返回相同的值,即使在多次调用realloc后传递相同的大小也是如此。此外,即使我们分配了16个字节,它也会返回24,这是分配器实现细节的结果。

来自malloc_usable_size的手册页:

  

注意

     

由于对齐和最小大小限制,malloc_usable_size()返回的值可能大于请求的分配大小。尽管应用程序可以覆盖多余的字节而不会产生不良影响,但这并不是一种好的编程习惯:分配中的多余字节数取决于底层实现。

     

此功能的主要用途是用于调试和内省。

相关问题