简体   繁体   English

无符号int的内存分配(calloc,malloc)

[英]Memory allocation (calloc, malloc) for unsigned int

For my C application I tried to initialize memory. 对于我的C应用程序,我尝试初始化内存。 I am aware of the slower calloc, but fortunatelly there is no need to track performance. 我知道较慢的calloc,但幸运的是,无需跟踪性能。

I need memory space for just one unsigned int element (up to 65535). 我只需要一个无符号int元素的内存空间(最多65535)。

This is the part of my code that doesn't work: 这是我的代码无效的部分:

//Declaration
unsigned int part1;

//Allocation
part1 = (unsigned int) calloc (1,sizeof(unsigned int));

That throws the compiler warning: 这引发了编译器警告:

warning: cast from pointer to integer of different size [-Wpointer-to-int-cast] 警告:从指针转换为不同大小的整数[-Wpointer-to-int-cast]

Why does the above code doesn't work, where... 为什么上面的代码不起作用,在哪里...

unsigned long size;
size =(unsigned long) calloc (1,sizeof(unsigned long));

...works great? ...效果很好?

Thank you! 谢谢!

calloc returns void* so you should use it like calloc返回void*因此您应该像这样使用它

unsigned int* part1 = calloc (1,sizeof(*part1));

then assign it like 然后像分配它

*part1 = 42;

If you have allocated space for several elements 如果您已为几个元素分配空间

part1[0] = 42; // valid indices are [0..nmemb-1]

may be clearer. 可能更清楚。

Note that you also have to free this memory later 请注意,您稍后还必须free此内存

free(part1);

Alternatively, if you only need a single element, just declare it on the stack 另外,如果只需要一个元素,只需在堆栈上声明它即可

unsigned int part1 = 42;

Regarding why casting a point to unsigned long doesn't generate a warning, sizeof(void*)==sizeof(unsigned long) on your platform. 关于为什么将点强制转换为unsigned long不会生成警告,请在您的平台上使用sizeof(void*)==sizeof(unsigned long) Your code would not be portable if you relied on this. 如果您依赖此代码,则该代码将不可移植。 More importantly, if you use a pointer to store a single integer, you'd leak your newly allocated memory and be unable to ever store more than one element of an array. 更重要的是,如果使用指针存储单个整数,则会泄漏新分配的内存,并且无法存储一个数组中的多个元素。

Use code below. 使用下面的代码。 Calloc() will return void* So you will have to convert it in the SomeType* Calloc()将返回void *,因此您必须将其转换为SomeType *

unsigned int* part1; unsigned int * part1;

//Allocation
part1 = (unsigned int*) calloc (1,sizeof(unsigned int));

you have to understand these types of memory allocation to avoid doing these errors : 您必须了解以下类型的内存分配,以避免发生以下错误:

Static memory allocation: 静态内存分配:

unsigned int part1; unsigned int part1;

The size is fixed. 大小是固定的。 It needs to be known at compile time. 需要在编译时知道它。 Freeing the memory is done on scope exit directly. 释放内存直接在范围出口处完成。 The variable is allocated on the stack. 该变量分配在堆栈上。 indeed, this type of memory allocation is done at compile time, its lifetime is entire runtime of program. 实际上,这种类型的内存分配是在编译时完成的,其生存期是程序的整个运行时。 the advantage of using this type of allocation is efficient execution time. 使用这种类型的分配的优点是有效的执行时间。 but if we declare more static data space than we need we waste space, this is the disadvantage of this type. 但是,如果声明的静态数据空间多于所需的空间,那么这将是这种类型的缺点。

Dynamic memory allocation: 动态内存分配:

unsigned int* part1 = calloc( n, sizeof(unsigned int) ); unsigned int * part1 = calloc(n,sizeof(unsigned int));

The size can vary, you can find the value at runtime. 大小可能有所不同,您可以在运行时找到该值。 You are responsible for freeing the memory with free() predefined C function. 您负责使用free()预定义的C函数释放内存。 The variable is allocated on the heap. 该变量分配在堆上。

you can see more details in web site : http://www.cs.virginia.edu/~son/cs414.f05/lec11.slides.pdf 您可以在网站上查看更多详细信息: http : //www.cs.virginia.edu/~son/cs414.f05/lec11.slides.pdf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM