A question about memory allocation in C. Yes, I have to use C. There's
So I'm writing a program to build some trees, and the relevant part of my code looks pretty much like this:
struct _node
{
struct node *parent;
char flags;
struct _node *l;
struct _node *r;;
};
typedef struct _node node;
Now, this is a very memory-intense operation, so I want these structs to be as small as possible. (Yes, it really does matter) When I do the math on memory size, I see that I should need (32 bits*3) + 8bits = 104 bits = 13 bytes.
The problem is, sizeof(node) returns 16 bytes = 128 bits!! So I'm allocating more than I need here! And if I manually use malloc() to allocate 13 bytes, I get overflow errors.
Also interestingly, I'm able to stick 3 more chars in there, without increasing the node size. This suggests to me that my compiler is allocating memory in 32-bit chunks. Can't I get it to allocate 1 byte at a time? This is GCC on 32-bit linux, FWIW.
posted by sbutler at 12:27 PM on November 8, 2006