Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Memory allocation questions

Reply
Thread Tools

Memory allocation questions

 
 
Eirik WS
Guest
Posts: n/a
 
      02-07-2004
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?
 
Reply With Quote
 
 
 
 
Johan Lindh
Guest
Posts: n/a
 
      02-07-2004
Eirik WS wrote:

> 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.
 
Reply With Quote
 
 
 
 
Christian Bau
Guest
Posts: n/a
 
      02-07-2004
In article <kqn30c.u75.ln@192.168.0.1>,
Eirik WS <(E-Mail Removed)> wrote:

> 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.
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      02-08-2004

"Eirik WS" <(E-Mail Removed)> wrote in message
news:kqn30c.u75.ln@192.168.0.1...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
An idea for heap allocation at near stack allocation speed Bjarke Hammersholt Roune C++ 14 03-06-2011 08:07 AM
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM
Dynamic memory allocation and memory leak... s.subbarayan C Programming 10 03-22-2005 02:48 PM
memory allocation questions (newbie) Stephen Ramsay C Programming 18 07-04-2004 05:12 PM



Advertisments