__BUILTIN_OBJECT_SIZE(3)

πŸ“¦ version 15.1, in freebsd 15.1

okay so bestie, welcome to the __BUILTIN_OBJECT_SIZE page β€” it’s a section 3 manual page from the Library Functions Manual. it’s giving β€œcompiler magic that tells you how big a thing is,” and honestly? iconic. πŸŽ€βœ¨

🌸 NAME 🌸

__builtin_object_size β€” return the size of the given object. that’s the whole assignment, no cap. πŸ’…

✨ SYNOPSIS ✨

size_t
__builtin_object_size(void *ptr, int type);

πŸ¦‹ DESCRIPTION πŸ¦‹

The __builtin_object_size() function is a clang(1) and gcc(1) built-in function that returns the size of the object referenced by ptr if known at compile time. so yeah, the compiler is doing its little detective thing before runtime, and we love that for it. πŸ”πŸ’–

If the ptr expression has any side effects, then they will not be evaluated. like, not even a little bit β€” the side effects just do not happen, periodt. 🫢

🫢 RETURN VALUES 🫢

If the size of the object is not known or the ptr expression has side effects, the __builtin_object_size() function returns:

  • (size_t)-1 for type 0 and 1.
  • (size_t)0 for type 2 and 3.

If the size of the object is known, then the __builtin_object_size() function returns the maximum size of all the objects that the compiler knows can be pointed to by ptr when type & 2 == 0, and the minimum size when type & 2 != 0. yes, that bitmask check is doing exactly what it looks like, bestie. 🧚✨

πŸ‘‘ SEE ALSO πŸ‘‘

πŸ’œ HISTORY πŸ’œ

The __builtin_object_size() appeared in GCC 4.1. a lil throwback, but make it compiler lore. ⭐

πŸ’€ CAVEATS πŸ’€

bestie, listen: this is a non-standard, compiler-specific extension. so don’t expect every random toolchain on the timeline to know the assignment. πŸ™ˆ

Note that currently the object size calculation pass is only done at -O1 or above, meaning that this function always returns -1 when the optimizer is off. that’s right β€” optimizer off means -1 every time, no cap. 😭

There are some discussions about always doing the object size pass, but the issue is that without the optimization pass data sizes are not going to be correct. so it’s not just being dramatic for fun β€” the numbers genuinely get messy. πŸ’€

For that reason code fortification (size-checked replacement functions) is currently disabled when optimization is off. so yeah, the fortified vibe is sitting this one out when -O0 shows up. 🫢


πŸ’– that’s the whole page, bestie. this one came from Debian, dated April 27, 2024 β€” mwah, slay, and may your object sizes be known at compile time. πŸ’…πŸŽ€

πŸ“¦ where this page comes from
  • freebsd 15.1: base 15.1
  • πŸ“œ the page's license: BSD-2-Clause
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms