Malloc vs新 - 不同的填充 [英] Malloc vs new -- different padding
问题描述
我正在审查我们的项目的其他人的C ++代码,该项目使用MPI进行高性能计算(10 ^ 5 - 10 ^ 6个核心)。该代码旨在允许在不同架构上的(可能)不同的机器之间进行通信。他写了一个注释,说明了一些符合以下内容:
我们通常使用
new
和delete
,但这里我使用malloc
和c $ c>。这是必要的,因为当使用
new
时,一些编译器将以不同的方式填充数据,导致在不同平台之间传输数据时出现错误。这不会发生在malloc
。
我从标准新
vs malloc
的问题知道。
new / delete和malloc之间的区别是什么/ free?提示编译器可以不同方式计算对象的大小(但是为什么不同于使用 sizeof
?)。 p>
malloc& placement new vs. new 是一个相当普遍的问题,但只是谈到 new
使用构造函数,其中 malloc
't,与此无关。
malloc如何理解对齐?说,内存保证与 new
或 malloc
这是我以前想的。
我的猜测是,他错误诊断自己的错误在过去的一段时间,并推断 new
和 malloc
给出不同数量的填充,我认为可能不是这样。但我没有找到与谷歌或任何以前的问题的答案。
帮助我,StackOverflow,你是我唯一的希望。
p> IIRC有一个挑剔的点。 malloc
保证返回与任何标准类型对齐的地址。 :: operator new(n)
只能保证返回一个地址与任何标准类型不大于n 对齐,如果 T
不是字符类型,则 new T [n]
只需要返回与
但是这只在你使用特定于实现的技巧时才有意义,比如使用指针的底部几个位来存储标志,或者以其他方式依赖地址来比它严格需要的更多的对齐。
它不影响对象内的填充,其必然具有完全相同的布局,如何分配它占用的内存。所以很难看出差异如何导致传输数据的错误。
有没有任何迹象,该评论的作者认为对象在堆栈或全局,在他看来他们是填充像malloc或填充像新的?
也许他很困惑,但也许他所说的代码不仅仅是一个直接的区别 malloc(sizeof(Foo)* n)
vs new Foo [n]
。也许更像:
malloc((sizeof(int)+ sizeof(char))* n);
vs。
code> struct Foo {int a;炭b }
new Foo [n];
也就是说,也许他会说我使用malloc em>表示我手动将数据打包到未对齐的位置,而不是使用结构体。实际上 malloc
不需要为了手动打包结构,但没有意识到这是一个较小程度的混乱。有必要定义通过线路发送的数据布局。当使用 struct 时,不同的实现将以不同的方式填充数据。
I'm reviewing someone else's C++ code for our project that uses MPI for high-performance computing (10^5 - 10^6 cores). The code is intended to allow for communications between (potentially) different machines on different architectures. He's written a comment that says something along the lines of:
We'd normally use
new
anddelete
, but here I'm usingmalloc
andfree
. This is necessary because some compilers will pad the data differently whennew
is used, leading to errors in transferring data between different platforms. This doesn't happen withmalloc
.
This does not fit with anything I know from standard new
vs malloc
questions.
What is the difference between new/delete and malloc/free? hints at the idea that the compiler could calculate the size of an object differently (but then why does that differ from using sizeof
?).
malloc & placement new vs. new is a fairly popular question but only talks about new
using constructors where malloc
doesn't, which isn't relevant to this.
how does malloc understand alignment? says that memory is guaranteed to be properly aligned with either new
or malloc
which is what I'd previously thought.
My guess is that he's misdiagnosed his own bug some time in the past and deduced that new
and malloc
give different amounts of padding, which I think probably isn't true. But I can't find the answer with Google or in any previous question.
Help me, StackOverflow, you're my only hope!
IIRC there's one picky point. malloc
is guaranteed to return an address aligned for any standard type. ::operator new(n)
is only guaranteed to return an address aligned for any standard type no larger than n, and if T
isn't a character type then new T[n]
is only required to return an address aligned for T
.
But this is only relevant when you're playing implementation-specific tricks like using the bottom few bits of a pointer to store flags, or otherwise relying on the address to have more alignment than it strictly needs.
It doesn't affect padding within the object, which necessarily has exactly the same layout regardless of how you allocated the memory it occupies. So it's hard to see how the difference could result in errors transferring data.
Is there any sign what the author of that comment thinks about objects on the stack or in globals, whether in his opinion they're "padded like malloc" or "padded like new"? That might give clues to where the idea came from.
Maybe he's confused, but maybe the code he's talking about is more than a straight difference between malloc(sizeof(Foo) * n)
vs new Foo[n]
. Maybe it's more like:
malloc((sizeof(int) + sizeof(char)) * n);
vs.
struct Foo { int a; char b; }
new Foo[n];
That is, maybe he's saying "I use malloc", but means "I manually pack the data into unaligned locations instead of using a struct". Actually malloc
is not needed in order to manually pack the struct, but failing to realize that is a lesser degree of confusion. It is necessary to define the data layout sent over the wire. Different implementations will pad the data differently when the struct is used.
这篇关于Malloc vs新 - 不同的填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!