Pointer in Union

Hi,
In the current code,We are using pointer of union and assigning value.
class sample
{

union
{
short *two_int;
int *four_int;
double *eight_real;
char *one_ascii;
// void *v;
};
}

Than we assign value in following way.
sample.four_int[0] = (x + xoff); ( x and xoff and y and yoff all are integer)
sample.four_int[1] = (y + yoff);

Than we write data into file. it was working fine into 32 bit machine but it is not working 64bit machine. When I compare data and found that data is divided by 4. For Ex The File generating from 32 bit machine contain 80 than 64 bit
File contain 20.
I am suspecting only this peace of data.Can anyone provide me some input.



closed account (Dy7SLyTq)
why are you using a union?
You skipped the important part: how do you write the data into the file? if you use normal text output, e.g.

1
2
3
4
5
6
7
8
9
10
11
12
std::ostream& operator<<(std::ostream& os, const sample& s)
{
    os << s.tag << ' ';
    switch(s.tag) {
        case sample::SHORT:
            return os << s.four_short[0] << ' ' << s.four_short[1] << '\n';
        case sample::INT:
            return os << s.four_int[0] << ' ' << s.four_int[1] << '\n';
// ...
    }
    return os;
}

then it doesn't matter how many bits your machine has
Adding complete information.

I am working on very legacy code which is migration to 32 bit to 64 bit machine.it generally write file with data which was working fine in 32 bit but having issue in 64 bit machine.When I check the file and found that it is generally contain division of 4 of 32 bit value. I mean if we have 80 value in 32 bit machine than it will be 20 in 64 bit machine.(most case). I have looked into below piece of code and could not understand some of functionality.

1. why we call reverse function before write into File.

2. What is purpose of reverse function.

3. In write function,we always write one_ascii value although we have any data type.


I have tried to took some piece of code which help me to explain issue briefly.Please let
me know if I need to provide more information.





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
    class  GdsHeader
    {
    public:
      unsigned  short  rlen;
      char      record_type;
      char      data_type;
    };
    
    class  GdsRecord
    {
    public:
    #ifndef SWIG
      union
        {
        short    *two_int;
        int     *four_int;
        double   *eight_real;
        char     *one_ascii;
    //    void    *v;
        };
    #endif
      int  length;
    
          GdsRecord (); // { v = 0; length = 0; }
          ~GdsRecord ()  ;//  { delete v; }
      void  len (int l, int type);
    };

    class  GdsRecord
        {
        public:
        #ifndef SWIG
          union
            {
            short    *two_int;
            int     *four_int;
            double   *eight_real;
            char     *one_ascii;
        //    void    *v;
            };
        #endif
          int  length;
        
              GdsRecord (); // { v = 0; length = 0; }
              ~GdsRecord ()  ;//  { delete v; }
          void  len (int l, int type);
        };
      

  
        class  GdsBlock
        {
          bool      valid_block ();
          int      len;
        
        public:
        
          bool      record_unred;
          int      header_ftell;
          GdsHeader  h;
          GdsRecord  r;
          int      array_size;
        //  bool      re_read;
        
        //          GdsBlock () { re_read = false; }
                  GdsBlock () { record_unred = false; header_ftell = 0; }
          void      set (int rt, int dt, int sz)
                    {TBE;
                    h.record_type = rt;    h.data_type = dt;
                    array_size = sz;       r.len (sz, dt);TBX;
                    }
           int      read_header (FILE *);
          void      read_block (FILE *);
        
          void      write (FILE *);
          void      prt ();
        };
        
        
        void    GdsBlock :: write (FILE *outstr)
        {
        
              switch(h.data_type)
              {
                     Case 3:
                       for(i = 0; i < array_size; i++)
                       {
                          cout<<r.four_int[i]<<endl;
                         int *temp = &r.four_int[i];
                        reverse(4,temp);
                 }
             fwrite (r.one_ascii, 1, i, outstr);
        }


        
        

    #ifndef sparc
            static  void  reverse (int len, char *buf)
            {
              TBE;
              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];
              TBX;
            }
            inline  void  reverse (int len, short *s)  { reverse (len, (char *) s); }
            inline  void  reverse (int len, int *s)    {  reverse (len, (char *) s); }
            inline  void  reverse (int len, double *s)  { reverse (len, (char *) s); }
            inline  void  reverse (int len, unsigned char *s)  {reverse (len, (char *) s); }
            #endif 





Last edited on
That reverse() function reverses the order of bytes in an array if "sparc" is not defined (as does the tmp = c[0]; c[0] = c[1]; c[1] = tmp; part which you posted on Stackoverflow: http://stackoverflow.com/questions/17447838 but not here)

And yes, that write() function always writes one_ascii - it's a pretty ghastly design, but since four_int and one_ascii are both pointers to data, and they are members of a union, they always point at the same address (assuming a typical compiler extension that defines reading non-active union members)
Last edited on
I posted the question and got response that it may be endianness issue.But Can anyone provide input that How can I convert 32 bit endianness to 64 bit endianness.
There is no "32 bit endianness" or "64 bit endianness".
Thanks a lot for reply.Does reverse function matter on 32 bit to 64 bit machine becuase of endianness.Do I need to do code change for support on 64 bit in reverse.Just suspecting reverse after looking code multiple time.I am using gcc and not sparc. just try to know your opinion as stuck completely on this issue
gcc version : 4.7
Last edited on
Topic archived. No new replies allowed.