Created
February 23, 2023 14:59
-
-
Save RodrigoDornelles/8923f7e3d97108d8fde339d9c8d5c56d to your computer and use it in GitHub Desktop.
Dangerous memory leakage without compiler warnings in C Language.
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> | |
int main() | |
{ | |
char* foo = malloc(4); | |
char* old_foo = foo; | |
foo[0] = 'f'; | |
foo[1] = 'o'; | |
foo[2] = 'o'; | |
foo[3] = '\0'; | |
printf("\n%lx", (unsigned long) foo); | |
printf("\t%lx", (unsigned long) old_foo); | |
printf("\n%3s %3s", foo, old_foo); | |
foo = "FOO"; ///< Memory leak | |
printf("\n%lx", (unsigned long) foo); | |
printf("\t%lx", (unsigned long) old_foo); | |
printf("\n%3s %3s", foo, old_foo); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output