Union and malloc

2162 views
Skip to first unread message

Jerome Lambourne

unread,
Aug 22, 1998, 3:00:00 AM8/22/98
to
If I have a Union with an int and a char* inside. How do I malloc
memory for the char*?? Do I have to malloc the whole union?
And if the Union is part of a structure, does that complicate things?

Jerome

Mark Brown

unread,
Aug 23, 1998, 3:00:00 AM8/23/98
to
Jerome Lambourne <moon...@moonsteel.ml.org> writes:

Just allocate memory for the union or structure. For example

union foo {
int x;
char *y;
};

union foo *ptr = malloc(sizeof(union foo));

(or sizeof(*ptr) if you prefer that style). If you want the char * to
point at something you will have to arrange for the memory for that to
be allocated and deallocated seperately.

--
Mark Brown mailto:bro...@tardis.ed.ac.uk (Trying to avoid grumpiness)
http://www.tardis.ed.ac.uk/~broonie/
EUFS http://www.eusa.ed.ac.uk/societies/filmsoc/

Lawrence Kirby

unread,
Aug 23, 1998, 3:00:00 AM8/23/98
to
In article <35DF4C86...@moonsteel.ml.org>
moon...@moonsteel.ml.org "Jerome Lambourne" writes:

>If I have a Union with an int and a char* inside. How do I malloc
>memory for the char*?? Do I have to malloc the whole union?

Yes.

>And if the Union is part of a structure, does that complicate things?

Not really. In that case when you allocate the structure you allocate
memory for all of its members including the union. Therefore memory is
allocated to hold each of the union's members (one at a time). The only
issue is pointers. Allocating memory for the pointer (e.g. as the
union member above) doesn't allocate memory for the pointer to point to.
You need to do that as a distinct step an initialise the pointer
appropriately before you use it.

--
-----------------------------------------
Lawrence Kirby | fr...@genesis.demon.co.uk
Wilts, England | 7073...@compuserve.com
-----------------------------------------


Alex...@scitex.com

unread,
Aug 23, 1998, 3:00:00 AM8/23/98
to
In article <35DF4C86...@moonsteel.ml.org>,

Jerome Lambourne <moon...@moonsteel.ml.org> wrote:
> If I have a Union with an int and a char* inside. How do I malloc
> memory for the char*?? Do I have to malloc the whole union?
> And if the Union is part of a structure, does that complicate things?

union foo {
int i;
char *chptr;
};
struct bar {
union foo f;
...
};

.....
union foo *fooptr = malloc(sizeof *fooptr);
/* Dynamically allocated object of type union foo */
/* But I think your question is not about this... */
fooptr->chptr = malloc(10);
/* Now chptr points to dynamically allocated array of 10 chars */
/* That was your problem? */
struct bar *barptr = malloc(sizeof *barptr);
/* dynamically allocated object of type struct bar;
this struct contains union foo */
barptr->f.chptr = malloc(10);
/* Now chptr of an union foo inside struct bar points to
dynamivally allocated array of 10 chars */

free(barptr->f.chptr);
free(barptr);
free(fooptr->chptr);
free(fooptr);

--
Regards,
Alex Krol
Disclaimer: I'm not speaking for Scitex Corporation Ltd

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum

Ben Pfaff

unread,
Aug 28, 1998, 3:00:00 AM8/28/98
to
Jerome Lambourne <moon...@moonsteel.ml.org> writes:

If I have a Union with an int and a char* inside. How do I malloc
memory for the char*??

malloc (sizeof (char *))



Do I have to malloc the whole union?

I don't think so.

And if the Union is part of a structure, does that complicate things?

You can't separately allocate the union within a structure. If, on
the other hand, the union within the structure is really a union *,
it's not an issue.

An example of what you are trying to do would help.
--
(supporter of the campaign for grumpiness where grumpiness is due in c.l.c)

Please: do not email me copies of your posts to comp.lang.c
do not ask me C questions via email; post them instead

Chris Torek

unread,
Aug 28, 1998, 3:00:00 AM8/28/98
to
In article <35DF4C86...@moonsteel.ml.org> Jerome Lambourne
<moon...@moonsteel.ml.org> asks a question that seems ambiguous,
but could be read as: `if I have an object of type "pointer to
union U", where union U contains as one of its members an object
of type "pointer to char",

>>Do I have to malloc the whole union?

`Or can I get away with something smaller?'

In article <87zpcpi...@dial1.msu.edu> Ben Pfaff <pfaf...@pilot.msu.edu>
answers:


>>Do I have to malloc the whole union?
>I don't think so.

The question has, I think, come up before in comp.std.c, where the
answer was essentially "you have to allocate the whole thing". An
example:

union u {
char *cp;
char buf[1048576]; /* 1 MB */
};

In this case, sizeof(union u) is likely to be 1048576 (because
sizeof(char) is likely to be far less than 1048576 and there is
probably no padding). One might think, then, that:

union u *up = malloc(sizeof(up->cp));
/* ... code that only ever uses up->cp, never up->buf ... */

is okay, and it certainly would save on memory (why allocate 1 MB
when we only want something like 4 or 8 bytes?). Unfortunately,
apparently compilers are at liberty to do weird things that involve
assuming a non-NULL "up" points to at least 1 MB.

I know of no actual compilers on which allocating only "sizeof(char *)"
will cause a failure, but one should at least be aware of the thin
ice, as K&R put it.
--
In-Real-Life: Chris Torek, Berkeley Software Design Inc
El Cerrito, CA Domain: to...@bsdi.com +1 510 234 3167
Antispam notice: unsolicited commercial email will be handled at my
consulting rate; pyramid-scheme mail will be forwarded to the FTC.

Reply all
Reply to author
Forward
0 new messages