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!