Re: malloc() for struct member
- From: Barry Schwarz <schwarzb@xxxxxxxxx>
- Date: Wed, 05 Oct 2005 21:15:26 -0700
On 5 Oct 2005 20:06:15 -0700, "googler" <pinaki_m77@xxxxxxxxx> 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>>
.
- Follow-Ups:
- Re: malloc() for struct member
- From: googler
- Re: malloc() for struct member
- References:
- malloc() for struct member
- From: googler
- malloc() for struct member
- Prev by Date: Re: malloc() for struct member
- Next by Date: Re: Which numbers evaluate to true and false?
- Previous by thread: Re: malloc() for struct member
- Next by thread: Re: malloc() for struct member
- Index(es):
Relevant Pages
|