32bit vs 64 bit in structure and union

Hi,
I have question on union assignment.
In the below code,I write one_ascii but I have copied the value in r.four_int[0] and r.four_int[1].Does r.one_ascii contain r.four_int value as it is union and allocate memory for r.one_ascii.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include<iostream>
    #include<stdint.h>
    #include<stdio.h>
    using namespace std;
    static  void  reverse (int len, char *buf)
    {
      char  tmp[24];
      int  i;
    
      for (i = 0; i < len; i++) tmp[i] = buf[i];
      for (i = 0; i < len; i++) buf[i] = tmp[ (len - 1) - i];
    }
    inline  void  reverse (int len, unsigned char *s)  { reverse (len, (char *) s); }
    inline  void  reverse (int len, int *s)    { reverse (len, (char *) s); }
    
    class  GdsHeader
    {
    public:
      unsigned  short  rlen;
      //int64_t rlen;
      char      record_type;
      char      data_type;
    };
    class  GdsRecord
    {
    public:
      union
        {
        short    *two_int;
        int     *four_int;
        double   *eight_real;
        char     *one_ascii;
        };
     GdsRecord (); // { v = 0; length = 0; }
          ~GdsRecord ()  ;//  { delete v; }
    };
    GdsRecord :: GdsRecord ()
    {
      int length = 4000 * 8;
      one_ascii = new char[length];
    }
    
    GdsRecord :: ~GdsRecord()
    {
      delete [] one_ascii;
    }
    
    class  GdsBlock
    {
    public:
    
      GdsHeader  h;
      GdsRecord  r;
      GdsBlock () {
          h.rlen = 2;
          h.record_type = 16;
          h.data_type = 3;
          r.four_int[0] =1485;
          r.four_int[1] =576;
      }
      void      write (FILE *);
    };
    void GdsBlock::write (FILE *outstr)
    {
      int i;
      char *c,tmp;
      switch(h.data_type)
        {
          case 3: /* FOUR BYTE SIGNED INTEGER */
          for(i = 0; i < 2; i++)
          {
    
              reverse(4,&r.four_int[i]);
          }
          h.rlen = (4 * 2) + 4;
          break;
        default:
          printf("Error: bad record type %d in GdsBlock :: external\n", (int) h.data_type);
          }
    
      i = h.rlen - 4;
      c = (char *) &h.rlen;
      tmp = c[0];
      c[0] = c[1];
      c[1] = tmp;
      printf(" Rlen...%d record_type %d data_type %d",h.rlen,h.record_type,h.data_type);
      cout<<endl;
      //fwrite (&h, 1, sizeof (h), outstr);
      //fwrite (r.one_ascii, 1, i, outstr);
      fprintf(outstr,"%d %d %d",h.rlen,h.record_type,h.data_type);
      fprintf(outstr,"%s",r.one_ascii);
      fclose(outstr);
    }
    
    int main()
    {
        FILE * pFile;
        GdsBlock b;
    
    pFile = fopen("out.txt","w");
        b.write(pFile);
    
         /*Read File */
         FILE * rFile;
         long lSize;
         char buffer[40];
         size_t result;
         char c;
         rFile = fopen ( "out.txt" , "r" );
         if(rFile == NULL)
             printf("Error in  open file");
    #if 1
         else
         {do {
                 c = fgetc (rFile);
               printf("%c",c);
             } while (c != EOF);
         fclose (rFile);
         //delete []buffer;
         }
    #endif
Last edited on
Topic archived. No new replies allowed.