with a single compilation unit, a single file is compiled that includes all necessary other code to build one binary object. the main file may have .h or .c suffix, the latter especially when it contains a main function and not merely declarations for a library. this can be the most simple way for small projects.
the more common way is to compile parts of the application separately into multiple objects and maintain makefiles as well as header files with declarations for each object and then use a linker to connect references between objects. this has benefits like encapsulation and saving time in the development of big projects by only recompiling objects that changed, but introduces significant overhead and may reduce optimization opportunities.
a popular project that uses as single compilation unit is sqlite.
the style of having one file including others reminds of including javascript in html.
structs
functions
preprocessor
floor
is often slowterminology for function parameters
parameter: formal parameter: the variables in a function declaration/definition. describes what arguments a function can take
argument: actual parameter: the values or variables passed during a function call
file_buffer = mmap(0, file_size, PROT_READ, MAP_SHARED, file_descriptor, 0); md5((unsigned char*)file_buffer, file_size, result); munmap(file_buffer, file_size);
[1 2 3 1 2 3] vs [[1 2 3] [1 2 3]]
flat
nested
easier to access because sub-arrays can be iterated with a single incremented index, and without having to incorporate the sub-array size in the indexing calculation
easier to use with generic array operations, for example sorting
the data for sub-arrays has to be allocated separately and later freed
pro
con
general alignment: each type aligns to a multiple of its size, with padding added between members and at the struct’s end to maintain alignment. mixed type cases:
used size and total allocated size can be tracked separately. the total length of active elements can be reduced but elements can also be added up to the allocated size, and the allocated size can be automatically expanded, for example with realloc
pro
there are multiple options for when to do resizing
option
option
manual ensure-n before usage, which resizes if necessary
no free space checks when adding
trying to add more than what was allocated for may lead to buffer overflows
the biggest slowdown i have experienced when programming in c versus other languages comes from having to be more specific: