Skip to content

Instantly share code, notes, and snippets.

@Scrappers-glitch
Last active January 26, 2023 21:37
Show Gist options
  • Save Scrappers-glitch/bb618f9f76e0d6e47f2193bf5c36eee3 to your computer and use it in GitHub Desktop.
Save Scrappers-glitch/bb618f9f76e0d6e47f2193bf5c36eee3 to your computer and use it in GitHub Desktop.
An example showing GNU `realloc`
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
int main() {
// allocating memory for 6 integers (total = 6 * 4 = 24 bytes)
int* ptr = malloc(6 * sizeof(int));
for (int i = 0; i < 6; i++) { /* 6 * 4 = 24 bytes = 24 * 8 = 192 bits */
ptr[i] = INT32_MAX;
}
ptr = realloc(ptr, 10 * sizeof(int));
for (int i = 5; i < 10; i++) {
ptr[i] = 31;
}
printf("%lu\n", ptr[0]);
printf("%lu\n", ptr[10]);
printf("%lu\n", ptr[11]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment