8000 CppLang · euccas/expmap Wiki · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

CppLang

Euccas Chen edited this page Dec 15, 2017 · 9 revisions

C++ Language Notes

unsigned int types

unsigned int

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.

uint{bit}_t

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.

strcpy vs memcpy

  • Behavior difference: strcpy stops when it encounters a NULL or '\0'
  • Performance difference: memcpy is usually more efficient than strcpy, which must scan the data it copies

How to check stack collapsing issue around arrays at runtime?

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.

Visual Studio reports vector out of range error

Clone this wiki locally
0