☰
Current Page
Main Menu
Home
Home
Editing
FixedWidthTypes
Edit
Preview
h1
h2
h3
Keybinding
default
vim
emacs
Markup
Markdown
Plain Text
Pod
RDoc
reStructuredText
AsciiDoc
BibTeX
Creole
MediaWiki
Org-mode
Textile
Help 1
Help 1
Help 1
Help 2
Help 3
Help 4
Help 5
Help 6
Help 7
Help 8
Autosaved text is available. Click the button to restore it.
Restore Text
--- title: FixedWidthTypes --- * Pros and Cons of fixed width types from C99's stdint.h * Fixed width types for platform code and binary codecs * Motivation for standard types for independent code * Use of semantic typedefs * Discriminating uintptr\_t vs intptr\_t vs void* vs size_t # General Portability * If you choose fixed width types then there is less chance of code breaking when moved onto a different platform * I think that's a fallacy. * Where we're needlessly using INT16 or UINT16, we're actually trying to guarantee that int hold a 16-bit value. That's already guaranteed by the C standard anyway. * Similarly with INT32 or UINT32, where the C standard guarantees a long will hold the necessary values. * Consider also the CHAR8*, UINT8*, string.h mess we've got. It really would be simpler to use plain char for strings. # Overflow * Using standard types int and char is only ok if you are absolutely sure that they are never going to overflow under any circumstances / platforms / compilers. * Overflow applies just as much to fixed-width types - if we're going to do any arithmetic on a fixed-width type, we still have to bounds check it. # Linking to standard libraries * There's a huge advantage to standard types in being able to link against different standard libraries without vigorous sprinklings of bug-hiding casts. * Systems programmers have gone to the effort of abstracting the underlying platform in implementing a standard library - it seems wasteful to ignore that. # Guarantee of existance * [u]int[8|16|32]\_t are not guaranteed to exist - a library for an 8-bit micro \*could\* define only int8\_t and uint8_t. * All compliant libraries are required to define [u]int\_least[8|16|32]\_t however, so maybe we ought to use those... :) # Static type checking * it gives Lint a greater chance of picking up errors. * Nope, lint already knows about the width of the platform's standard types. * If we want better checking we should use purely-semantic typedefs (e.g. typedef UINT16 CRC16_T) more and turn on lint's strong type checks. # Performance * As for the performance hit, we are talking about code that has to run efficiently on a 16-bit embedded micro, if that code is then ported to a PC platform then speed of execution is hardly going to be an issue. * Agree. The performance advantage is negligeable. * But why prevent the compiler from doing its job? <!-- vim: filetype=markdown -->
Uploading file...
Sidebar
# SideBar * [Home][1] * [Projects][2] * * * <!-- --> * [Code][3] * [Tech][4] * [Network][5] * [MediaCentre][6] * [UAV][7] * * * <!-- --> * [Travel][8] * [Music][9] * [Horse Riding][10] * [Study][11] * [Games][12] * [Other Activities][13] * * * <!-- --> * [Car][14] * [House][15] * [Watch][16] * [Clothing][17] * [Miscellany][18] * * * [1]: /Home [2]: /Projects [3]: /Code/Code [4]: /Tech/Tech [5]: /Network/Network [6]: /MediaCentre/MediaCentre [7]: /UAV/UAV [8]: /Travel/Travel [9]: /Music/Music [10]: /HorseRiding/HorseRiding [11]: /Study/Study [12]: /Games/Games [13]: /Do/Do [14]: /Car/Car [15]: /House/House [16]: /Watch/Watch [17]: /Clothing/Clothing [18]: /Miscellany/Miscellany <!-- vim: filetype=markdown -->
Edit message:
Cancel