malloc,free:错误无效下一个大小(快)

时间:2014-06-18 11:52:30

标签: c memory-management free

我目前正在学习c然后遇到free()和malloc()。 所以有一个小程序可以玩malloc和free。

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

int main(int argc, char *argv[]) {
    char *char_ptr; // A char pointer
    int *int_ptr; // An integer pointer
    char *char_ptr1;
    int mem_size;

    if (argc < 2)// If there aren't command-line arguments,
        mem_size = 50; // use 50 as the default value.
    else
        mem_size = atoi(argv[1]);

    printf("\t[+] allocating %d bytes of memory on the heap for char_ptr\n", mem_size);
    char_ptr = (char *) malloc(mem_size); // Allocating heap memory

    if(char_ptr == NULL) { // Error checking, in case malloc() fails
        fprintf(stderr, "Error: could not allocate heap memory.\n");
    exit(-1);
    }

    strcpy(char_ptr, "This is memory is located on the heap.");
    printf("char_ptr (%p) --> '%s'\n", char_ptr, char_ptr);

    printf("\t[+] allocating 12 bytes of memory on the heap for int_ptr\n");
    int_ptr = (int *) malloc(12); // Allocated heap memory again

    if(int_ptr == NULL) { // Error checking, in case malloc() fails
        fprintf(stderr, "Error: could not allocate heap memory.\n");
        exit(-1);
    }

    *int_ptr = 31337; // Put the value of 31337 where int_ptr is pointing.
    printf("int_ptr (%p) --> %d\n", int_ptr, *int_ptr);


    printf("\t[-] freeing char_ptr's heap memory...\n");
    free(char_ptr); // Freeing heap memory

    printf("\t[+] allocating another 15 bytes for char_ptr\n");
    char_ptr1 = (char *) malloc(15); // Allocating more heap memory

    if(char_ptr == NULL) { // Error checking, in case malloc() fails
        fprintf(stderr, "Error: could not allocate heap memory.\n");
        exit(-1);
    }
    else printf("succeded in allocation, [pointer] %p\n", char_ptr);


    strcpy(char_ptr1, "new memory");
    printf("char_ptr (%p) --> '%s'\n", char_ptr1, char_ptr1);

    printf("\t[-] freeing int_ptr's heap memory...\n");
    free(int_ptr); // Freeing heap memory
    printf("\t[-] freeing char_ptr's heap memory...\n");
    free(char_ptr1); // Freeing the other block of heap memory
}

所以,如果我为char_ptr分配了一个相当大的memoryspace,它运作良好。

但令我困惑的是,对于小值,它不起作用。 我得到的价值: 错误:“free()无效下一个大小(快)”。

从我在其他帖子中读到的内容中,当想要释放尚未分配的内存时会发生这种情况,这在我的示例中并非如此。

那么以什么方式分配char_ptr与char_ptr1的释放有关?

3 个答案:

答案 0 :(得分:2)

如果只分配一个小缓冲区,则代码如

strcpy(char_ptr, "This is memory is located on the heap.");

可能会溢出分配的区域(您需要为此字符串分配至少大小为39字节的缓冲区)。特别是,您可以覆盖malloc自身使用的部分内部数据结构来跟踪分配。确保malloc'ed空间足够大,以容纳您放入其中的所有数据。

int_ptr可能会出现类似问题。你的12的大小是奇数,但应该是安全的(在“普通”机器上)。但更好的是:

int_ptr = malloc(sizeof(int));

答案 1 :(得分:0)

如果您编写的数据多于分配空间的数据,则会出现未定义的行为。

答案 2 :(得分:0)

有点背景。

通常malloc接收一个参数,告诉他你想分配多少空间,并返回指向这个内存块的指针。

type * var = malloc(sizeof(type) * length);

type可以是您希望在堆上分配的任何类型,intchar等。

让我们来看看你的代码。这一行例如:

char_ptr = (char *) malloc(mem_size); // Allocating heap memory

在这里,您希望使用lenght = mem_size分配一个字符数组。此代码有效,因为sizeof(char)为1.它需要1个字节来存储字符。

然而,更好的写作方式是:

char_ptr = malloc(sizeof(char) * mem_size); // Allocating heap memory

这种感觉更好,并且你坚持使用相同的格式,尽管sizeof(char)在某种程度上是多余的。另外,转换malloc操作的结果。将此提升为您指定的类型。请参阅此帖子here

回到你的问题:

  

那么以什么方式分配char_ptr与char_ptr1的释放有关?

在这种情况下不是这样。我觉得你混淆了保护指针,是char_ptr而不是刚刚分配的char_ptr1

     |
     v

if(char_ptr1 == NULL) { // Error checking, in case malloc() fails
    fprintf(stderr, "Error: could not allocate heap memory.\n");
    exit(-1);
}