小编Jim*_*son的帖子

相同的匿名结构是否兼容?

考虑这个代码片段:

#include <threads.h>

int thread_start(void *ptr)
{
    struct {
        int a;
        int b;
    } *data = ptr;

    return 0;
}

int main(int argc, char *argv[])
{
    struct {
        int a;
        int b;
    } data;

    thrd_t thread;
    thrd_create(&thread, thread_start, &data);
    thrd_join(thread, NULL);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

关于 a void *to 的转换struct { int; int } *,假设匿名结构的字段与最初分配的结构的字段相同,根据 C 标准,这是定义明确的行为吗?

c standards language-lawyer

4
推荐指数
1
解决办法
104
查看次数

什么在堆栈上和Rust中的堆上发生了什么?

我对Rust的内存分配系统感到困惑.

在Java中,您使用new在堆上分配内存.在你使用的C中malloc(),其他一切都在堆栈中.

我想,在Rust中,Box<T>在堆上分配内存但是在阅读了" Rust编程语言 "第15.2章中的 "定义我们自己的智能指针"一节后,似乎MyBox<T>没有任何特殊的注释来使T堆上的live 值得以实现.

  1. 堆栈究竟是什么以及堆上发生了什么?

  2. 实施MyBox<T>基本上是一样的Box<T>吗?

  3. 如果实现是相同的,那么什么使得T存储在堆而不是堆栈上?

  4. 如果实现不相同,那么Box<T>在堆上分配内存是什么?

rust

1
推荐指数
1
解决办法
667
查看次数

标签 统计

c ×1

language-lawyer ×1

rust ×1

standards ×1