C/C++ Best Practice¶
Note that this page is in addition to the style guide which covers stylistic aspects of writing code, this page covers other topics relating to writing code for Blender.
Order of Operations for "sizeof(..)"¶
When calculating the size of a value in bytes, order
sizeof first, eg:
sizeof(type) * length, this avoids integer overflow errors by
promoting the second value to
size_t in the case it's a smaller type
such as an int.
Note that for array allocation, we have
Comment unused arguments in C++ code¶
When a C++ function has an unused argument, prefer declaring it like
int /*my_unused_var*/ to using the
UNUSED() macro. This is because
the macro does not work properly for MSVC-- it only prevents using the
variable but does not suppress the warning. It also has a more complex
Avoid Unsafe String C-API's¶
Given the current state of Blender's internal data structures (DNA structs for example), fixed size char buffers are used making it necessary to use low level string manipulation.
Unsafe C-API functions should be avoided as these have been a cause of bugs historically (see examples).
This table lists function to avoid and alternatives that should be used instead.
BLI_string_join may also be an alternative for concatenating strings.
Notes relating to fixed size char buffer use:
- Unless otherwise stated fixed sized char buffers must be null terminated.
- Queries that rely on null termination are acceptable such as
strstrand related functions.
- When constructing UTF-8 encoded strings which may not fit destination
buffer, copy the strings with
BLI_strncpy_utf8, or use
BLI_str_utf8_invalid_stripon the resulting string.
- When performing low level operations on byte arrays, it's preferable
to calculate sizes and use
memcpyto construct the buffer.
- Complex logic to construct fixed size char buffers should assert the final size fits within buffer size.
- Libraries in
extern/are not maintained as part of Blender's code, this policy doesn't apply there.
StringPropertyRNA::getcallbacks (defined in
strcpyas the string must be large enough to hold a string length defined by