[NO REPLY] Base types and pointers (rev #3, 2018-1-15)

A place of insane ideas, nothing to see here.
User avatar
Giovanni
Site Admin
Posts: 12949
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 732 times
Been thanked: 609 times
Contact:

[NO REPLY] Base types and pointers (rev #3, 2018-1-15)

Postby Giovanni » Sun Dec 10, 2017 5:50 pm

Constants

  • Binary constants are allowed using the 0b prefix.
  • The c and s suffixes are added for char and short constants. Can be used with u.

Types checking

  • Strong checking, mixing types always gives errors (not warnings).
  • No integer promotion mechanism, mixing types is forbidden without explicit casts.
    • Sign-extension and truncation is only done through casts.
  • Casts are mandatory for constants assigned to derived types.
  • Constant suffixes or casts are mandatory for everything but "int".

Bool type

  • Bool is a base type not compatible with integer types without an explicit cast, it can be assigned with true/false or the result of comparison operators.
  • The words true and false are actual keywords not macros.
  • Comparison operators explicitly return a bool.
  • Conditional statements if, do, while, for ( ; condition; ) explicitly require a bool.

Pointers

There are several restrictions:

  • Assigning pointers to different pointer types is an error.
  • Comparing pointers with different pointer types is an error.
  • A cast between pointers is possible only if the two pointers are "related", meaning that one pointer points to an ancestor type of the other type.
  • The void type is considered ancestor to all types. It is always possible to cast from/to a pointer to void.
  • Constants can be assigned to pointers using casts but only if the constant is compatible with the pointed type alignment constraints.
  • The void type has no alignment constraints. If you NEED to create an unaligned pointer then cast it first to void* then to the final pointer type.

Giovanni

Return to “Safer C”

Who is online

Users browsing this forum: No registered users and 1 guest