Skip to content
Prev 46540 / 63424 Next

Why does an empty vector occupy 40 bytes?

On 29/08/2013 14:39, Hadley Wickham wrote:
You have not: the start of the vector area also needs to be aligned 
(since it might hold doubles or CHARSXPs).

See memory.c, which says, inter alia

/* All vector objects must be a multiple of sizeof(SEXPREC_ALIGN)
    bytes so that alignment is preserved for all objects */

/* Node Classes.  Non-vector nodes are of class zero. Small vector
    nodes are in classes 1, ..., NUM_SMALL_NODE_CLASSES, and large
    vector nodes are in class LARGE_NODE_CLASS.  For vector nodes the
    node header is followed in memory by the vector data, offset from
    the header by SEXPREC_ALIGN. */

And also, object.size() is only approximate, and documented to be so. In 
fact many short vectors are using larger blocks of memory incompletely, 
and of course the OS is supplying memory in pages.