problem with <bitset> and reinterpret_cast

Hi! Here's some code...

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <cstdlib>
#include <iostream>
#include <bitset>
#include <fstream>

#define RANDOM_A      (1103515245)
#define RANDOM_C      (12345)
#define RANDOM_SHIFT  (16)

#define MAX_UNSIGNED_SHORT  (0xffff)
#define MAX_UNSIGNED_LONG   (0xffffffff)

const unsigned char NOMBRE_DE_BIT_DANS_UN_BYTE = 8;
const unsigned char TAILLE_CHAR = sizeof(unsigned char);

const unsigned long TAILLE_DES_BLOCS_MEMOIRE = 1024*1024;
const unsigned short NOMBRE_DE_BLOC_MEMOIRE = (MAX_UNSIGNED_LONG + 1) / (TAILLE_DES_BLOCS_MEMOIRE * NOMBRE_DE_BIT_DANS_UN_BYTE);

const unsigned char POSITION_DEBUT_FICHIER = 0;
const unsigned char OFFSET_DE_FIN_DE_FICHIER = 0;

const unsigned long TAILLE_DES_TRANCHES_DE_MEMOIRE_SUR_LE_DISQUE = TAILLE_DES_BLOCS_MEMOIRE * NOMBRE_DE_BLOC_MEMOIRE;

using namespace std;

int main(int argc, char *argv[])
{
    char* monTableau = new char[TAILLE_DES_BLOCS_MEMOIRE];
    
    for(unsigned long i = 0; i < TAILLE_DES_BLOCS_MEMOIRE; i++)
    {
        monTableau[i] = 0;
    }
    
    fstream monFichier("data.bin", ios::in | ios::out | ios::binary);
    
    unsigned long long offset = monFichier.tellg();
    
    for(unsigned short i = 0; i < NOMBRE_DE_BLOC_MEMOIRE; i++)
    {
        monFichier.write(monTableau, TAILLE_DES_BLOCS_MEMOIRE);
    }
    
    delete[] monTableau;
    
    monTableau = NULL;
    
    bitset<NOMBRE_DE_BIT_DANS_UN_BYTE> monBitSet;
    
    bool nEstPasPasseParLa;
    
    unsigned long long nombreDeBitsModifies = 0;
    
    unsigned char nombreDeTranchesMemoiresConsecutivesSurLeDisque = 1;
    
    bool trancheMemoireNonAjoutee;
    
    for(unsigned long m_ulRandomSeed = 0; nombreDeBitsModifies <= MAX_UNSIGNED_LONG; m_ulRandomSeed++)
    {
        if(monTableau)
        {
        }
        else
        {
            monTableau = new char;
        }
        
        nEstPasPasseParLa = true;
        
        for(unsigned char i = 0; i < nombreDeTranchesMemoiresConsecutivesSurLeDisque; i++)
        {
            monFichier.seekg(offset + i*TAILLE_DES_TRANCHES_DE_MEMOIRE_SUR_LE_DISQUE + m_ulRandomSeed / NOMBRE_DE_BIT_DANS_UN_BYTE);
            monFichier.read(monTableau, TAILLE_CHAR);
            
            monBitSet = reinterpret_cast<bitset<NOMBRE_DE_BIT_DANS_UN_BYTE>*>(monTableau);
        
            if(monBitSet[m_ulRandomSeed % NOMBRE_DE_BIT_DANS_UN_BYTE])
            {
                nEstPasPasseParLa = false;
            }
        }
        
        if(nEstPasPasseParLa)
        {
            monBitSet[m_ulRandomSeed % NOMBRE_DE_BIT_DANS_UN_BYTE] = 1;
            
            delete monTableau;
            monTableau = reinterpret_cast<char*>(&monBitSet);
            
            monFichier.seekp(offset + m_ulRandomSeed / NOMBRE_DE_BIT_DANS_UN_BYTE);
            monFichier.write(monTableau, TAILLE_CHAR);
            
            nombreDeBitsModifies++;
        }
        
        while(nEstPasPasseParLa)
        {
            m_ulRandomSeed = (RANDOM_A * m_ulRandomSeed) + RANDOM_C;
            
            trancheMemoireNonAjoutee = true;
            
            for(unsigned char i = 0; i < nombreDeTranchesMemoiresConsecutivesSurLeDisque && trancheMemoireNonAjoutee; i++)
            {
                monFichier.seekg(offset + i*TAILLE_DES_TRANCHES_DE_MEMOIRE_SUR_LE_DISQUE + m_ulRandomSeed / NOMBRE_DE_BIT_DANS_UN_BYTE);
                monFichier.read(monTableau, TAILLE_CHAR);
            
                monBitSet = reinterpret_cast<bitset<NOMBRE_DE_BIT_DANS_UN_BYTE>*>(monTableau);
            
                if(monBitSet[m_ulRandomSeed % NOMBRE_DE_BIT_DANS_UN_BYTE])
                {
                    nEstPasPasseParLa = false;
                    
                    delete monTableau;
                    monTableau = new char[TAILLE_DES_BLOCS_MEMOIRE];
                    
                    for(unsigned long i = 0; i < TAILLE_DES_BLOCS_MEMOIRE; i++)
                    {
                        monTableau[i] = 0;
                    }
                    
                    monFichier.seekp(OFFSET_DE_FIN_DE_FICHIER, ios::end);
                    
                    for(unsigned short i = 0; i < NOMBRE_DE_BLOC_MEMOIRE; i++)
                    {
                        monFichier.write(monTableau, TAILLE_DES_BLOCS_MEMOIRE);
                    }
                    
                    nombreDeTranchesMemoiresConsecutivesSurLeDisque++;
                
                    delete[] monTableau;
                
                    monTableau = NULL;
                    
                    trancheMemoireNonAjoutee = false;
                }
            }
            
            if(nEstPasPasseParLa)
            {
                monBitSet[m_ulRandomSeed % NOMBRE_DE_BIT_DANS_UN_BYTE] = 1;
                
                delete monTableau;
                monTableau = reinterpret_cast<char*>(&monBitSet);
                
                monFichier.seekp(offset + m_ulRandomSeed / NOMBRE_DE_BIT_DANS_UN_BYTE);
                monFichier.write(monTableau, TAILLE_CHAR);
                
                nombreDeBitsModifies++;
            }
        }
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


I have problems with lines 75 and 107, with which I get a "non-lvalue in assignment" error. Everything else seems just fine... I might know what it is but I thought you guys could give us a hint on how to use reinterpret_cast properly.

Thanks,
AeonFlux
You can not do what you are trying to do. The compilation error is a syntax error (you need to reference the pointer), but the real problem is that you're trying to reinterpret_cast with non-POD types.

AeonFlux1212 wrote:
how to use reinterpret_cast properly.
Impossible - by design, there is no proper way to use it.
monBitSet is not a pointer.
Yes you guys are right I think I fixed it ... I made monBitSet a pointer to a new bitset<NOMBRE_DE_BIT_DANS_UN_BYTE>.... the rest follows...

Thanks a lot!
AeonFlux




... L B what is a non-POD type?
Hello again... after a lot of testing and debugging I figured out that casting a char* to a bitset<>* and vice-versa simply doesn't work... like L B mentionned earlier... I mean it works, but you can't really use the results like I wanted to use them.

So I had to make my own charTObits and bitsTOchar functions, and now everything seems to work just fine... :-)

Thanks for the help, reply if you have any questions.

AeonFlux
Topic archived. No new replies allowed.