Quickies

[categories] [index] [all (527)] [latest]

C
  1. int n = 1;
    int isLittleEndian = (*(char *)&n == 1);
    
  2. // $ gcc -m32 -fasm-blocks asm.c -o asm
    
    #include <stdio.h>
    
    int main (int argc, char *argv[]) {
    
        int espValue;
    
        _asm {
              mov [espValue], esp;
        }
    
        printf("espValue: %u\n", espValue);
    
        return 0;
    }
    
  3. void x() {
        void *returnAddress = __builtin_return_address(0);
        printf("%p\n", returnAddress);
    }
    

    The level argument is number of frames to scan up the call stack.

    See http://gcc.gnu.org/onlinedocs/gcc/Return-Address.html.

  4. int a[3]= { 1, 2, 3 };
    int len = sizeof(a)/sizeof(*a);