The general answer is B but in your case there is no difference between A and B, and C would generate a compiler error.
It can be better explained when having a dynamically created array:
// my_data1 is a volatile pointer
uint8_t* volatile my_data1 = (uint8_t* volatile)malloc(10);
// The array elements are volatilevolatile uint8_t* my_data2 = (uint8_t*)malloc(10);
// Both are volatilevolatile uint8_t* volatile my_data3 = (uint8_t* volatile)malloc(10);
With the volatile pointers, the casts must also contain the volatile keyword.
But with arrays, the pointer can't be re-assigned so that version C is not allwed and the position of the volatile keyword does not care.
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
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 constvolatile 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.
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
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 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.
created a Dialog base Application with Tree control..Now i had Selected some leaf item from my tree control ,And after that click on to open any dialog then the Tree Leaf unselected.
Now i want tree item should be selected after closing the dialog.
Now I tell u full story. when i drag the item into grid it also add into TreeView.Now suppose there are 3/4 item into editor and u select one the parallely tree item will also select.so now this time both the thing are selected but as u will click some where else like open an dialogbox and close it then tree item will be selected but partially it`s colour will be Gray..now my problem is that Gary colour. I need it Fully selected Item.
That should be the way is works. Each thread should acquire the Mutex (i.e. lock it) in order to protect the data. When they finish the critical section they should Release it. The point being that you only want one thread through this path at any one time.
When the ReleaseMutex are all released or only one at a time
It depends on the scheduler. Thread scheduling is a very interesting topic in OS design, and the rules can vary from platform. But a general rule is that the waiting thread with the highest priority runs. However some OSs throw in a bit of priority inversion on occasion to free up any deadlocks.
Within the same priority group you might decide on say, 'last thread run', or 'first thread waiting' to decide which to activate and give the CPU to. A bit like FIFO, LRU LIFO etc, there are a lot of designs.
The important point though is that the OS does this for you so no need to worry about it. Use the example from Microsoft and all will be well.
i need help for this project. I would glad if someone could help me with my concern.
Create in C/C++ client and server application for UNIX operating system using BSD
sockets (located at eva.fit.vutbr.cz or merlin.fit.vutbr.cz) which will provide directory
service (small LDAP) .
• Final programs should be compiled into filename called client and
• Create makefile which can be used for automatic compilation of source
files by typing make statement in UNIX shell.
• All errors should be printed on stderr.
• Directory database will be presented as text file (CSV format) with
• “id,login,surname,firstname,faculty” . Each entry (line) is terminated by
We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
In any case, game development in C++ will be tough, and if you are a beginners don't even bother.