Skip to content

Instantly share code, notes, and snippets.

@d3x0r
Last active December 4, 2018 01:32
Show Gist options
  • Save d3x0r/c2e37709dedb26d4257f5862440b327b to your computer and use it in GitHub Desktop.
Save d3x0r/c2e37709dedb26d4257f5862440b327b to your computer and use it in GitHub Desktop.
C Notes (fixme)

know the length of things.

  • when passing arrays as pointers, include passing the length.
    • function handleBuffer( char*buffer, size_t length );

buffer/length pairing

  • conventions in C libraries typically follow
    • (destination, source) as in destination = source;
    • (state object, int arguments ) as in the state object for the function should be the first parameter.
      • putImage( Image *imageOnto, Image *imageSource, int x, int y );
      • sendTCP( PCLIENT socket, POINTER buffer, size_t length );
    • There is an exception that fread/fwrite is (buffer

Strings

  • it's always safe consider string literals are 'const char []', the converse is not true. While by the standard they are 'char[]'. They are treated in many implementations as data that can be put into read-only/unwritable (constant) memory. - string literals are arrays because sizeof "1234" returns the size of the array. - "1234"[1]='5'; is left open in the standard as an undefined behavior.

Preprocesor

  • Preprocessor commands might be indented like this
#if defined( _WIN32 ) || defined( __CYGWIN__ )
#  define _GetMyThreadID()  ( (( ((uint64_t)GetCurrentProcessId()) << 32 ) | ( (uint64_t)GetCurrentThreadId() ) ) )
#  define GetMyThreadID()  (GetThisThreadID())
#else
// this is now always the case
// it's a safer solution anyhow...
#  ifndef GETPID_RETURNS_PPID
#    define GETPID_RETURNS_PPID
#  endif
#  ifdef GETPID_RETURNS_PPID
#    ifdef __ANDROID__
#      define GetMyThreadID()  (( ((uint64_t)getpid()) << 32 ) | ( (uint64_t)(gettid()) ) )
#    else
#      define GetMyThreadID()  (( ((uint64_t)getpid()) << 32 ) | ( (uint64_t)(pthread_self()) ) )
#    endif
#  else
#    define GetMyThreadID()  (( ((uint64_t)getppid()) << 32 ) | ( (uint64_t)(getpid()|0x40000000)) )
#  endif
#  define _GetMyThreadID GetMyThreadID
#endif
``
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment