确保内存分配的大小合适
Ensure the allocation of memory is of the right size
我似乎有一个问题我无法理解,实验室助手说“你的内存分配不会分配正确的大小,你需要使用类型本身的大小而不是变量。”。
我试过像这样使用 sizeof (struct object)
printf("%d", sizeof(struct object));
来查看尺寸和 returns 36
。在分配中,大小与 struct object
相同,所以我有点迷失为什么它会分配错误的大小。当我 运行 它时,分配似乎对我来说是正确的,并且在调试器中它没有显示任何错误,所以如果有人能看一下,我将非常感激。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NameLength 20
#define UnitLenght 10
struct object
{
char name[NameLength];
float amount;
char unit[UnitLenght];
};
struct inventory
{
struct object *add;
int nrOfobject;
};
void allocateMemory(struct inventory *allItem);
int main(void)
{
struct inventory shopping = {NULL, 0};
allocateMemory(&shopping);
return 0;
}
void allocateMemory(struct inventory *allItem)
{
struct object *tempurary;
if (allItem->nrOfobject == 0)
tempurary = (struct object *)calloc(1, sizeof(*tempurary));
else
tempurary = (struct object *)realloc(allItem->add, sizeof(*tempurary)*(allItem->nrOfobject +1));
allItem->add = tempurary;
}
void allocateMemory(struct inventory *allItem)
{
struct object *tempurary;
if (allItem->nrOfobject == 0)
tempurary = (struct object *)calloc(1, sizeof(*tempurary));
else
tempurary = (struct object *)realloc(allItem->add, sizeof(*tempurary)*(allItem->nrOfobject +1));
allItem->add = tempurary;
}
大小看起来是正确的,尽管我会删除不必要的转换和第一个元素的清除(因为我们不会将后续元素清零)。另外,在覆盖指针之前检查 realloc()
的结果(否则我们可以留下内存泄漏):
int allocateMemory(struct inventory *allItem)
{
struct object *temporary;
if (allItem->nrOfobject == 0) {
temporary = malloc(sizeof *temporary);
} else {
temporary = realloc(allItem->add, (sizeof *temporary)*(allItem->nrOfobject + 1));
}
if (!temporary) {
return 0;
}
allItem->add = temporary;
return ++allItem->nrOfobject;
}
我似乎有一个问题我无法理解,实验室助手说“你的内存分配不会分配正确的大小,你需要使用类型本身的大小而不是变量。”。
我试过像这样使用 sizeof (struct object)
printf("%d", sizeof(struct object));
来查看尺寸和 returns 36
。在分配中,大小与 struct object
相同,所以我有点迷失为什么它会分配错误的大小。当我 运行 它时,分配似乎对我来说是正确的,并且在调试器中它没有显示任何错误,所以如果有人能看一下,我将非常感激。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NameLength 20
#define UnitLenght 10
struct object
{
char name[NameLength];
float amount;
char unit[UnitLenght];
};
struct inventory
{
struct object *add;
int nrOfobject;
};
void allocateMemory(struct inventory *allItem);
int main(void)
{
struct inventory shopping = {NULL, 0};
allocateMemory(&shopping);
return 0;
}
void allocateMemory(struct inventory *allItem)
{
struct object *tempurary;
if (allItem->nrOfobject == 0)
tempurary = (struct object *)calloc(1, sizeof(*tempurary));
else
tempurary = (struct object *)realloc(allItem->add, sizeof(*tempurary)*(allItem->nrOfobject +1));
allItem->add = tempurary;
}
void allocateMemory(struct inventory *allItem) { struct object *tempurary; if (allItem->nrOfobject == 0) tempurary = (struct object *)calloc(1, sizeof(*tempurary)); else tempurary = (struct object *)realloc(allItem->add, sizeof(*tempurary)*(allItem->nrOfobject +1)); allItem->add = tempurary; }
大小看起来是正确的,尽管我会删除不必要的转换和第一个元素的清除(因为我们不会将后续元素清零)。另外,在覆盖指针之前检查 realloc()
的结果(否则我们可以留下内存泄漏):
int allocateMemory(struct inventory *allItem)
{
struct object *temporary;
if (allItem->nrOfobject == 0) {
temporary = malloc(sizeof *temporary);
} else {
temporary = realloc(allItem->add, (sizeof *temporary)*(allItem->nrOfobject + 1));
}
if (!temporary) {
return 0;
}
allItem->add = temporary;
return ++allItem->nrOfobject;
}