问题
I've the following struct:
#define M 3
#pragma pack(push)
#pragma pack(1)
struct my_btree_node {
struct my_btree_node *pointers[M];
unsigned char *keys[M - 1];
int data[M - 1];
unsigned char number_of_keys;
};
#pragma pack(pop)
The sizeof(struct my_btree_node)
function returns a value of 49 byte for this struct. Does allocating memory for this struct using malloc
return a 64 byte block because on 64 bit systems pointers are 16-byte-aligned or will it indeed be 49 bytes?
Is there a way to align memory with a smaller power of two than 16 and is it possible to get the true size of the allocated memory inside the application?
I want to reduce the number of padding bytes in order to save memory. My applications allocates millions of those structs and I do not want to waste memory.
回答1:
malloc
uses internal heap-structure. It is implementation-dependent yet one may expect that the memory is allocated by a whole number of (internal) blocks. So usually it's not possible to allocate exactly 49 bytes by a single malloc
call. You can build some subsystem of your own on top of malloc
to do this, yet I see no reason why you may want it.
P.S. To reduce memory wasting, you can pre-allocate an array consisting of, say, 100 structs, when you need just one more, and return &a[i] until all free indexes are wasted. As arrays are never padded, the memory wasting would be reduced in about 100 times.
回答2:
malloc(3) is defined to
The
malloc()
andcalloc()
functions return a pointer to the allocated memory, which is suitably aligned for any built-in type. On error, these functions returnNULL
.NULL
may also be returned by a successful call tomalloc()
with a size of zero, or by a successful call tocalloc()
with nmemb or size equal to zero.
So a conforming implementation has to return a pointer aligned to the largest possible machine alignment (with GCC, it is the macro __BIGGEST_ALIGNMENT__)
If you want less, implement your own allocation routine. You could for example allocate a large array of char
and do your allocation inside it. That would be painful, perhaps slower (processors dislike unaligned data, e.g. because of CPU cache constraints), and probably not worthwhile (current computers have several gigabytes of RAM, so a few millions of hundred-byte sized data chunks is not a big deal).
BTW, malloc
is practically implemented in the C standard library (but -on Linux at least- the compiler knows about it, thanks to __attribute__
-s in GNU glibc headers; so some internal optimizations inside GCC know and take care of calls to malloc
).
来源:https://stackoverflow.com/questions/29095298/gcc-memory-alignment-using-malloc