Should a C ++ embedded application use a common header with typedefs for C ++ built-in types?

This is a common practice when I try to avoid the direct use of built-in types and instead include standardtypes.h, which has elements such as:

// \Common\standardtypes.h typedef double Float64_T; typedef int SInt32_T; 

Almost all components and source files become dependent on this header, but some people claim that it needs to ignore the size of types (in practice, this was not necessary).

Is this good practice (especially on systems with large components)? Are there any better alternatives? Or should built-in types be used?

+4
source share
7 answers

The biggest problem with this approach is that so many developers do this, if you use a third-party library, you are likely to run into a conflict of symbol names or multiple names for the same types. It would be wise to stick to the standard implementation provided by C99 stdint.h.

If your compiler does not provide this header (such as VC ++), create one that complies with this standard. For example, for VC ++ you can find http://msinttypes.googlecode.com/svn/trunk/stdint.h

In your example, I see a small point for determining floating point types of a certain size, since they are usually closely related to the target FP equipment and the view used. In addition, the range and accuracy of the floating point value is determined by the combination of the exponent width and significant width, so the total width alone does not mean much or does not guarantee compatibility between platforms. As for single and double precision, there is much less variability on different platforms, most of which use IEEE-754 representations. On some 8-bit compilers, float and double are both 32-bit, and long double on x86 GCC is 80 bits, but only 64 bits in VC ++. X86 FPU supports 80 bits in hardware (2) .

+1
source

You can use the standardized versions available in modern implementations of C and C ++ in the header file: stdint.h

It has the following types: uint8_t, int32_t, etc.

All in all, this is a good way to protect your code from platform dependence. Even if you have not experienced a need for it to date, this certainly simplifies the interpretation of the code, since you do not need to guess the size of the repository, as for "int" or "long", which will differ in size from the platform.

+9
source

It would probably be better to use the standard POSIX types defined in stdint.h et al., For example. uint8_t , int32_t etc. I'm not sure if there is still a part of C ++, but they are on C99.

+3
source

Since it has not yet been specified, and even if you have already accepted the answer:

Only specific types are used when you need specific types. This basically means that when you save data, if you interact directly with the equipment or use some other code (such as a network stack) that expects specific types. In most cases, you should simply use abstract-sized types so that your compiler can optimize more intelligently and so that future readers of your code are not burdened with useless details (such as the size and signature of the loop counter).

(As mentioned in several other answers, use stdint.h, not something homemade, when writing new code and not interacting with the old one.)

+2
source

I think this is not a good practice. Good practice is to use something like uint32_t where you really need a 32-bit unsigned integer, and if you don't need a specific range, use only unsigned .

+1
source

This can make a difference if you are doing cross-platform code where the size of your own types can vary from system to system. For example, the type wchar_t can vary from 8 bits to 32 bits, depending on the system.

Personally, however, I do not think that the approach you described is as practical as its proponents have suggested. I would not use this approach even for a cross-platform system. For example, I would prefer my system to use wchar_t directly and simply write code with the knowledge that the size of wchar_t will vary by platform. I think this is more valuable.

+1
source

As others have said, use the standard types defined in stdint.h. I do not agree with those who say to use them only in some places. This works well when you work with a single processor. But when you have a project that uses several types of processors (for example, ARM, PIC, 8051, DSP) (which is not uncommon in embedded projects), tracking what int means or can copy code from one processor to another almost requires the use of definitions type fixed size.

At least it is required for me, since over the past six months I have been working on 8051, PIC18, PIC32, ARM and x86 code for various projects, and I can not track all the differences without pretending to be somewhere.

0
source

All Articles