gpt4 book ai didi

c++ - 使用 operator new 分配的数据结构的 C 代码是否有任何副作用

转载 作者:太空宇宙 更新时间:2023-11-04 00:45:00 24 4
gpt4 key购买 nike

啊.h

struct S1{...};

extern "C" struct S1 *f();

extern "C" void freeS1(struct S1 *ptr);

a.cpp

extern "C" struct S1 *f() {
struct S1 *ptr = new struct S1();
...
return ptr;
};

extern "C" void freeS1(struct S1 *ptr) {
delete ptr;
};

b.c

struct S1 *ptr = f();
.....
freeS1(ptr);

最佳答案

内存就是内存,哪个分配器提供它并不重要,只要它正确对齐并且分配和释放函数正确匹配(并且结构定义相同)。应该没有问题。

关于c++ - 使用 operator new 分配的数据结构的 C 代码是否有任何副作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44773474/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com