Click here to Skip to main content
16,010,553 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionMemory aligned bit fields Pin
Trevor Johansen24-Sep-10 21:34
Trevor Johansen24-Sep-10 21:34 
AnswerRe: Memory aligned bit fields Pin
Luc Pattyn24-Sep-10 23:02
sitebuilderLuc Pattyn24-Sep-10 23:02 
AnswerRe: Memory aligned bit fields Pin
Alain Rist24-Sep-10 23:53
Alain Rist24-Sep-10 23:53 
AnswerRe: Memory aligned bit fields Pin
Aescleal25-Sep-10 0:56
Aescleal25-Sep-10 0:56 
AnswerRe: Memory aligned bit fields Pin
Trevor Johansen25-Sep-10 6:51
Trevor Johansen25-Sep-10 6:51 
AnswerRe: Memory aligned bit fields Pin
Aescleal25-Sep-10 7:26
Aescleal25-Sep-10 7:26 
GeneralRe: Memory aligned bit fields Pin
Trevor Johansen25-Sep-10 8:40
Trevor Johansen25-Sep-10 8:40 
AnswerRe: Memory aligned bit fields Pin
Aescleal25-Sep-10 10:07
Aescleal25-Sep-10 10:07 
Ouch. Neither of your examples are going to work.

The first one won't because of this line...

COMM_FR myflags = new( (void *)(0x1400000) ) COMM_FR;


What this line says is: "Initialise an object of type COMM_FR at the specified location in memory. Create a completely new object of type COMM_FR and initialise it from the object you've just created."

You can see this by taking the address of COMM_FR - it's almost certainly not going to be 0x1400000.

The second won't work Flags is never going to be a pointer. It's a physical lump of memory, not the address of the memory. Here's how I'd have a go at writing the same sort of thing:

struct COMM
{
    unsigned RX0E:1;
    unsigned TX0E:1;
    unsigned RX1E:1;
    unsigned TX1E:1;
    unsigned RX2E:1;
    unsigned TX2E:1;
    unsigned EOFF:1;
    unsigned FNEF:1;
};


Quick note here: You don't need typedef so much in C++. Generally it's only used to simplify complex types.

union COMM_FR
{
    COMM flags;
    unsigned reg;

    COMM_FR() : reg( 0x00 ) {}
};


You only need to initialise one "view" of the union in it's constructor. Constructors don't have a return type, even void. The comment about typedef applies as before.

Now you've got two ways of setting up access to your device... The first way is if you want to zero the register when you first use it - you have to invoke the constructor:

int main()
{
    COMM_FR *myflags = new( (void *)(0x1400000) ) COMM_FR;

    myflags->flags.RXOE = 1;

    std::cout << std::hex << myflags->reg << std::endl;
}

The placement new invokes the constructor so the register will be written to implicitly before the line setting RXOE.

The second way is if something else has initialised the hardware and you're just using it:

int main()
{
    COMM_FR *myflags = reinterpret_cast<COMM_FR *>( (void *)0x1400000 );

    myflags->flags.RXOE = 1;

    std::cout << std::hex << myflags->reg << std::endl;
}

This one doesn't execute the constructor at all, so the final result depends on what state the register is in before you set RXOE.

PS: Just noticed that Luc makes a good point about volatile. Make sure that anything that touches the bare metal is volatile or your cunning compiler will have a good go at eliminating writes if it doesn't change the visible effects of your program.
GeneralRe: Memory aligned bit fields Pin
Trevor Johansen25-Sep-10 11:15
Trevor Johansen25-Sep-10 11:15 
AnswerRe: Memory aligned bit fields Pin
Luc Pattyn25-Sep-10 8:55
sitebuilderLuc Pattyn25-Sep-10 8:55 
GeneralRe: Memory aligned bit fields Pin
Trevor Johansen25-Sep-10 9:34
Trevor Johansen25-Sep-10 9:34 
GeneralRe: Memory aligned bit fields Pin
Luc Pattyn25-Sep-10 9:45
sitebuilderLuc Pattyn25-Sep-10 9:45 
GeneralRe: Memory aligned bit fields Pin
Trevor Johansen25-Sep-10 10:57
Trevor Johansen25-Sep-10 10:57 
AnswerRe: Memory aligned bit fields Pin
Luc Pattyn25-Sep-10 11:18
sitebuilderLuc Pattyn25-Sep-10 11:18 
GeneralRe: Memory aligned bit fields Pin
Trevor Johansen25-Sep-10 11:56
Trevor Johansen25-Sep-10 11:56 
GeneralRe: Memory aligned bit fields Pin
Luc Pattyn25-Sep-10 12:14
sitebuilderLuc Pattyn25-Sep-10 12:14 
GeneralRe: Memory aligned bit fields Pin
Alain Rist25-Sep-10 9:57
Alain Rist25-Sep-10 9:57 
GeneralRe: Memory aligned bit fields Pin
Aescleal25-Sep-10 10:19
Aescleal25-Sep-10 10:19 
AnswerRe: Memory aligned bit fields Pin
Alain Rist25-Sep-10 11:26
Alain Rist25-Sep-10 11:26 
GeneralRe: Memory aligned bit fields Pin
Aescleal25-Sep-10 22:31
Aescleal25-Sep-10 22:31 
GeneralRe: Memory aligned bit fields Pin
Trevor Johansen25-Sep-10 10:51
Trevor Johansen25-Sep-10 10:51 
GeneralRe: Memory aligned bit fields Pin
Aescleal25-Sep-10 22:34
Aescleal25-Sep-10 22:34 
QuestionDirectX - Antialiasing and Plotting Pixels Query [Moved] Pin
simon alec smith24-Sep-10 9:52
simon alec smith24-Sep-10 9:52 
QuestionHow to process multiple commands on CAccessor. Pin
Spawn@Melmac24-Sep-10 8:36
Spawn@Melmac24-Sep-10 8:36 
QuestionGetHeaderCtrl returns null Pin
genush24-Sep-10 6:09
genush24-Sep-10 6:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.