POV-Ray : Newsgroups : povray.off-topic : A puzzle about C (and probably C++)... Server Time
1 Nov 2024 23:16:57 EDT (-0400)
  A puzzle about C (and probably C++)... (Message 1 to 7 of 7)  
From: Darren New
Subject: A puzzle about C (and probably C++)...
Date: 17 Feb 2011 13:29:45
Message: <4d5d6919$1@news.povray.org>
Under what circumstances does it matter what order the members of a union 
are written in?  When does it make a difference as to which type inside the 
union is declared first?

-- 
Darren New, San Diego CA, USA (PST)
  "How did he die?"   "He got shot in the hand."
     "That was fatal?"
          "He was holding a live grenade at the time."


Post a reply to this message

From: Kevin Wampler
Subject: Re: A puzzle about C (and probably C++)...
Date: 17 Feb 2011 14:14:06
Message: <4d5d737e$1@news.povray.org>
As I recall in older versions of C you could only initialize the first 
element in a union, any remaining space being zero-initialized.  I think 
that if you don't specify a field name in the initialization it still 
sets the value of the first field.  Non totally sure though, since I 
rarely use unions in my code (the POD restriction gets in the way, and 
I'm not writing code where the memory reduction is really important anyway).


On 2/17/2011 10:29 AM, Darren New wrote:
>
> Under what circumstances does it matter what order the members of a
> union are written in? When does it make a difference as to which type
> inside the union is declared first?
>


Post a reply to this message

From: Le Forgeron
Subject: Re: A puzzle about C (and probably C++)...
Date: 17 Feb 2011 14:28:50
Message: <4d5d76f2$1@news.povray.org>
Le 17/02/2011 19:29, Darren New nous fit lire :
> 
> Under what circumstances does it matter what order the members of a
> union are written in?  When does it make a difference as to which type
> inside the union is declared first?
> 
IBM extension: When you use zero-extent member in a union, it must be
the last "field".

e.g.

union Foo {
  int a;
  char b[0];
}

This syntax is supported by gcc too, but it does not care about the
order unless you want to initialise it. (for C99 and after)



typedef union {

  int a;
  long c;
  char b[0];

} Foo;

main()
{
	Foo k={257};
	printf("Hello %d %d\n",k.b[0],k.b[1]);
}


So, the first might not be always important (it is for initialisation at
declaration time), but the last would, on AIX at least.


Post a reply to this message

From: Darren New
Subject: Re: A puzzle about C (and probably C++)...
Date: 17 Feb 2011 14:33:26
Message: <4d5d7806@news.povray.org>
Le_Forgeron wrote:
> IBM extension: When you use zero-extent member in a union, it must be
> the last "field".

OK. A clarification:  When any order of the elements is permissible, when 
does it make a difference what order you declare them in?

Also, obviously, if you can only initialize the first field and there's a 
specific field you want to initialize, it has to be first. (But you're right 
that it has something to do with initialization.)

-- 
Darren New, San Diego CA, USA (PST)
  "How did he die?"   "He got shot in the hand."
     "That was fatal?"
          "He was holding a live grenade at the time."


Post a reply to this message

From: Le Forgeron
Subject: Re: A puzzle about C (and probably C++)...
Date: 17 Feb 2011 16:05:56
Message: <4d5d8db4$1@news.povray.org>
Le 17/02/2011 20:33, Darren New nous fit lire :
> When any order of the elements is permissible, when does it make a
> difference what order you declare them in?

When ? or is it rather "In which (silly) environment ?"
Or did you find something in the C standard ?

There is the standard (well, many: C K&R, C89, C99, gnu99, ... ), and
there is the various implementations...

can you enlight us ?


Post a reply to this message

From: Darren New
Subject: Re: A puzzle about C (and probably C++)...
Date: 17 Feb 2011 19:14:19
Message: <4d5db9db$1@news.povray.org>
Le_Forgeron wrote:
> Or did you find something in the C standard ?

Oh, it's in the C standard, and not really a particularly "silly" 
environment needed.  It's just something I got curious about and looked up once.

I'll post the answer tomorrow morning. :-)

-- 
Darren New, San Diego CA, USA (PST)
  "How did he die?"   "He got shot in the hand."
     "That was fatal?"
          "He was holding a live grenade at the time."


Post a reply to this message

From: Darren New
Subject: Re: A puzzle about C (and probably C++)...
Date: 18 Feb 2011 12:16:05
Message: <4d5ea955$1@news.povray.org>
Darren New wrote:
> I'll post the answer tomorrow morning. :-)

The answer I was *thinking* was when the native bit pattern for a "zero" 
value isn't all zeros for one or more of the basic types.

For example, I worked on an AT&T 3B2 for a while, where the bit pattern for 
NULL was 0x80000000.  So

static union { void* p, long l } x;
static union { long l, void* p } y;

would wind up with different values for x.l and y.l even before any 
assignments.  The same could happen with floats/doubles being first, if 
you're not using IEEE754.

The standard says a static union is initialized to the zero value for the 
first element.  I got curious once and actually tracked it down in the standard.

-- 
Darren New, San Diego CA, USA (PST)
  "How did he die?"   "He got shot in the hand."
     "That was fatal?"
          "He was holding a live grenade at the time."


Post a reply to this message

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