简体   繁体   English

malloc和array有什么区别

[英]What is the difference between malloc and array

I want to ask about malloc and array. 我想问一下malloc和数组。

int *x; int * x;

x = (int*)malloc(sizeof(int)); x =(int *)malloc(sizeof(int));

and

int x[4]; int x [4];

what is the difference between them? 它们之间有什么区别?

The most important difference between int *xp; int *xp;之间最重要的区别int *xp; and int xa[4]; int xa[4]; is sizeof(xp) != sizeof(xa) the size of the declared object. sizeof(xp) != sizeof(xa)声明对象的大小。

You can pass the xa object as int *pparam to a function, but you cannot pass xp as a int aparam[4] to a function, as aparam describes the whole 4 int object and pparam describes a pointer to an object that might have any length. 您可以将xa对象作为int *pparam传递给函数,但是不能将xp作为int aparam[4]传递给函数,因为aparam描述了整个4 int对象,而pparam描述了指向可能具有任何对象的对象的指针长度。

Also xa will be reserved in the data area of the linked program, while the pointer malloc(sizeof(int)*4) will be allocated by a system call at runtime and on the heap. xa还将保留在链接程序的数据区域中,而指针malloc(sizeof(int)*4)将在运行时和堆上由系统调用分配。 Check the vast address difference in a debugger! 在调试器中检查巨大的地址差异!

Well, there are multiple differences. 好吧,这里有多个差异。

This allocates a buffer of one int on the heap... 这会在堆上分配一个int的缓冲区。

int *x;
x = (int*)malloc(sizeof(int));

And this allocates an array of four int s either on the stack or in global memory, or perhaps declares it as a member of a struct or class, if it appears within the definition of a struct or class... 并且这会在堆栈或全局内存中分配由四个int的数组,或者如果它出现在结构或类的定义中,则可能将其声明为结构或类的成员...

int x[4];

Other than the location of the allocations, the first allocated space for one int and the second allocated space for four int s. 除了分配的位置,一个int的第一个已分配空间和四个int的第二个已分配空间。 But assuming you meant to do this instead... 但是假设您打算这样做...

int *x;
x = (int*)malloc(sizeof(int) * 4);

...then in that case, both allocations are a block of memory that is four times the size of an int on your platform. ...那么在这种情况下,两个分配都是一块内存,其大小是平台上int大小的四倍。 And from a usage standpoint, you can then use them both in pretty much the same way; 从使用角度来看,您几乎可以以相同的方式使用它们。 x[0] is the first int in either case, and since neither are declared const , you can read or write to either in the same way. 在任何一种情况下, x[0]都是第一个int ,并且由于都未声明为const ,因此您可以用相同的方式读取或写入其中之一。

So now we get to the difference in the allocation characteristics & lifetime of the two different ways of allocating that memory: 因此,现在我们来了解两种不同的分配内存方式的分配特征和生命周期的差异:

In the malloc() 'ed case, memory for that request is allocated on the heap, and its lifetime is however long you want to keep it until you call free() on it. 在使用malloc()的情况下,该请求的内存在堆上分配,但是您希望保留它的寿命很长,直到对其调用free()为止。 In the other case, if you declared it as a local variable inside a method/function, its lifetime is until program execution exits the scope in which it was declared. 在另一种情况下,如果您在方法/函数内部将其声明为局部变量,则其生存期是直到程序执行退出声明它的范围为止。 If you declared it as a global variable, its lifetime is the lifetime of the entire application. 如果将其声明为全局变量,则其生存期就是整个应用程序的生存期。 And if you declared it as a member variable of a struct or class, then its lifetime is that of its enclosing struct/class, whatever that may be. 而且,如果您将其声明为struct或class的成员变量,那么它的生存期就是其封闭的struct / class的生存期,无论如何。

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

相关问题 数组和malloc之间的区别 - Difference between array and malloc 数组类型和用 malloc 分配的数组的区别 - Difference between array type and array allocated with malloc malloc(sizeof(int))和malloc(sizeof(int *))有什么区别 - What is the difference between malloc(sizeof(int)) and malloc(sizeof(int*)) 使用malloc和数组初始值设定项之间的区别? - Difference between using malloc and initializer for array? 创建数组“正常”和malloc之间的区别 - difference between creating an array “normally” and with malloc 当我必须指定内存大小时,malloc数组和常规数组有什么区别? - What is the difference between malloc array and regular array when in both I have to specify memory size? 在 C 中,与 malloc 和变量数组声明相关的两个给定代码片段之间有什么区别? - In C, what is the difference between the two given code snippets relating to malloc and variable array declaration? int *p[5]和通过malloc得到的指针有什么区别? - What is the difference between int *p[5] and a pointer obtained through malloc? array([array([]),array([])])和array([[],[]])有什么区别? - What is the difference between array([array([]),array([])]) and array([[],[]])? array和ArrayList有什么区别? - What is difference between array and ArrayList?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM