Wiki Roadmap Timeline Tickets New Ticket Source Search Help / Guide About Trac Login

Ticket #475 (new defect)

Opened 8 months ago

Last modified 8 months ago

64-bit unions are not the same size as C language unions in some situations

Reported by: bll Assigned to: lindquist
Priority: major Milestone:
Component: backend Version: hg tip
Keywords: 64bit union C Cc:

Description

Works on 32bit.

** C:

#include <stdio.h>
#include <stdlib.h>

union a {
  long double a1;
  char        ac[20];
};

int
main (int argc, char *argv[]) {
  union a a;
  printf ("C\n");
  printf ("sizeof a: %d\n", sizeof (a));
  printf ("sizeof a.a1: %d\n", sizeof (a.a1));
  printf ("sizeof a.ac: %d\n", sizeof (a.ac));
  printf ("offsetof a.ac: %d\n", (int) ((char *) &a.ac - (char *) &a.a1));
  return 0;
}

bll-desktop:bll$ cc -o x1c x1.c
bll-desktop:bll$ ./x1c
C
sizeof a: 32
sizeof a.a1: 16
sizeof a.ac: 20
offsetof a.ac: 0

** D2:

import std.stdio;

union ua {
  real     a1;
  char     ac[20];
};

int
main (string argv[]) {
  ua a;
  writefln ("D");
  writefln ("sizeof a: %d", a.sizeof);
  writefln ("sizeof a.a1: %d", a.a1.sizeof);
  writefln ("sizeof a.ac: %d", a.ac.sizeof);
  writefln ("offsetof a.ac: %d", a.ac.offsetof);
  return 0;
}

bll-desktop:bll$ ldc2 -d x1.d
bll-desktop:bll$ ./x1
D
sizeof a: 24
sizeof a.a1: 16
sizeof a.ac: 20
offsetof a.ac: 0
bll-desktop:bll$ ldc2 --version
LLVM D Compiler LDC trunk
based on DMD v2.052 and LLVM 2.9

** D1:

alias char[] string;
import io.Stdout;

union ua {
  real     a1;
  char     ac[20];
};

int
main (string argv[]) {
  ua a;
  Stdout ("D1\n");
  Stdout ("sizeof a: ", a.sizeof, "\n");
  Stdout ("sizeof a.a1: ", a.a1.sizeof, "\n");
  Stdout ("sizeof a.ac: ", a.ac.sizeof, "\n");
  Stdout ("offsetof a.ac: ", a.ac.offsetof, "\n");
  return 0;
}

bll-desktop:bll$ ldc x2.d
bll-desktop:bll$ ./x2
D1
sizeof a: , 24, 
sizeof a.a1: , 16, 
sizeof a.ac: , 20, 
offsetof a.ac: , 0,
bll-desktop:bll$ ldc --version
LLVM D Compiler LDC trunk
based on DMD v1.067 and LLVM 2.9

I used:

hg clone 'https://bitbucket.org/lindquist/ldc'

git clone 'https://github.com/AlexeyProkhin/phobos'

git clone 'https://github.com/AlexeyProkhin/druntime'

svn co 'http://svn.dsource.org/projects/tango/tags/releases/0.99.9' tango

patch -p0 -d tango/ < tango-0.99.9.patch

Change History

09/18/11 15:32:25 changed by bll

  • component changed from frontend (D2 only) to backend.
Copyright © 2008, LDC Development Team.