Memory allocation questions

Discussion in 'C Programming' started by Eirik WS, Feb 7, 2004.

  1. Eirik WS

    Eirik WS Guest

    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?
     
    Eirik WS, Feb 7, 2004
    #1
    1. Advertising

  2. Eirik WS

    Johan Lindh Guest

    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.
     
    Johan Lindh, Feb 7, 2004
    #2
    1. Advertising

  3. In article <kqn30c.u75.ln@192.168.0.1>,
    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?


    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.
     
    Christian Bau, Feb 7, 2004
    #3
  4. Eirik WS

    Mike Wahler Guest

    "Eirik WS" <> 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
     
    Mike Wahler, Feb 8, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephen Ramsay

    memory allocation questions (newbie)

    Stephen Ramsay, Jul 2, 2004, in forum: C Programming
    Replies:
    18
    Views:
    519
    Herbert Rosenau
    Jul 4, 2004
  2. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    707
    Eric Sosman
    Mar 22, 2005
  3. Ken
    Replies:
    24
    Views:
    3,876
    Ben Bacarisse
    Nov 30, 2006
  4. chris
    Replies:
    6
    Views:
    993
    chris
    Oct 28, 2005
  5. Bjarke Hammersholt Roune
    Replies:
    14
    Views:
    1,193
    Bjarke Hammersholt Roune
    Mar 6, 2011
Loading...

Share This Page