Click here to Skip to main content
15,895,084 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Know when a child popup dialog is opened Pin
_Flaviu12-Dec-16 5:24
_Flaviu12-Dec-16 5:24 
GeneralRe: Know when a child popup dialog is opened Pin
Victor Nijegorodov12-Dec-16 7:46
Victor Nijegorodov12-Dec-16 7:46 
GeneralRe: Know when a child popup dialog is opened Pin
_Flaviu12-Dec-16 23:48
_Flaviu12-Dec-16 23:48 
AnswerRe: Know when a child popup dialog is opened Pin
leon de boer13-Dec-16 16:47
leon de boer13-Dec-16 16:47 
GeneralRe: Know when a child popup dialog is opened Pin
_Flaviu15-Dec-16 23:24
_Flaviu15-Dec-16 23:24 
QuestionProper declaration of array with volatile values inside it? Pin
arnold_w7-Dec-16 3:30
arnold_w7-Dec-16 3:30 
AnswerRe: Proper declaration of array with volatile values inside it? Pin
Jochen Arndt7-Dec-16 4:22
professionalJochen Arndt7-Dec-16 4:22 
AnswerRe: Proper declaration of array with volatile values inside it? Pin
leon de boer7-Dec-16 6:39
leon de boer7-Dec-16 6:39 
I am confused what Jochen is saying to you but I will say nothing you nor Jochen have code will make the items within the array volatile.

First lets cover the basics volatile is an optimizer instruction it has very little to do with the C language as such it is also HIGHLY vendor sensitive. So the first thing you always need to do is check your vendor tools and specification.

My next warning is to always put the volatile between the type and the name not at the front as people love to do. Things like the arm compilers are notorious with volatile in the front of the type and if you use it in macros you will end up with it on the wrong thing. I covered this in a recent article on baremetal with the RPi but Arm covers this in there documentation
ARM Information Center[^]
You see the format, that is not just the arm standard it is the standard I suggest you use as most embedded compilers get it right
type volatile * const ptr;
 |     |     |   |    |
 |     |     |   |    +------> ptr is a
 |     |     |   +-----------> constant
 |     |     +---------------> pointer to a
 |     +---------------------> volatile
 +---------------------------> integer/long/etc

Almost all vendor tools will fail to do with volatile what the full C specification says because the specification isn't written by embedded processor vendors and they get little say in it. Some of the junk that is in the C specification around the word volatile is ignored because it doesn't make sense in micro-controllers. The scary stat is 96% of vendor tools fail the full C implementation (Testing result reference :[^])

So lets get down to specifics the volatile must also be on any item within the array putting a volatile on the array itself will not work almost any compiler. The compiler doesn't see "blocks" of things as volatile it only sees things it is accessing in the mode it is accessing them as volatile.

The fact you want a volatile inside a malloc would tend to indicate you are playing around with a DMA controller so I will reference hardware access here. If this is just shared multitask memory nothing changes it's just simpler to understand.

This is a basic hardware register definition example for something like a DMA controller
typedef uint32_t volatile rwreg32;								// 32 bit read/write register
typedef uint32_t const volatile readreg32;						// 32 bit read only register

I can then define an array of them and the volatile will be on each and every array item because when the C compiler picks up an array entry they are a type which includes the word volatile. Note if you accessed the enteries as words or bytes those accesses would not be volatile, it is that specific.
rwreg32* SomeArray = (rwreg32*)malloc(10 * sizeof(rwreg32));

You notice there is no word volatile on the array itself that is pointless and won't work unless you were passing the whole array to something. The same situation exists with structs if you wish to use them as representations of hardware. The volatile must be on each item in the struct AS IT WOULD BE ACCESSED and even down to bit packs if that is how you access them. Here is a small random sample from an OTG chipset struct
volatile const struct {
    volatile unsigned SpaceAvailable : 16; // @0
    volatile unsigned QueueSpaceAvailable : 8; // @16
    volatile unsigned Terminate : 1; // @24
    volatile enum {
        InOut = 0,
        ZeroLengthOut = 1,
        PingCompleteSplit = 2,
        ChannelHalt = 3,
    } TokenType : 2; // @25
    volatile unsigned Channel : 4; // @27
    volatile unsigned Odd : 1; // @31
} __attribute__ ((__packed__)) Status; // Read Only +0x2c

You see how they have put volatile on each entry in the struct as they would be accessed, and as the struct itself may be accessed as a whole it is put on the struct itself.

So basically however you access the hardware must be seen as a volatile type, just putting volatile on something doesn't make it volatile as a whole if you don't access it that way. So even declaring something like a register as volatile doesn't work if you access the register as bits. Many micro-controllers have bit operations they can do on registers and special hardware and that is what those who write the C specification seem to not understand time and time again. Some vendors will allow you just to mark the register as volatile but many more require you to mark each bit within the register as volatile if you use bit operations. That is why many vendors tools have a more simple definition called SFR for special function register where they do the volatile on everything for you.

So this is why the correct way to do this is however your C tool vendor says to do it because the C standard may be of little help on anything more than a simple shared memory use of the word. In 4 revisions of the C standard the committee hasn't even managed to give us a way to define a write only register which almost every micro-controller has, so don't expect them to deal with volatile anytime soon.

If you are on something like an ARM6+ it gets worse the C standard has no idea what to do with Out-Of-Order-Execution processors like the Arm Smile | :) Most of the new fancy phones run Weakly-Ordered CPU's and if you are coding for them you need to understand how to deal with it and a few more tricks you won't find in the C standard. I bring it up because on those you don't even write directly to the hardware (there is a cache between you an it) so the idea of volatile gets even more convoluted.
In vino veritas


modified 7-Dec-16 20:45pm.

QuestionTree Ctrl Object Selection Pin
raajpatel4-Dec-16 20:57
raajpatel4-Dec-16 20:57 
GeneralRe: Tree Ctrl Object Selection Pin
Richard MacCutchan4-Dec-16 21:16
mveRichard MacCutchan4-Dec-16 21:16 
GeneralRe: Tree Ctrl Object Selection Pin
raajpatel5-Dec-16 1:20
raajpatel5-Dec-16 1:20 
GeneralRe: Tree Ctrl Object Selection Pin
Peter_in_27805-Dec-16 1:36
professionalPeter_in_27805-Dec-16 1:36 
QuestionMutex WaitForSingleObject does not Signal Pin
ForNow1-Dec-16 13:14
ForNow1-Dec-16 13:14 
AnswerRe: Mutex WaitForSingleObject does not Signal Pin
Richard MacCutchan1-Dec-16 21:39
mveRichard MacCutchan1-Dec-16 21:39 
GeneralRe: Mutex WaitForSingleObject does not Signal Pin
ForNow2-Dec-16 2:25
ForNow2-Dec-16 2:25 
GeneralRe: Mutex WaitForSingleObject does not Signal Pin
Richard MacCutchan2-Dec-16 3:01
mveRichard MacCutchan2-Dec-16 3:01 
GeneralRe: Mutex WaitForSingleObject does not Signal Pin
ForNow10-Dec-16 15:12
ForNow10-Dec-16 15:12 
GeneralRe: Mutex WaitForSingleObject does not Signal Pin
Richard MacCutchan10-Dec-16 20:46
mveRichard MacCutchan10-Dec-16 20:46 
AnswerRe: Mutex WaitForSingleObject does not Signal Pin
Munchies_Matt9-Dec-16 2:52
Munchies_Matt9-Dec-16 2:52 
GeneralRe: Mutex WaitForSingleObject does not Signal Pin
ForNow10-Dec-16 15:07
ForNow10-Dec-16 15:07 
GeneralRe: Mutex WaitForSingleObject does not Signal Pin
Munchies_Matt10-Dec-16 21:01
Munchies_Matt10-Dec-16 21:01 
GeneralRe: Mutex WaitForSingleObject does not Signal Pin
ForNow11-Dec-16 0:17
ForNow11-Dec-16 0:17 
GeneralRe: Mutex WaitForSingleObject does not Signal Pin
Munchies_Matt11-Dec-16 21:48
Munchies_Matt11-Dec-16 21:48 
QuestionC++ Pin
Member 1287185126-Nov-16 3:50
Member 1287185126-Nov-16 3:50 
AnswerRe: C++ Pin
PIEBALDconsult26-Nov-16 4:10
mvePIEBALDconsult26-Nov-16 4:10 

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.