我们可以重用分配的内存吗 [英] Can we reuse allocated memory

查看:64
本文介绍了我们可以重用分配的内存吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对此问题的后续操作.

在解释我的问题时,我声明可以重用已分配的内存,因为它没有声明的类型,并且被告知它是错误的C.

When explaining my problem, I declared that allocated memory could be reused because it has no declared type, and I was told that it was incorrect C.

以下是说明问题的代码示例:

Here is a code example illustrating the question:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

struct Elt {
    int id;
    char name[32];
};

struct Elt2 {
    double val;
    char name[16];
};

static_assert(sizeof(struct Elt2) < sizeof(struct Elt), "Incorrect sizes");

int main() {
    struct Elt actual1 = { 1, "foo"};
    struct Elt2 actual2 = {2.0, "bar"};

    struct Elt* elt = malloc(sizeof(struct Elt));
    memcpy(elt, &actual1, sizeof(*elt));          // populates the allocated memory
    printf("elt: %d %s\n", elt->id, elt->name);
    struct Elt2 *elt2 = (void *) elt;             // declares a new pointer to a shorter type
    memcpy(elt2, &actual2, sizeof(*elt2));        // effective type is now struct Elt2
    printf("elt2: %g %s\n", elt2->val, elt2->name);
    //printf("elt: %d %s\n", elt->id, elt->name);    UB: storage now contains an Elt2 object
    free(elt);                                    // only legal use for elt
    return 0;
}

我相信n1570草案的6.5表达式§6允许:

I believe that 6.5 Expression §6 of draft n1570 allows it:

访问其存储值的对象的有效类型是声明的类型. 87)如果一个值通过一个值存储到一个没有声明类型的对象中 具有非字符类型的左值,则左值的类型变为 该访问以及未修改的后续访问的对象的有效类型 储值.如果将值复制到没有声明类型的对象中,则使用 memcpy或memmove,或者被复制为字符类型的数组,然后是有效类型 该访问以及未修改该对象的后续访问的已修改对象的 值是从中复制值的对象的有效类型(如果有的话).

The effective type of an object for an access to its stored value is the declared type of the object, if any.87) If a value is stored into an object having no declared type through an lvalue having a type that is not a character type, then the type of the lvalue becomes the effective type of the object for that access and for subsequent accesses that do not modify the stored value. If a value is copied into an object having no declared type using memcpy or memmove, or is copied as an array of character type, then the effective type of the modified object for that access and for subsequent accesses that do not modify the value is the effective type of the object from which the value is copied, if it has one.

注释87:

87)分配的对象没有声明的类型.

87) Allocated objects have no declared type.

问题:

是否正在重用分配的内存来存储另一个适合该内存的对象 符合C?

Is reusing allocated memory for storing a different object that can fit in that memory conformant C?

推荐答案

如果不是,那将是灾难性的.许多人使用这些技巧在malloc上实现自己的细粒度内存管理.

If not, that would be catastrophic. Many people use such tricks to implement their own fine grained memory management on to of malloc.

是的,这正是您所引用的标准中的段落.注意,它会仔细选择单词.它说

So, yes, this is exactly what the paragraph in the standard that you are citing is about. Notice that it choses the words carefully. It says

如果将值存储到具有未声明类型 ...

这个没有声明类型的属性不会在对象的整个生命周期内发生变化,因此该规定在任何时候将新值写入该对象时都适用.

this property of having no declared type doesn't change through the lifetime of the object, so the provision applies at any time a new value is written into it.

如果出于某种奇怪的原因,委员会希望说有效类型只能更改一次,那么他们会说类似

If, for some weird reason the committee would have wanted to say that the effective type is only changeable once, they would have say something like

如果将值存储到具有无效类型 ...

这篇关于我们可以重用分配的内存吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆