Skip to content

Instantly share code, notes, and snippets.

@shafik
shafik / WhatIsStrictAliasingAndWhyDoWeCare.md
Last active April 26, 2025 00:19
What is Strict Aliasing and Why do we Care?

What is the Strict Aliasing Rule and Why do we care?

(OR Type Punning, Undefined Behavior and Alignment, Oh My!)

What is strict aliasing? First we will describe what is aliasing and then we can learn what being strict about it means.

In C and C++ aliasing has to do with what expression types we are allowed to access stored values through. In both C and C++ the standard specifies which expression types are allowed to alias which types. The compiler and optimizer are allowed to assume we follow the aliasing rules strictly, hence the term strict aliasing rule. If we attempt to access a value using a type not allowed it is classified as undefined behavior(UB). Once we have undefined behavior all bets are off, the results of our program are no longer reliable.

Unfortunately with strict aliasing violations, we will often obtain the results we expect, leaving the possibility the a future version of a compiler with a new optimization will break code we th

@nickrolfe
nickrolfe / windows_modern_opengl_context.c
Last active April 21, 2025 16:43
Sample code showing how to create a window using a modern OpenGL core profile context without any libraries other than the standard Win32 wglXXX calls.
// Sample code showing how to create a modern OpenGL window and rendering context on Win32.
#include <windows.h>
#include <gl/gl.h>
#include <stdbool.h>
typedef HGLRC WINAPI wglCreateContextAttribsARB_type(HDC hdc, HGLRC hShareContext,
const int *attribList);
wglCreateContextAttribsARB_type *wglCreateContextAttribsARB;
@mmmunk
mmmunk / freestanding_hello.c
Created September 26, 2016 09:30
Freestanding MinGW
// Freestanding with MinGW:
// http://nullprogram.com/blog/2016/01/31/
// http://nullprogram.com/blog/2016/02/28/
// https://support.microsoft.com/da-dk/kb/99456
#include <windows.h>
int WINAPI mainCRTStartup(void)
{
char msg[] = "Hello, world!\n";
@philipheimboeck
philipheimboeck / Readme.md
Last active March 29, 2023 13:20
C++ String: An own String implementation with operator overloading and free methods

C++ String

Example application that demonstrates the use of the following:

  • Operator overloading
    • Free Operators
    • Class Operators
  • Friendship
  • Iterator
@aras-p
aras-p / preprocessor_fun.h
Last active May 3, 2025 13:47
Things to commit just before leaving your job
// Just before switching jobs:
// Add one of these.
// Preferably into the same commit where you do a large merge.
//
// This started as a tweet with a joke of "C++ pro-tip: #define private public",
// and then it quickly escalated into more and more evil suggestions.
// I've tried to capture interesting suggestions here.
//
// Contributors: @r2d2rigo, @joeldevahl, @msinilo, @_Humus_,
// @YuriyODonnell, @rygorous, @cmuratori, @mike_acton, @grumpygiant,
@lynxluna
lynxluna / main.c
Created December 8, 2012 22:01
Win32 Game Loop
#include <Windows.h>
#include <tchar.h>
#define CURRENT_WND_CLASS _T("GameWndClass_Didiet")
#define DEF_CX 800
#define DEF_CY 600
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
INT WINAPI _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow )
@yoggy
yoggy / CreateProcess_CreateNoWindow_Test.cpp
Created March 6, 2012 09:36
How to use CreateProcess() function (WIN32)
//
// see also...
//
// CreateProcess (MSDN)
// http://msdn.microsoft.com/ja-jp/library/cc429066.aspx
//
#include <SDKDDKVer.h>
#include <windows.h>
int APIENTRY WinMain(HINSTANCE hInstance,