-
Notifications
You must be signed in to change notification settings - Fork 0
CppLang
unsigned int
is an unsigned integer and the compiler decides its bits. The C++ standard requires the minimum range is 0-65535, which is uint16.
Like int
, unsigned int
typically is an integer that is fast to manipulate for the current architecture (normally it fits into a register), so it's to be used when a "normal", fast integer is required.
unsigned
and unsigned int
are synonymous.
The uint{bit}_t types are used when you need an exact-width ingeter, such as writing to hardware registers.
- uint64_t: 64bit unsigned integer. Range: 0 - 0xFFFFFFFFFFFFFFFF (2^64-1 18,446,744,073,709, 551,615)
- uint32_t: 32bit unsigned integer. Range: 0 - 0xFFFFFFFF (2^32-1 4,294,967,295)
- uint16_t: 16bit unsigned integer. Range: 0 - 0xFFFF (2^16-1 65,535)
- uint8_t: 8bit unsigned integer. Range: 0 - 0xFF (2^8-1 255)
unsigned int
types are cross-platform, but uint{bit}_t
types are not.
- Behavior difference:
strcpy
stops when it encounters aNULL
or'\0'
- Performance difference:
memcpy
is usually more efficient thanstrcpy
, which must scan the data it copies
When accessing an index in array which is out of the array's index range, sometimes C/C++ won't give you error message telling you the access is out of range. This happens when the stack memory collapses. You may only observe such errors when finding other pointer's address is changed, which is not supposed to change.
Such issues can be avoided if we check if accessing is out of range at runtime.
One method is overloading the []
operation of the array.
In the overloaded function, first check if the index is out of range (by comparing with array's length, for example).
If using a vector, such out of range issue can be reported.