Last active
January 26, 2023 21:37
-
-
Save pavly-gerges/bb618f9f76e0d6e47f2193bf5c36eee3 to your computer and use it in GitHub Desktop.
An example showing GNU `realloc`
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 <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