问题描述
GCC 4.5.1 C89
我写了这个源$ C $ C为我更好地理解malloc和释放calloc的。
我明白了,只是有几个问题。
开发=的malloc(数量* * sizeof的设备);
等于该释放calloc。我不关心清理内存。
开发=释放calloc(数字,* sizeof的设备);
什么是准确,比起在一个while循环做这5次:
开发=的malloc(sizeof的*的设备);
我想的第一个和第二个是创建一个指针5结构设备。第三是建立一个单一的指向struct device的?
我的程序说明了这3种不同的方法编译和的valgrind --leak检查=全跑了。
任何意见非常感谢。
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;结构设备{
#定义MAX_NAME_SIZE 80
为size_t ID;
焦炭名[MAX_NAME_SIZE]
};结构设备* create_device(结构设备* DEV);
无效destroy_device(结构设备* DEV);INT主要(无效)
{
为size_t num_devices = 5;
为size_t I = 0;
结构设备*设备= NULL;
结构设备* dev_malloc = NULL;
结构设备* dev_calloc = NULL; 对于(i = 0; I< num_devices;我++){
设备= create_device(设备);
/ *指定值* /
于设备和SEQ ID = 1 + 1;
sprintf的(设备 - >的名字,设备%俎,与于设备GT; ID);
/ *打印值* /
的printf(ID ----- [%祖] \\ n,与于设备GT; ID);
的printf(名称--- [%S] \\ n,与于设备GT;名);
/ *测试免费* /
destroy_device(设备);
} 的printf(\\ n);
dev_malloc =的malloc(num_devices * sizeof的* dev_malloc);
对于(i = 0; I< num_devices;我++){
/ *指定值* /
dev_malloc-和SEQ ID = 1 + 1;
sprintf的(dev_malloc->的名字,dev_malloc%俎,dev_malloc-> ID);
/ *打印值* /
的printf(ID ----- [%祖] \\ n,dev_malloc-> ID);
的printf(名称--- [%S] \\ n,dev_malloc->名);
}
/ *测试免费* /
destroy_device(dev_malloc); 的printf(\\ n);
dev_calloc =释放calloc(num_devices,sizeof的* dev_calloc);
对于(i = 0; I< num_devices;我++){
/ *指定值* /
dev_calloc-和SEQ ID = 1 + 1;
sprintf的(dev_calloc->的名字,dev_calloc%俎,dev_calloc-> ID);
/ *打印值* /
的printf(ID ----- [%祖] \\ n,dev_calloc-> ID);
的printf(名称--- [%S] \\ n,dev_calloc->名);
}
/ *测试免费* /
destroy_device(dev_calloc); 返回0;
}结构设备* create_device(结构设备* DEV)
{
/ *不检查内存错误 - 只是简单的测试* /
返回开发=的malloc(sizeof的* DEV);
}无效destroy_device(结构设备* DEV)
{
如果(DEV!= NULL){
免费(DEV);
}
}
编辑为清楚起见
- 我想第一个和第二个是创建一个指向5结构设备。第三是建立一个单一的指向struct device的?
第一个的malloc(编号* sizeof的(*设备))
将分配足够的内存来存储数量
的设备
秒。正如其他人所说,你可以把这个块如设备
的数组。你得到的指针将指向块的开始。
INT数= 5;
设备* PTR =的malloc(*号的sizeof(* PTR));
/* 东东 */
免费(PTR);
这是使用第二个释放calloc
做同样的事情,同时还初始化内存为0。同样,你可以使用治疗块等的阵列设备
。
INT数= 5;
设备* PTR =释放calloc(数量的sizeof(* PTR));
/* 东东 */
免费(PTR);
第三个,循环5次,将导致5个不同的指针足够大,5个不同的块存储一个设备
每个。这也意味着每5个三分球有是免费
编辑独立。
设备*师生比[5];
对(INT I = 0; I&小于5 ++ⅰ)
{
师生比[I] =的malloc(sizeof的(*师生比由[i]));
}
/* 东东 */
对(INT I = 0; I&小于5 ++ⅰ)
{
免费(师生比[I]);
}
gcc 4.5.1 c89
I have written this source code for my better understanding of malloc and calloc.
I understand, but just have a few questions.
dev = malloc(number * sizeof *devices);
is equal to this calloc. I am not concerned about clearing the memory.
dev = calloc(number, sizeof *devices);
What is that exactly, compared to doing this 5 times in a while loop:
dev = malloc(sizeof *devices);
I guess the first one and the second is creating a pointer to 5 struct device. And the third is creating a single pointer to a struct device?
My program illustrates this 3 different methods compiled and ran with valgrind --leak-check=full.
Many thanks for any advice.
#include <stdio.h>
#include <stdlib.h>
struct Devices {
#define MAX_NAME_SIZE 80
size_t id;
char name[MAX_NAME_SIZE];
};
struct Devices* create_device(struct Devices *dev);
void destroy_device(struct Devices *dev);
int main(void)
{
size_t num_devices = 5;
size_t i = 0;
struct Devices *device = NULL;
struct Devices *dev_malloc = NULL;
struct Devices *dev_calloc = NULL;
for(i = 0; i < num_devices; i++) {
device = create_device(device);
/* Assign values */
device->id = i + 1;
sprintf(device->name, "Device%zu", device->id);
/* Print values */
printf("ID ----- [ %zu ]\n", device->id);
printf("Name --- [ %s ]\n", device->name);
/* Test free */
destroy_device(device);
}
printf("\n");
dev_malloc = malloc(num_devices * sizeof *dev_malloc);
for(i = 0; i < num_devices; i++) {
/* Assign values */
dev_malloc->id = i + 1;
sprintf(dev_malloc->name, "dev_malloc%zu", dev_malloc->id);
/* Print values */
printf("ID ----- [ %zu ]\n", dev_malloc->id);
printf("Name --- [ %s ]\n", dev_malloc->name);
}
/* Test free */
destroy_device(dev_malloc);
printf("\n");
dev_calloc = calloc(num_devices, sizeof *dev_calloc);
for(i = 0; i < num_devices; i++) {
/* Assign values */
dev_calloc->id = i + 1;
sprintf(dev_calloc->name, "dev_calloc%zu", dev_calloc->id);
/* Print values */
printf("ID ----- [ %zu ]\n", dev_calloc->id);
printf("Name --- [ %s ]\n", dev_calloc->name);
}
/* Test free */
destroy_device(dev_calloc);
return 0;
}
struct Devices* create_device(struct Devices *dev)
{
/* Not checking for memory error - just simple test */
return dev = malloc(sizeof *dev);
}
void destroy_device(struct Devices *dev)
{
if(dev != NULL) {
free(dev);
}
}
edited for clarity
- I guess the first one and the second is creating a pointer to 5 struct device. And the third is creating a single pointer to a struct device?
The first one malloc(number * sizeof(*devices))
would allocate enough memory to store number
of Device
s. As others have mentioned, you can treat this block like an array of Device
. The pointer you get back will point to the beginning of the block.
int number = 5;
Device *ptr = malloc(number * sizeof(*ptr));
/* stuff */
free(ptr);
The second one that uses calloc
does the same thing, while also initializing the memory to 0. Again, you can use treat the block like an array of Device
.
int number = 5;
Device *ptr = calloc(number, sizeof(*ptr));
/* stuff */
free(ptr);
The third one, looping 5 times, would result in 5 different pointers to 5 different blocks large enough to store one Device
each. This also means each of the 5 pointers has to be free
'ed individually.
Device *ptrs[5];
for(int i = 0; i < 5; ++i)
{
ptrs[i] = malloc(sizeof(*ptrs[i]));
}
/* stuff */
for(int i = 0; i < 5; ++i)
{
free(ptrs[i]);
}
这篇关于差异malloc和释放calloc的用途的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!