Skip to content

Instantly share code, notes, and snippets.

@RodrigoDornelles
Created February 23, 2023 14:59
Show Gist options
  • Save RodrigoDornelles/8923f7e3d97108d8fde339d9c8d5c56d to your computer and use it in GitHub Desktop.
Save RodrigoDornelles/8923f7e3d97108d8fde339d9c8d5c56d to your computer and use it in GitHub Desktop.
Dangerous memory leakage without compiler warnings in C Language.
#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;
}
@RodrigoDornelles
Copy link
Author

output


5c2eb0	5c2eb0
foo foo
de9057	5c2eb0
FOO foo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment