E
Eirik WS
Consider the following code:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int x;
int y;
} tals;
int main(void) {
tals *ein, *to;
ein = malloc(sizeof(tals));
ein->x = 13;
free(ein);
to = malloc(sizeof(tals));
to->y = to->x - 0;
printf("%d\n", to->y);
free(to);
return 0;
}
You can see what happens. I declare two pointers to a
tals structure. I allocate memory for the first, assign its x
field to a value of 13 and free it.
Then I allocate memory for the second structure and
assign the value to->x - 0 to it.
On my computer(x86, GNU/Linux), the printf says 13.
Is this code portable? Should I rely on it preserving the value
of the free'd structure?
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int x;
int y;
} tals;
int main(void) {
tals *ein, *to;
ein = malloc(sizeof(tals));
ein->x = 13;
free(ein);
to = malloc(sizeof(tals));
to->y = to->x - 0;
printf("%d\n", to->y);
free(to);
return 0;
}
You can see what happens. I declare two pointers to a
tals structure. I allocate memory for the first, assign its x
field to a value of 13 and free it.
Then I allocate memory for the second structure and
assign the value to->x - 0 to it.
On my computer(x86, GNU/Linux), the printf says 13.
Is this code portable? Should I rely on it preserving the value
of the free'd structure?