Click here to Skip to main content
15,914,368 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: how to create client and server winsock in one project or use 2 winsock in one project? Pin
Moak26-Sep-10 0:59
Moak26-Sep-10 0:59 
GeneralRe: how to create client and server winsock in one project or use 2 winsock in one project? Pin
Richard MacCutchan26-Sep-10 1:04
mveRichard MacCutchan26-Sep-10 1:04 
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 
First off the code I presented had something similar to what Luc suggested. That works.

Placement new...

There is a problem using placement new with hardware and that's constructors. C++ objects are meant to be constructed in uninitialised memory. If you use placement new to overlay a particular memory location with an object the constructor will run and there's a chance the memory you're trying to overlay (i.e. interpret differently) will be modified. Over a set of device registers this might be interesting!

However the block of memory you're looking at has already been constructed - there's an object there it's just the compiler doens't know it. So instead of using placement new just cast the location to a pointer to the C++ representation of the raw memory.

A quick example would be something like the BIOS warmboot flag. You know where it is but it's been initialised when the computer boots. By the time any code you write gets control it's already been initialised. The warm_boot_control object is there, any program you write doesn't know what format it is.

So you declare a C++ class:

class warm_boot_flag
{
    public:
        void prepare_for_warm_boot();
        void prepare_for_cold_boot();

    private:
        unsigned char flag_;
};


and you use it by:

warm_boot_flag *warm_boot_flag *warm_boot_flag_ptr( reinterpret_cast<warm_boot_flag *>( 0x472 ) );


and:

warm_boot_flag->prepare_for_warm_boot();


If, on the other hand, you were writing a BIOS and wanted to make sure the warmboot flag is set up properly, you could then use placement new as you'd want to make sure that the flag is constructed. You could use almost the same class:

class warm_boot_flag
{
    public:
        warm_boot_flag() { prepare_for_cold_boot(); }

        void prepare_for_warm_boot();
        void prepare_for_cold_boot();

    private:
        unsigned char flag_;
};


and you'd use it with placement new:

warm_boot_flag *warm_boot_pointer = new( (void *)(0x472) ) warm_boot_flag;


So in a nutshell if you're writing a component that requires initialising hardware, use placement new. If you're writing a component that's using a resource already set up use an old fashioned cast.

Cheers,

Ash
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 
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 

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.