|
Not quite. Read the other replys.
Each item in the union STARTS at the same place. They are used either when you know that of two different variables only one is needed (but which one is not known in advance), or when you want to use the same data in different ways. In your case, field takes 32 bits, on a 32 bit system. x represents bit 1 of field, y represents bit 2, and space represents 4-6.
if you do:
struct somestruct mystruct;
mystruct.field = 0xa8000000;
than field (dropping the last 24 bits which are all 0) contains the following in binary:
10101000
Therefore
mystruct.x == 1
mystruct.y == 0
mystruct.z == 1
mystrict.space = 2
(Note, double check this, I might have the layout backwards. I cannot remember this unless I'm deep into such code)
Bitfields almost always make your program larger and slower than just using an int. Therefore the only time you normally see them is in networking and hardware. Both are cases where limits in transfer speed or length are far more important that CPU time or memory. It is generally not worth your time to use them elsewhere.
Warning, whenever you see data structures like this, the author knew something about the exact hardware in question! There are dragons lurking for anyone who trys to run this on anything else without a lot of care. If you do not understand what I'm getting at, stick to less advanced programs. Many programers get by without ever needing to deal with bitfiels.
|
|
|
|
|
Your question poses a number of interesting points
I am guessing that this is making use of Anonymous Unions.
Yes. An anonymous union is just a way to tell the compiler that the enclosed members share the same storage. For instance:
union{
int x;
int y;
}
x=100;
cout<<y; Here, we've used an anonymous union to locally declare two variables sharing the same position in memory. Most of the time, anonymous unions are used for type definition inside a struct :
struct foo{
union{
int x;
int y;
};
};
foo f;
f.x=100;
cout<<f.y; Also I don't understand this notation "unsigned x: 1;", what is this saying?
This is an orthogonal feature called bitfields. By adding a bitfield specification to a member, we instruct the compiler to use the indicated bits for storage instead of what it normally would take to store the member. For instance:
struct foo{
unsigned x:2;
unsigned y:2;
unsigned z:2;
unsigned w:2;
}; Here, member variables x , y , z and w will take only two bits each, and the resulting foo will only occupy one byte (alignment issues aside.) Of course, these variables will not be able to hold a value greater than what can be expressed with two bits (3) --the compiler cannot do miracles.
What do we have if we combine these two features?
struct SomeStruct{
union{
unsigned int field;
struct{
unsigned x: 1;
unsigned y: 1;
unsigned z: 1;
unsigned space: 3;
};
};
}; By virtue of the anonymous union, field will share the same storage as x , y , y and space . And because we are using bitfields on those, the overall effect is that SomeStruct.x will refer to the first bit of SomeStruct.field , SomeStruct.y to the second bit, etc. Got it? This kind of constructs (which are very low-level and depend on non-portable issues like type alignment, size of primitive types, big- or little-endiannness, etc.) are commonly used to reproduce bit-specified data structures like those you can find in some data protocols (for instance, TCP data units), and allow the programmer to handle the data at the sub-byte level.
Finally, is this C/C++ or just for C++?
Neither This construct relies on what we could call an "anonymous struct ":
struct{
unsigned x: 1;
unsigned y: 1;
unsigned z: 1;
unsigned space: 3;
}; Alas, anonymous struct s are not standard C or C++, though MSVC admits them. Apart from this, the rest is C/C++ (though its usage would differ in minor syntactic issues.)
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
|
|
|
|
|
Thanks for another very good explanation.
Then, Why would one put at the end of the union:
unsigned space : 3
Which just seems to be the total bits in the union is this the case and why would you do it?
So, then to access anything from:
So, to access x,y, or z you would:
SomeStuct mystruct;
int my_x = mystruct.x;
int my_y = mystruct.y;
int my_z = mystruct.z;
How would you get the values from field ?
Thanks
|
|
|
|
|
At a guess the unsigned space:3 is a mistake. I expect what space is meaning to contain is the other bits that an unsigned int occupies. On 32-bit machines space would be 29, i.e. 32 - 1 - 1 - 1 = 29.
smesser wrote:
How would you get the values from field ?
unsigned int my_field = mystruct.field;
Or do you mean how do you get x, y and z from mystruct.field?
Ant.
I'm hard, yet soft. I'm coloured, yet clear. I'm fruity and sweet. I'm jelly, what am I? Muse on it further, I shall return! - David Walliams (Little Britain)
|
|
|
|
|
Actually here is the actual struct
typedef struct
{
union
{
unsigned int field;
struct
{
unsigned xyz_offset: 1;
unsigned collision_fwd_fuselage: 1;
unsigned collision_aft_fuselage: 1;
unsigned collision_lt_wing: 1;
unsigned collision_rt_wing: 1;
unsigned collision_lt_tail: 1;
unsigned collision_rt_tail: 1;
unsigned collision_vt_tail: 1;
unsigned collision_lt_gear: 1;
unsigned collision_rt_gear: 1;
unsigned collision_nose_gear: 1;
unsigned collision_prop1: 1;
unsigned collision_prop2: 1;
unsigned collision_prop3: 1;
unsigned collision_prop4: 1;
unsigned weapon_strike: 1;
unsigned space: 16;
};
};
}CollisionType;
Yes, I wanted to get x,y, and z.
I think you would do it like this:
For example:
if( mystruct.x )
{
do_x_stuff();
}
if( mystruct.y )
{
do_y_stuff();
}
if( mystruct.z )
{
do_z_stuff();
}
Since each bit is either on or off I believe this would work.
|
|
|
|
|
Hi!
I'm looking for some platform-independent C/C++ function which would be to pre-fill a large memory area with some value, just like FillMemory in win32 does.
Does such a function exist, or do I have to make my own in assembly?
|
|
|
|
|
memset , in <string.h> .
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
|
|
|
|
|
Have you looked at memset ?
__________________________________________
a two cent stamp short of going postal.
|
|
|
|
|
Is it elegant to use 'memset' in a full C++ environment?
|
|
|
|
|
Is it elegant to use 'memset' in a full C++ environment?
It is all the rage in C++ parties lately Seriously now, its usage is just fine, and, unlike FillMemory , it is platform-independent, which is what you were looking for.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
|
|
|
|
|
and, of course, is there any way to put not bytes but ints (4bytes) after each other?
cause memset seems suitable only for single bytes...
|
|
|
|
|
There is no low-level function to do this that I know of. You'd have to resort to a manual loop or some STL algorithm like std::fill . The difference is that memset is probably optimized for the particular HW targeted by the compiler, and so much faster.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
|
|
|
|
|
Hi,
Could anyone tell me of an image class that is extremely fast and efficient in terms of bitmap blitting? BitBlt from microsoft is ridiculously slow, I use an old version of a class called CImage which is the fastest I've ever found, but it still isn't fast enough, I really want to make my game so that it can be run on a 300 or 350 mhz without much problem, but the way it is now it really needs at least a 500, so please let me know if anyone knows of a better class or library function to use for bitmaps.
Thanks!
Kelly Ryan
|
|
|
|
|
|
I have two file names, want to find out if they go to the same file.
What would you recommend?
Currently I'm doing a lot of "cleanup" (trim, lower, canonicalize, unquote, etc.), but this obviously doesn't catch a drive mapped to the same network path.
I do have r/w access to the file, but cannot modify it (as someone else might actually be using it the very moment)
TIA
Peter
we are here to help each other get through this thing, whatever it is Vonnegut jr. boost your code || Fold With Us! || sighist | doxygen
|
|
|
|
|
Maybe you can use SetFileTime temporarily set the creation time of file A to some reasonably unique value (in the distant past, for instance) and check the same attribute for file B with GetFileTime . If they are the same, the file is the same. After checking, revert to the original creation time and you're done.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
|
|
|
|
|
If e: and f: both "point to" \\machine\share, you could use NetUseGetInfo() or WNetGetConnection() to resolve the path to UNC and then just do a string compare.
char szFile1[MAX_PATH],
szFile2[MAX_PATH];
WNetGetConnection("e:\\pictures\\file1.jpg", szFile1, sizeof(szFile1));
WNetGetConnection("f:\\pictures\\file1.jpg", szFile2, sizeof(szFile2));
bool bSame = (strcmp(szFile1, szFile2) == 0);
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
Hi,
In My application,I have to implement Tab Views in SplitterWnd.Pl Help me.I tried with examples but I am not able to do that.
pl give an example.
dadsadasd
|
|
|
|
|
Help please!
I used simple wrapper class for the thread. It is just below.
so method ::Start() is used to run it. So some strange thing
is happening. If I call it inside some method
void MyClass::SomeMethod(){
this->p5dev_engine.Start();
...
}
Thread runs but when SomeMethod() is exited, thread does not run too.
But I need it continue to run.
May be did something in wrong way?
class P5DevEngine{
friend DWORD WINAPI DeviceEngineProc(LPVOID lpParameter);
public:
CRITICAL_SECTION csect;
P5DevEngine();
~P5DevEngine();
void Start();
void Stop();
inline BOOL IsRunning(){return running;}
private:
P5Device* p5;
BOOL running;
HANDLE thread;
int thID;
};
static DWORD WINAPI DeviceEngineProc(LPVOID lpParameter){
P5Device* device=(P5Device*)GetP5Device();
P5DevEngine* engine=(P5DevEngine*)lpParameter;
while(TRUE){
EnterCriticalSection(&engine->csect);
device->Cycle(30);
LeaveCriticalSection(&engine->csect);
::Sleep(30);
}
}
P5DevEngine::P5DevEngine(){
InitializeCriticalSection(&csect);
running=FALSE;
thread=NULL;
}
P5DevEngine::~P5DevEngine(){
DeleteCriticalSection(&csect);
}
void P5DevEngine::Start(){
P5Device* dev=(P5Device*)GetP5Device();
if(!dev->IsInited()) dev->Init();
thread=CreateThread(NULL,1,DeviceEngineProc,(LPVOID)this,0,(LPDWORD)thID);
if(thread)
running=TRUE;
else{
running=FALSE;
Error("Can not run thread");
}
}
void P5DevEngine::Stop(){
if(thread){
TerminateThread(thread,0);
running=FALSE;
}
}
|
|
|
|
|
void MyClass::SomeMethod(){
this->p5dev_engine.Start();
...
} I don't know for sure, but if the this object referred to inside MyClass::SomeMethod gets deleted after execution of that member function, the thread most likely will terminate abruptly, as ~P5DevEngine deletes the critical section DeviceEngineProc relies on.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
|
|
|
|
|
Thank you for replay.;););)
Situation is such:
MyClass is not deleted after execution ::SomeMethod().
p5dev_engine is not deleted too.
I tryed use this wothout CriticalSelection.
Behavior does not change.
May be I nedd to pass some flags to CreateThread(...)?
|
|
|
|
|
I've created an app that starts up 4 socket client threads. Before they can even start I need to pass the ip address and port in which they are talking to the socket servers on. Can I pass a struct via windows messages? if so is there a tutorial on how to do this?
Thanks
Tom Wright
tawright915@yahoo.com
|
|
|
|
|
Are you using MFC to set up your threads? If so, the solution to your problem dependes on wether the threads are of worker or User Interface type. If the former, you cannot use messages to communicate to the threads, and must rely on some other tecnhique (for instance, Win32 pipes.)
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
|
|
|
|
|
This is an MFC application and I am starting the thread using the AfxBeginThread.
Cannot I not pass a pointer to the structure as the second parameter?
Thanks
Tom Wright
tawright915@yahoo.com
|
|
|
|
|
AfxBeginThread can be used to create worker or UI threads. As you mention the second parameter, I'll assume is the worker variant you're using.
Yes, you can pass a pointer to the structure inside that second parameter. There's one caveat, though, if the struct is dynamically created with new : as you don't know where exactly in time the thread will access the data in the structure, you shouldn't delete it outside the thread:
my_struct * ms=new my_struct(...);
AfxBeginThread(thread_proc,ms,...);
delete ms; the proper way is to let the thread itself delete the data.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
|
|
|
|
|