B
Bryan Parkoff
I create one 32 Bits variable and four pointer variables. Four pointer
variables link or point to one 32 Bits variable. Each pointer variable is 8
Bits. Look at my example below.
unsigned int AA = 0;
unsigned char* const AA_Byte1 = (unsigned char*)&AA;
unsigned char* const AA_Byte2 = (unsigned char*)&AA + 1;
unsigned char* const AA_Byte3 = (unsigned char*)&AA + 2;
unsigned char* const AA_Byte4 = (unsigned char*)&AA + 3;
AA = 0x41424344;
printf("First Byte: %Xh", *AA_Byte1);
printf("Second Byte: %Xh", *AA_Byte2);
printf("Third Byte: %Xh", *AA_Byte3);
printf("Fourth Byte: %Xh", *AA_Byte4);
printf("Native DWord: %Xh", AA);
My style above works fine for my own coding. I don't need to use SHIFT,
AND, OR, or other keywords because it may slow on Pentium 4. For example
below.
unsigned char A = 0x01;
unsigned char B = 0x20;
unsigned int C = (B << 8 | A) & 0xffff;
I am not sure if my style coding will work on other processors
(non-Intel and non-AMD like Big/Little Endian). I don't use union because
it is not efficiency, but I would prefer to leave one variable at 32 Bits or
64 Bits. I use pointer alaising to point one 32 Bits / 64 Bits variable
will benefit great.
Please explain what Strict Pointer Aliasing rule mean because I am told
that it will not work at high optimization on other processors, but it runs
fine on any Intel and AMD processor family.
It is necessary to place CONST between "unsigned char*" and "AA_Byte1"
because I don't want the pointer address to be modified. One problem is
that I can't do in C++ class because it is not designed at initialization.
I tried to use static outside of C++ class, but it does not work that way.
Can you please give the hint how to overcome the problem? I can only be
able to place static const before unsigned char*, but can't do between
unsigned char* and variable name in C++ class. There has to be another way
to prevent pointer address to be modified.
I am told that C++ class is much better than global object because it
can remain in stack segment, but global object can remain in data segment
(Intel / AMD arch). I understand that store objects in stack segment is
much faster than data segment. Is it true?
Why do many C programmers want to avoid using C++ class while they use
struct object in global object? It won't be very efficency. Please explain
what you think that I should stay with C++ class. Thanks...
variables link or point to one 32 Bits variable. Each pointer variable is 8
Bits. Look at my example below.
unsigned int AA = 0;
unsigned char* const AA_Byte1 = (unsigned char*)&AA;
unsigned char* const AA_Byte2 = (unsigned char*)&AA + 1;
unsigned char* const AA_Byte3 = (unsigned char*)&AA + 2;
unsigned char* const AA_Byte4 = (unsigned char*)&AA + 3;
AA = 0x41424344;
printf("First Byte: %Xh", *AA_Byte1);
printf("Second Byte: %Xh", *AA_Byte2);
printf("Third Byte: %Xh", *AA_Byte3);
printf("Fourth Byte: %Xh", *AA_Byte4);
printf("Native DWord: %Xh", AA);
My style above works fine for my own coding. I don't need to use SHIFT,
AND, OR, or other keywords because it may slow on Pentium 4. For example
below.
unsigned char A = 0x01;
unsigned char B = 0x20;
unsigned int C = (B << 8 | A) & 0xffff;
I am not sure if my style coding will work on other processors
(non-Intel and non-AMD like Big/Little Endian). I don't use union because
it is not efficiency, but I would prefer to leave one variable at 32 Bits or
64 Bits. I use pointer alaising to point one 32 Bits / 64 Bits variable
will benefit great.
Please explain what Strict Pointer Aliasing rule mean because I am told
that it will not work at high optimization on other processors, but it runs
fine on any Intel and AMD processor family.
It is necessary to place CONST between "unsigned char*" and "AA_Byte1"
because I don't want the pointer address to be modified. One problem is
that I can't do in C++ class because it is not designed at initialization.
I tried to use static outside of C++ class, but it does not work that way.
Can you please give the hint how to overcome the problem? I can only be
able to place static const before unsigned char*, but can't do between
unsigned char* and variable name in C++ class. There has to be another way
to prevent pointer address to be modified.
I am told that C++ class is much better than global object because it
can remain in stack segment, but global object can remain in data segment
(Intel / AMD arch). I understand that store objects in stack segment is
much faster than data segment. Is it true?
Why do many C programmers want to avoid using C++ class while they use
struct object in global object? It won't be very efficency. Please explain
what you think that I should stay with C++ class. Thanks...