POV-Ray : Newsgroups : povray.off-topic : Programming language discussion : Re: Programming language discussion Server Time
3 Sep 2024 17:14:24 EDT (-0400)
  Re: Programming language discussion  
From: Warp
Date: 22 Oct 2010 13:16:54
Message: <4cc1c706@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> Warp wrote:
> >   'union' in C is basically a construct which exists to save some bytes
> > of memory in some rare circumstances.

> I have also seen it used for typecasting, but nowadays I think casting the 
> address of the thing to the "wrong" pointer type and then indirecting it 
> gets used more.  I.e.;  { long x = ...; float f = *(float*)&x; }

  The most clever use of 'union' I have seen was like this:

union Matrix4x4
{
    double m[4][4];
    struct
    {
        double m11, m12, m13, m14,
            m21, m22, m23, m24,
            m31, m32, m33, m34,
            m41, m42, m43, m44;
    };
};

  This means you can access the matrix like "transform.m[1][2] = 5;"
or like "transform.m23 = 5;";

  Of course in this case this is just a trick to have two naming
conventions to access the same data (rather than to save memory).

> >   In Objective-C you usually don't have to check for null pointers. 

> That's the other way to handle it. Or to throw an exception which you can 
> catch.  The Obj-C way certainly sounds more ... PHPish. ;-)

  It's actually really handy in Objective-C when developing for the iPhone.

  You could have a member which points to some screen element, such as eg.
a label or an image, and you can write code which handles that element
(eg. hides/unhides it, or updates its text to show some info, and of course
when the owner dies, it has to release the object). However, when editing
the GUI of the program with Interface Builder, you can simply remove that
screen element without the program crashing due to a null pointer.

  (There are exceptions to this, of course, ie. situations where a null
pointer is invalid and will cause eg. a 'invalid parameter' exception at
runtime, but in many situations null pointers will be quite "transparent".)

-- 
                                                          - Warp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.