🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #510

Re: AVL tree insertion - programming exercise

#510
From: Waldek Hebisch
Date: Sun, 18 Oct 2026 15:12
41 lines
3172 bytes
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
> On 2026-10-18 10:42, Waldek Hebisch wrote:
>>> [...]
>> 
>> I read the above as: "can access part of object that fits in allocated
>> space".  That is important for structs and unions: you clearly do not
>> want to allocate memory for biggest union variant.  [...]
> 
> Mind to explain that opinion?
> 
> If I define a (C-)'union' type I definitely want to store anything
> possible in those unions, changing the value to another type as
> necessary. - Mind the 'sizeof' of a union is [at least?] the size
> of its largest component. - If I 'malloc' a union I use that size.
> In cases where I want to use just one [smaller] component-type of
> a union I just allocate that specific type not the union-type.
> 
> But maybe you meant something different?

I meant what I wrote.  C type system is too weak to give exact
type for more complicated data structure.  Instead, real
program give approximate type and depend on holes in type
system.  So you have pointer to union and program is structured
so that it only accesses valid variant.  Allocating memory
for whole using may waste a lot of memory and does not buy you
any real safety: accessing wrong variant is likely to lead to
troubles regardless of amount of memory that you allocated.
And in case of interesting data structures variant does not
change after creation (allocation).  If you need different
variant you simply allocate new piece of memory.

BTW: Pascal 'new' for variant records takes extra argument specifying
requested variant.  Corresponding pointer has type 'pointer to
variant record', but legally can only be used with the variant
specified in call to 'new'.  In C you get equivalent effect (with
essentially the same conditions) by allocating amount of memory
needed for specific variant of union, but assigning 'malloc'
result to pointer to union.

-- 
                              Waldek Hebisch
Technical Details
Message-ID: <11b2njv$3rvef$2@paganini.bofh.team>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!newsfeed.bofh.team!paganini.bofh.team!not-for-mail
References: <86ece2kppv.fsf@linuxsc.com> <11amvh2$14pou$3@kst.eternal-september.org> <11ao2mr$4l9$1@reader2.panix.com> <11ap0fo$1t8gq$2@kst.eternal-september.org> <11aqnp0$sb$1@reader2.panix.com> <11arlj9$2q39c$1@kst.eternal-september.org> <20261015163445.76@kylheku.com> <11b20os$3qlbh$1@paganini.bofh.team> <11b29gv$3pvda$3@dont-email.me>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!newsfeed.bofh.team!paganini.bofh.team!not-for-mail
From: antispam@fricas.org (Waldek Hebisch)
Newsgroups: comp.lang.c
Subject: Re: AVL tree insertion - programming exercise
Date: Sun, 18 Oct 2026 15:12:01 -0000 (UTC)
Organization: bofh.team Usenet service
Message-ID: <11b2njv$3rvef$2@paganini.bofh.team>
References: <86ece2kppv.fsf@linuxsc.com> <11amvh2$14pou$3@kst.eternal-september.org> <11ao2mr$4l9$1@reader2.panix.com> <11ap0fo$1t8gq$2@kst.eternal-september.org> <11aqnp0$sb$1@reader2.panix.com> <11arlj9$2q39c$1@kst.eternal-september.org> <20261015163445.76@kylheku.com> <11b20os$3qlbh$1@paganini.bofh.team> <11b29gv$3pvda$3@dont-email.me>
Injection-Date: Sun, 18 Oct 2026 15:12:01 -0000 (UTC)
Injection-Info: paganini.bofh.team; logging-data="4062671"; mail-complaints-to="usenet@bofh.team"; posting-host="d028f9ad2fa2c51f22dbfed0d81d7daf"; posting-account="69e66f1681224ab92235b464ce7a90b6"
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (Linux/6.1.0-9-amd64 (x86_64))
X-Postfilter: Postfilter-NG 2026.10.2