Last active
June 23, 2024 21:31
-
-
Save matu3ba/1b1edf44fbe50ebd258eda29907beac1 to your computer and use it in GitHub Desktop.
clang c89 ptr to int -Weverything
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <assert.h> | |
#include <inttypes.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
void ptrtointtoptr(void); | |
static void memset_16aligned(void * ptr, char byte, size_t size_bytes, uint16_t alignment) { | |
assert((size_bytes & (alignment-1)) == 0); | |
assert(((uintptr_t)ptr & (alignment-1)) == 0); | |
memset(ptr, byte, size_bytes); | |
} | |
void ptrtointtoptr(void) { | |
const uint16_t alignment = 16; | |
const uint16_t align_min_1 = alignment - 1; | |
void * mem = malloc(1024+align_min_1); | |
void *ptr = (void *)((((uint64_t)mem)+align_min_1) & ~(uint64_t)align_min_1); | |
printf("0x%08" PRIXPTR ", 0x%08" PRIXPTR "\n", (uintptr_t)mem, (uintptr_t)ptr); | |
memset_16aligned(ptr, 0, 1024, alignment); | |
free(mem); | |
} | |
int main(void) { ptrtointtoptr(); } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
misterspoon@pc ~/d/t/tryc> clang -std=c89 -Weverything ptrtoint_inttoptr.c && ./a.out | |
0x623AA76542A0, 0x623AA76542A0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment