Common GCC Attributes

Function attributes fallthrough The fallthrough attribute with a null statement serves as a fallthrough statement. It hints to the compiler that a statement that falls through to another case label, or user-defined label in a switch statement is intentional and thus the -Wimplicit-fallthrough warning must not trigger. For compatibility with GCC and Clang you can also use // fall through or // fallthrough. switch(cond) { case 1: bar(1); __attribute__((fallthrough)); case 2: … } deprecated & deprecated (msg) The deprecated attribute results in a warning if the function is used anywhere in the source file. This is useful when identifying functions that are expected to be removed in a future version of a program. The warning also includes the location of the declaration of the deprecated function, to enable users to easily find further information about why the function is deprecated, or what they should do instead. ...

13 August 2024 · 4 min

What is the difference between `char a[]` and `char *p`

The Array version: char a[] = "string"; Creates an array that is large enough to hold the string literal string, including its NULL terminator. The array string is initialized with the string literal string. The array can be modified at a later time. Also, the array’s size is known even at compile time, so sizeof operator can be used to determine its size. The pointer version: char *p = "string"; Creates a pointer to point to a string literal “string”. This is faster than the array version, but string pointed by the pointer should not be changed, because it is located in a read only implementation-defined memory. Modifying such an string literal results in Undefined Behavior. ...

28 February 2024 · 3 min