malloc() for struct member

Discussion in 'C Programming' started by googler, Oct 6, 2005.

  1. googler

    googler Guest

    Hello, I have a very simple question that I'm a bit confused about
    right now. Is it OK to allocate memory by malloc() or calloc() for a
    struct member and then call free() on it? For example, I have the code
    below.

    struct mystruct {
    int a;
    char *b;
    int c;
    };

    struct mystruct myobject;

    myobject.b = (char *)malloc(50);
    ....
    ....
    free(myobject.b);

    Is the above code OK, or is there any potential problem with it?

    Thanks!
     
    googler, Oct 6, 2005
    #1
    1. Advertising

  2. "googler" <> writes:
    > Hello, I have a very simple question that I'm a bit confused about
    > right now. Is it OK to allocate memory by malloc() or calloc() for a
    > struct member and then call free() on it? For example, I have the code
    > below.
    >
    > struct mystruct {
    > int a;
    > char *b;
    > int c;
    > };
    >
    > struct mystruct myobject;
    >
    > myobject.b = (char *)malloc(50);
    > ...
    > ...
    > free(myobject.b);
    >
    > Is the above code OK, or is there any potential problem with it?


    The only problem I can see is that you cast the result of malloc().
    This is legal but unnecessary, and can mask errors. Change the line
    to

    myobject.b = malloc(50);

    Other than that, I don't see anything wrong.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Oct 6, 2005
    #2
    1. Advertising

  3. On 5 Oct 2005 20:06:15 -0700, "googler" <> wrote:

    >Hello, I have a very simple question that I'm a bit confused about
    >right now. Is it OK to allocate memory by malloc() or calloc() for a
    >struct member and then call free() on it? For example, I have the code
    >below.
    >
    >struct mystruct {
    > int a;
    > char *b;
    > int c;
    >};
    >
    >struct mystruct myobject;
    >
    >myobject.b = (char *)malloc(50);


    Don't cast the return from malloc. It doesn't help and may cause the
    compiler to suppress a diagnostic you really want to see.
    >...
    >...
    >free(myobject.b);


    This is how you would avoid a memory leak. What prompted the
    question?

    >
    >Is the above code OK, or is there any potential problem with it?
    >
    >Thanks!



    <<Remove the del for email>>
     
    Barry Schwarz, Oct 6, 2005
    #3
  4. googler

    googler Guest

    > What prompted the question?

    A code like the one I gave resulted in a crash. That's why I posted the
    question. My main point was if allocating and freeing memory is OK when
    done on a struct member (I knew it's OK, but the above crash made me a
    bit confused).

    I presume it's a compilation problem since the release build works and
    the crash happens for debug build (which is weird since it is usually
    the other way round).
     
    googler, Oct 6, 2005
    #4
  5. googler

    Alex Fraser Guest

    "googler" <> wrote in message
    news:...
    [snip]
    > I presume it's a compilation problem since the release build works and
    > the crash happens for debug build (which is weird since it is usually
    > the other way round).


    I agree it is usually the other way round, but the same still applies: the
    difference is most likely due to undefined behaviour.

    Alex
     
    Alex Fraser, Oct 6, 2005
    #5
  6. googler

    Flash Gordon Guest

    Alex Fraser wrote:
    > "googler" <> wrote in message
    > news:...
    > [snip]
    >
    >>I presume it's a compilation problem since the release build works and
    >>the crash happens for debug build (which is weird since it is usually
    >>the other way round).

    >
    > I agree it is usually the other way round, but the same still applies: the
    > difference is most likely due to undefined behaviour.


    To expand on what Alex said (which I agree with), I would strongly
    suggest that you track down and fix the bug. It might not be causing a
    crash in the release build, but it could well be corrupting something
    under some conditions leading to incorrect results, for example raising
    millions of invoices for 139.90 instead of 149.90 and loosing some
    company lots of money. Or you might be lucky and it crashes on your
    customers instead of corrupting their vital data.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Oct 6, 2005
    #6
  7. googler

    KJ Guest

    googler wrote:
    > Hello, I have a very simple question that I'm a bit confused about
    > right now. Is it OK to allocate memory by malloc() or calloc() for a
    > struct member and then call free() on it? For example, I have the code
    > below.
    >
    > struct mystruct {
    > int a;
    > char *b;
    > int c;
    > };
    >
    > struct mystruct myobject;
    >
    > myobject.b = (char *)malloc(50);

    As Every Body Said Not Need To Type Cast.
    > ...
    > ...
    > free(myobject.b);
    >
    > Is the above code OK, or is there any potential problem with it?

    The code doesn't seems to have problem. But since i m not sure the
    whole code is there or not so it could be some other reason.
    >
    > Thanks!
     
    KJ, Oct 6, 2005
    #7
  8. googler wrote:
    >
    > Hello, I have a very simple question that I'm a bit confused about
    > right now. Is it OK to allocate memory by malloc() or calloc() for a
    > struct member and then call free() on it? For example, I have the code
    > below.
    >
    > struct mystruct {
    > int a;
    > char *b;
    > int c;
    > };
    >
    > struct mystruct myobject;
    >
    > myobject.b = (char *)malloc(50);
    > ...
    > ...
    > free(myobject.b);
    >
    > Is the above code OK, or is there any potential problem with it?


    The above is no different than:

    char *b;
    ...
    b = malloc(50);
    ...
    free(b);

    (Aside from the removal of the unnecessary, and sometimes problematic,
    cast of malloc's return.)

    The fact that the place you store the pointer is within a struct has
    no bearing on how malloc/free work.

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, Oct 6, 2005
    #8
  9. googler

    googler Guest

    Thanks everybody for the help.

    > > myobject.b = (char *)malloc(50);

    > As Every Body Said Not Need To Type Cast.


    Doesn't K&R use type cast with malloc()? Maybe it's not a good
    practice, but I'm wondering that in almost all C codes I've seen, type
    cast has been used with malloc().
     
    googler, Oct 6, 2005
    #9
  10. "googler" <> writes:
    > Thanks everybody for the help.
    >> > myobject.b = (char *)malloc(50);

    >> As Every Body Said Not Need To Type Cast.

    >
    > Doesn't K&R use type cast with malloc()? Maybe it's not a good
    > practice, but I'm wondering that in almost all C codes I've seen, type
    > cast has been used with malloc().


    Yes, but the errata list at
    <http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html> corrects it:

    142(6.5, toward the end): The remark about casting the return
    value of malloc ("the proper method is to declare ... then
    explicitly coerce") needs to be rewritten. The example is correct
    and works, but the advice is debatable in the context of the
    1988-1989 ANSI/ISO standards. It's not necessary (given that
    coercion of void * to ALMOSTANYTYPE * is automatic), and possibly
    harmful if malloc, or a proxy for it, fails to be declared as
    returning void *. The explicit cast can cover up an unintended
    error. On the other hand, pre-ANSI, the cast was necessary, and it
    is in C++ also.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Oct 6, 2005
    #10
  11. googler

    Baxter Guest

    After you free the memory, set the member variable to NULL and see if that
    changes anything.

    When you start getting weird errors/crashes, it's usually worthwhile to look
    for buffer overruns, freeing memory that has already been freed, and things
    like that.

    --
    ---------------------------------------------------------------------
    DataGet & PocketLog www.dataget.com
    Data Collectors www.baxcode.com
    --------------------------------------------------------------------



    "googler" <> wrote in message
    news:...
    > > What prompted the question?

    >
    > A code like the one I gave resulted in a crash. That's why I posted the
    > question. My main point was if allocating and freeing memory is OK when
    > done on a struct member (I knew it's OK, but the above crash made me a
    > bit confused).
    >
    > I presume it's a compilation problem since the release build works and
    > the crash happens for debug build (which is weird since it is usually
    > the other way round).
    >
     
    Baxter, Oct 7, 2005
    #11
    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. Angus Comber
    Replies:
    7
    Views:
    1,179
    Richard Heathfield
    Feb 5, 2004
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,405
    Chris Fogelklou
    Apr 20, 2004
  3. John
    Replies:
    13
    Views:
    716
  4. dutchgoldtony
    Replies:
    15
    Views:
    483
    Mike Wahler
    Nov 16, 2005
  5. pete142
    Replies:
    5
    Views:
    457
    Ben Bacarisse
    Jan 23, 2008
Loading...

Share This Page