了解malloc和灵活的阵列成员

发布于 2025-02-04 07:41:53 字数 1322 浏览 1 评论 0原文

我很好奇malloc()实际上分配内存。我正在通过knking阅读C编程。特别是,第17章。最初在章节void *malloc(size_t size)被描述为函数,该功能分配了size size> size> bytes的内存块并返回void *指向此内存块的指针。两个主要应用程序是动态分配字符串和数组。当称为malloc()时,返回的指针将被施放为适当的类型,例如,

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

malloc()返回将被施加到(int *) )。我的理解是,由于这种铸件发生,因此由malloc()的呼叫分配的内存块包含非直接的整数。但是,在本章中进行了更多阅读后,我认为我错了,但我无法确切地弄清楚发生了什么。

当阅读有关灵活数组成员的本章的最后一部分时,理解的冲突发生。定义结构的想法是,最后一个成员是“不完整”的,即

struct vstring {
    int len;
    char chars[];
};

作者然后继续说:我引用:

包含灵活数组成员的结构是 不完整的类型 。不完整的类型缺少确定其需要多少内存所需的信息的一部分。 ...特别是,不完整的类型不能成为其他结构的成员或数组的元素。但是,数组可能包含具有灵活数组成员的指针。

因此,很明显,我之前的理解必须有缺陷,否则会有一个呼叫,例如,

struct vstring *str = malloc(sizeof(struct vstring) + n);

将分配包含不完整类型的数组。

malloc()被铸造后的特定类型数组分配的内存块吗?如果不是,那么以下操作如何工作,

struct node {
    int value;
    struct node *next;
};

struct node *new_node = malloc(sizeof(*new_node));
new_node->value = 10;

如果由malloc()调用分配的内存不被声明为struct> struct struct node的元素?即使是我在帖子开始时放置的整数数组示例,我也能够立即通过订阅n来访问分配的内存的元素。

I am curious of how malloc() actually allocates memory. I am reading C programming by K.N.King for reference. In particular, chapter 17. Initially in the chapter void *malloc(size_t size) is described as function which allocates a block of memory of size bytes and returns a void * pointer to this memory block. Two main applications being dynamically allocating strings and arrays. When malloc() is called the returned pointer will be cast to the appropriate type, for example in,

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

the malloc() return will be cast to (int *). It was my understanding that since this cast occurs the memory block allocated by the call of malloc() contains uninitialised integers. However, after more reading in the chapter I think I am wrong but I can't figure out exactly what is going on.

The conflict in understanding occurred when reading the last section of the chapter on flexible array members. The idea of defining a structure with the last member being "incomplete", i.e.,

struct vstring {
    int len;
    char chars[];
};

The author then goes on to say, and I quote:

A structure that contains a flexible array member is an incomplete type. An incomplete type is missing part of the information needed to determine how much memory it requires. ... In particular, an incomplete type can't be a member of another structure or an element of an array. However, and array may contains pointers to structure that have a flexible array member.

So clearly my earlier understand must be flawed otherwise a call such as,

struct vstring *str = malloc(sizeof(struct vstring) + n);

would allocate an array containing an incomplete type.

Is the block of memory allocated by malloc() an array of a particular type after being cast? If not, then how can the following work,

struct node {
    int value;
    struct node *next;
};

struct node *new_node = malloc(sizeof(*new_node));
new_node->value = 10;

if the memory allocated by the malloc() call is not declared as elements of struct node? Even the integer array example I put at the beginning of the post, I would be able to access the elements of the the allocated memory by subscripting n immediately.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万水千山粽是情ミ 2025-02-11 07:41:53

malloc()返回将被施加到(int *)

n = malloc(sizeof(*n));中没有铸件。铸件是源代码中的显式操作员,加号的方式是添加的操作员或星号是乘法的操作员。演员是括号中的类型名称。在n = malloc(sizeof(*n));中,由malloc返回的值自动转换为n的类型, int *。这是一种转换,而不是演员。

是由malloc()分配的内存块。

malloc分配的内存没有声明的类型。出于C标准中的正式语义和技术原因,一旦将数据存储到其中,它就会采用有效的类型。只要您不尝试对内存做任何“有趣”的事情,例如将其用作不同类型的不同类型,这在很大程度上与普通使用无关。

如果没有,那么以下如何工作……

一旦您使用malloc来为对象分配足够的内存,则可以将该对象的值存储在分配的内存中。

…如果由malloc()呼叫分配的内存未声明为struct struct node

的元素

内存的有效类型由用于存储到内存的 lvalue 表达式的类型确定。 lvalue 是一种潜在指定对象的表达式。对于声明的对象,就像int x;一样,对象的名称x是它的lvalue表达式。当我们有指针时,就像int *p;一样,然后*pp指向的对象的lvalue表达式它是该对象的内存的有效指针,而不是无效的指针或无效的指针)。

已分配了new_node指向分配了malloc的内存,然后*new_node是适当类型的LVALUE表达式,struct struct node < /code>和new_node-&gt; valueint struct> struct> struct> struct> struct> struct> struct> struct> struct node的lvalue表达式。使用这些LVALUE表达式会为编译器提供有关如何在这些位置处理内存的信息。

the malloc() return will be cast to (int *).

There is no cast in n = malloc(sizeof(*n));. A cast is an explicit operator in source code, the way a plus sign is an operator for addition or an asterisk is an operator for multiplication. A cast is a type name in parentheses. In n = malloc(sizeof(*n));, the value returned by malloc is automatically converted to the type of n, which is int *. This is a conversion, not a cast.

Is the block of memory allocated by malloc() an array of a particular type after being cast?

The memory allocated by malloc has no declared type. For formal semantic and technical reasons in the C standard, it takes on an effective type once you store data into it. This is largely irrelevant to ordinary use as long as you are not trying to do anything “funny” with the memory, such as using it as different types at different types.

If not, then how can the following work…

Once you have used malloc to allocate sufficient memory for an object, you may store a value for that object into the allocated memory.

… if the memory allocated by the malloc() call is not declared as elements of struct node?

The effective type of the memory is determined by the type of the lvalue expression used to store to the memory. An lvalue is an expression that potentially designates an object. For a declared object, as with int x;, the name of the object, x, is an lvalue expression for it. When we have a pointer, as with int *p;, then *p is an lvalue expression for the object that p points to (assuming it is a valid pointer to memory for such an object, not a null pointer or an invalid pointer).

Have assigned new_node to point to memory allocated with malloc, then *new_node is an lvalue expression for the appropriate type, struct node, and new_node->value is an lvalue expression for the int member of struct node. Using these lvalue expressions informs the compiler about how to treat the memory at those locations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文