Memory allocation questions

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?
 
J

Johan Lindh

Eirik said:
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?

No.
 
C

Christian Bau

Eirik WS said:
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?

This code invokes undefined behavior by accessing to->x which has an
indeterminate value. In your case, the effect of the undefined behavior
was apparently that to->x held the value that was previously stored into
ein->x which is quite unfortunate. If you had been more lucky then your
program would have crashed; that would have told you immediately that
(1) no, it is not portable at all and (2) no, you can't rely on it, you
can't even be sure that your program doesn't crash immediately.
 
M

Mike Wahler

Eirik WS said:
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;

The expression 'to->x' invokes undefined behavior via
the dereference of an indeterminate pointer value.
printf("%d\n", to->y);

More undefined behavior (the value of 'to' is indeterminate).
free(to);
return 0;
}

You can see what happens.

Because the behavior is undefined, *anything* can happen,
which might be able to be 'seen' or not.
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.

OK so far.
Then I allocate memory for the second structure and
assign the value to->x - 0 to it.

KABOOM! Undefined behavior. Dereference of indeterminate pointer value.
On my computer(x86, GNU/Linux), the printf says 13.

I doesn't have to. It could do anything at all.
Is this code portable?

It's invalid.
Should I rely on it preserving the value
of the free'd structure?

Absolutely not. Perhaps the second 'malloc()' reused the same
memory as the first used. But that's only by 'accident'.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top