Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int main() {
- const int SIZE = 10;
- // Allocate memory for the array a.
- int* a = (int*) malloc(SIZE * sizeof(int));
- // Set the value in a
- for (int i = 0; i < SIZE; i++) {
- int j = i + 1;
- a[i] = j * j;
- }
- // Share the address of a to a1
- int* a1 = a;
- // Oh, the memory of a is freed!
- free(a);
- a = NULL;
- /* Now a1 becomes dangling pointer.
- Accessing data via a dangling pointer is
- an undefined behavior. */
- printf("%d\n", a1[3]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement