Hacker News new | past | comments | ask | show | jobs | submit login

The C language spec isn't what important here it's the ABI doesn't know the difference.



if using an int* over an int[] changes the memory layout of a struct, that necessarily implies a difference in the ABI.

As an example, C++'s std::array can be implemented as a struct containing a fixed-size C-style array. This can be passed-by-value. This means that returning a std::array can be more performant than returning a std::vector (which might be implemented as an int* that is reallocated when you add too many elements), because a std::array is returned on the stack while a std::vector is returned on the heap.

I was bit by this once when returning a std::unordered_map because I was doing a heap-allocation in a performance-critical section.


The memory layout is the ABI not the language spec.


The language spec for C constrains the memory layout of a struct. From 6.7.3.2.7 of the draft spec:

> a structure is a type consisting of a sequence of members, whose storage is allocated in an ordered sequence, (emphasis added)

https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: