C realloc不会更改字符数组的外观大小 [英] C realloc not changing apparent size of character array

查看:165
本文介绍了C realloc不会更改字符数组的外观大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行下面的代码时,得到给定的输出.

When I run the below code, I get the given output.

#include <stdio.h> /* printf */
#include <stdlib.h> /* malloc, realloc */

int main()
{
    char* characters = (char *) malloc(10 * sizeof(char));
    printf("Before: characters at %p, size=%lu\n", (void *) characters, sizeof(characters) / sizeof(characters[0]));
    char* temp = (char *) realloc(characters, 100);
    if (temp)
    {
        printf("After realloc, characters size = %lu, temp size = %lu\n", sizeof(characters) / sizeof(characters[0]), sizeof(temp) / sizeof(temp[0]));
        printf("After realloc, nums at %p, temp at %p\n", (void *) characters, (void *) temp);
        //characters = temp;
        free(temp);
    }
    free(characters);
}

/* Output:
Before: characters at 0x107b00900, size=8
After realloc, characters size = 8, temp size = 8
After realloc, nums at 0x107b00900, temp at 0x107b00910
test(3556) malloc: *** error for object 0x107b00900: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
*/

我正在尝试找出故障原因. 我认为malloc会为十个连续字符留出空间,并给我一个指向该数组第一个元素的指针,并将该指针存储在characters中.然后,我打印characters的大小,我希望是10,但是我得到8.这是第一件事.然后,我要求realloc在堆中找到一个新的位置,该位置有100个连续字符的空间,并向我返回指向第一个位置的指针.我把那个指针放在了临时.当我打印temp的大小时,即使我希望是100,我仍然得到8(再次).当我打印指向characterstemp的指针时,我在堆中得到了两个不同的位置.通常,然后我将重新分配characters指针以指向temp所指向的内容.然后我尝试释放指针,它告诉我不能释放0x107b00900,确切的位置是characters,因为此时未对对象进行malloc;但是,我为characters做了malloc空格.为什么会这样呢?我是否误解了mallocreallocsizeof或其他功能?谢谢您的帮助.

I'm trying to figure out what is malfunctioning. I think that malloc sets aside space for ten consecutive characters and gives me a pointer to the first element of that array, and I store that pointer in characters. I then print the size of characters, and I expect 10, but I get 8. That's the first weird thing. Then, I ask realloc to find a new spot in the heap, which has space for 100 consecutive characters and return to me a pointer to the first spot. I put that pointer into temp. When I print temp's size, I get 8 (again), even though I expect 100. When I print the pointers to characters and temp, I get two different locations in the heap. Usually, I would then reassign the characters pointer to point to whatever temp is pointing to. Then I tried to free my pointers, and it told me that it couldn't free 0x107b00900, the exact location characters is, because the object at that point wasn't malloced; however, I did malloc space for characters. Why is this happening? Am I misunderstanding the functionality of malloc, realloc, sizeof, or something else? Thank you for your help.

推荐答案

您不能在指针上使用sizeof来获取分配的内存量.

You can't use sizeof on your pointer to get the amount of memory allocated.

char* characters = (char *) malloc(10 * sizeof(char));

characters变量不知道它指向多少空间.跟踪它是您的工作.

The characters variable does not know how much space it is pointing to. It's your job to keep track of it.

char* temp = (char *) realloc(characters, 100);

realloc可以移动内存块-这就是这里发生的情况.这样做时,它将原来由字符指向的内存标记为未分配.因此,当您在最后一行上释放字符时,会得到一个错误,因为您正在释放系统已标记为未分配的内存.

realloc can move the memory block - which is what happens here. When it does that it marks the memory originally pointed to by characters as unallocated. Thus, when you free characters on the last line, you get an error because you are freeing memory that the system has marked as unallocated.

这篇关于C realloc不会更改字符数组的外观大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆