I'm having a little trouble understanding this issue of memory management.
Wouldn't the freeing of p2 free p1 there for if I attempt to print p1 it the program should segfault or not print that "data"?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char *p1;
p1 = (char *)malloc(sizeof(char) * 2);
strcpy(p1, "R");
char *p2;
p2 = p1;
printf("p1 address = %p, p1 data = %s; p2 address = %p, p2 data = %s\n", p1, p1, p2, p2);
free(p2);
// this should not print?
// should segfault if p2's address is the same as p1's and if I freed p2 wouldn't that essentially free p1?
// hrmmmmmmmmmmm......
printf("%p -> %s\n", p1, p1);
return 0;
}

New Topic/Question
Reply




MultiQuote









|