Cpp/Card

From PeerFreedom Wiki
< Cpp
Jump to navigation Jump to search

The "Card" is a cheat sheet with some of most common important information and reminders, for us (developers using cpp-pfp).

Topics are mainly taken from Category:Cpp

Standard

Conversions

See: Media:Cpp_conversions_bytelike.ods

--- Byte-like types ---

  • membyte: 3 types: byte, char, u-char (Cpp/MemorySafety1)
  • byteequiv: 4 types: byte, char, u-char, s-char

--- Linear memory ---

From any of membyte --> byteequiv obj-->obj or ptr-->ptr or ref-->ref use to_uchar_ptr : to_X_Y (X: char,uchar,schar,byte; Y:nothing,ptr,ref)

result basic type result should be an scalar object result should be a pointer result should be a reference
byte to_byte to_byte_ptr to_byte_ref
char to_char to_char_ptr to_char_ref
unsigned char to_uchar to_uchar_ptr to_uchar_ref
signed char to_schar no (UB) no (UB)

Scalar objects (simple 1 object)

- from byte from u-char from char from s-char
to byte (same) GOOD idef(2) idef(1)
to u-char GOOD (same) idef(2) idef(1)
to char idef(2) idef(2) (same) idef?
to s-char idef? idef? idef? (same)
// (1)
unsigned char hp = 200;
signed char x = hp;
x -= damage;
x +=  regen;
if (x < 0) { cout<<" Dead X-( "; x=0; }
// ---
if (x < 0) throw runtime_error("invalid x hp");
hp = x;
// (2)

/// client_X.cpp
char txt = 'A';
byte net = txt;
send_network(net);

// server_Y.cpp
byte net;
recv_network(net);
char txt = net; // == 'A' 

char(X) ---> byte ---other machine---> byte ---> char(Y);

char(X) ---> unsigned char ---other machine---> unsigned char ---> char(Y);


Memory pointer conversion (and then accessing it)

- from byte* from u-char* from char* from s-char*
to byte* (same) GOOD GOOD? idef?
to u-char* GOOD (same) idef? idef?
to char* idef? idef? (same) idef?
to s-char* UB UB UB ?


uchar* --> char* to_char_ptr

char* --> byte& to_byte_ref

vector_of_uchar.begin() --> byte& to_byte_ref

vector_of_uchar.at(0) --> byte& to_byte_ref


--- Containers ---

vector/array/mem* byte-like --> span<byte> to_spanbyte_const() / to_spanbyte_mutable() Cpp/DataMemoryToCrypto


vector/array/mem* byte-like --> hex-string ... ?

vector/array/mem* byte-like <-- hex-string ... ?

Create single byte object

stdpfp::byte b = stdpfp::byte(0x04); // C++14
std::byte b { 0x04 }; // C++17

b = stdpfp::byte(0x04); // C++14
b = { 0x04 }; // C++17 ?

fun( xxx + stdpfp::byte(0x04) ); // C++14
fun( xxx + std::byte{ 0x04 } ); // C++17 ?