main()
{
cout<< "in main"<< endl;
volatile GENERIC_COMMAND* A;//this has to be volatile only.
COMPLETION_WAIT cw;
A = new volatile union GENERIC_COMMAND;
A[0] = cw;
//....
}
Re: Error when using volatile object in overload assignment operator.
I don't understand why you wrap up your structs like above but your definition of how many bytes to use for a particular object shows your misunderstandings of structure memory layout. I used to have an interview with 4x year old project manager of a UK company and he made me laugh for his naive calculation of byte numbers being used after such a struct definition (as he claimed to be the sum of all object inside the struct), I wasn't the question maker or one with a final decision to report true or false, I was pretty much insulted of my C/C++ skills as if his embedded C knowledge were always up to date with old UK instruments. But that helps me conclude myself that whenever I meet anyone who asked me to write down clearly every single line of code to i.e traverse a tree, then I do believe this guy is just good enough to be my yoga or gym teacher.
Back to your problem, your client stub doesn't make any sense out of volatile main usage. There is nothing that says your unions can not be modified and needs the keyword to redefine the situation; and concurrency is not easily resolved in just a single keyword if you would want to make your system more robust with full features of qualified software. Many people prefer a vector in place of an array (pointer).
Secondly, I took the code you posted and attempted to compile it using the online Comeau C++ compiler. It fails to compile:
Code:
Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
Copyright 1988-2008 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions
"ComeauTest.c", line 9: error: declaration does not declare anything
struct
^
"ComeauTest.c", line 16: error: declaration does not declare anything
struct
^
"ComeauTest.c", line 24: error: declaration does not declare anything
struct
^
"ComeauTest.c", line 33: error: union "GENERIC_COMMAND" has no member "dword1"
this->dword1 = rhs.dword1;
^
"ComeauTest.c", line 34: error: union "GENERIC_COMMAND" has no member "dword2"
this->dword2 = rhs.dword2;
^
"ComeauTest.c", line 35: error: union "GENERIC_COMMAND" has no member "dword3"
this->dword3 = rhs.dword3;
^
"ComeauTest.c", line 36: error: union "GENERIC_COMMAND" has no member "dword4"
this->dword4 = rhs.dword4;
^
"ComeauTest.c", line 44: error: declaration does not declare anything
struct
^
"ComeauTest.c", line 54: error: declaration does not declare anything
struct
^
"ComeauTest.c", line 69: error: union "COMPLETION_WAIT" has no member "dword1"
command.dword1 = 0;
^
"ComeauTest.c", line 70: error: union "COMPLETION_WAIT" has no member "dword2"
command.dword2 = 0;
^
"ComeauTest.c", line 71: error: union "COMPLETION_WAIT" has no member "dword3"
command.dword3 = 0;
^
"ComeauTest.c", line 72: error: union "COMPLETION_WAIT" has no member "dword4"
command.dword4 = 0;
^
"ComeauTest.c", line 74: error: union "COMPLETION_WAIT" has no member "s"
command.s = s;
^
"ComeauTest.c", line 75: error: union "COMPLETION_WAIT" has no member "i"
command.i = i;
^
"ComeauTest.c", line 76: error: union "COMPLETION_WAIT" has no member "f"
command.f = f;
^
"ComeauTest.c", line 77: error: union "COMPLETION_WAIT" has no member
"store_address"
command.store_address = store_address >> 3;
^
"ComeauTest.c", line 78: error: union "COMPLETION_WAIT" has no member "opcode"
command.opcode = 0x1;
^
"ComeauTest.c", line 79: error: union "COMPLETION_WAIT" has no member "store_data"
command.store_data = store_data;
^
"ComeauTest.c", line 33: error: union "COMPLETION_WAIT" has no member "dword1"
this->dword1 = rhs.dword1;
^
detected during instantiation of "const GENERIC_COMMAND
&GENERIC_COMMAND::operator=(const T &) [with
T=COMPLETION_WAIT]" at line 82
"ComeauTest.c", line 34: error: union "COMPLETION_WAIT" has no member "dword2"
this->dword2 = rhs.dword2;
^
detected during instantiation of "const GENERIC_COMMAND
&GENERIC_COMMAND::operator=(const T &) [with
T=COMPLETION_WAIT]" at line 82
"ComeauTest.c", line 35: error: union "COMPLETION_WAIT" has no member "dword3"
this->dword3 = rhs.dword3;
^
detected during instantiation of "const GENERIC_COMMAND
&GENERIC_COMMAND::operator=(const T &) [with
T=COMPLETION_WAIT]" at line 82
"ComeauTest.c", line 36: error: union "COMPLETION_WAIT" has no member "dword4"
this->dword4 = rhs.dword4;
^
detected during instantiation of "const GENERIC_COMMAND
&GENERIC_COMMAND::operator=(const T &) [with
T=COMPLETION_WAIT]" at line 82
"ComeauTest.c", line 92: error: no operator "=" matches these operands
operand types are: volatile GENERIC_COMMAND = COMPLETION_WAIT
A[0] = cw;
^
24 errors detected in the compilation of "ComeauTest.c".
In strict mode, with -tused, Compile failed
Hit the Back Button to review your code and compile options.
Compiled with C++0x extensions enabled.
What compiler are you using?
Let's do something simple:
Code:
union foo
{
struct s1
{
int m_x;
};
struct s2
{
int m_x2;
};
s1 m_s1;
s2 m_s2;
};
int main()
{
foo f;
f.m_s1.m_x = 0;
}
Look at what's in red. You are missing declaring actual instances of your structs. You can't just declare structs -- you must actually create them. Given this, I don't see how your code would have compiled under any C++ compiler.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; July 27th, 2012 at 06:46 AM.
Re: Error when using volatile object in overload assignment operator.
thanks for mentioning that.
I am using g++ compiler. I have tried running the same on DEV C++ 4.9.9.2 bloodshed.
I do not get any warning which you are getting.
Sorry, I am not familiar to Comeau C++ compiler.
You can try this code on either g++(linux) or Dev C++(windows).
Re: Error when using volatile object in overload assignment operator.
Originally Posted by ritulguru
thanks for mentioning that.
I am using g++ compiler. I have tried running the same on DEV C++ 4.9.9.2 bloodshed.
Turn on the ANSI switch and you will see the same errors.
Code:
-Wall -pedantic
I believe those are the switches for g++.
The problem is that by default, the g++ compiler will compile non-standard C++ code. If you turn on the ANSI compiler switch, you will see the same errors I'm seeing now. The code you posted is not ANSI C++, it is C++ with compiler extensions that are non-standard. Once you start using extensions, then all bets are off as to how your code will behave (then it's your responsibility to read the manual).
I do not get any warning which you are getting.
Turn on the ANSI switch and you will get the errors with the g++ compiler. The g++ compiler must be able to compile C++ code without any extensions (and if extensions are used, it must emit an error), else it can't claim to be an ANSI C++ compiler.
Sorry, I am not familiar to Comeau C++ compiler.
It is an online, ANSI C++ compiler. If the code fails to compile on that compiler, chances are great that the code is not compliant. Many professional C++ programmers who want to test if their code is compliant go to the on-line Comeau compiler to test their code. Obviously your code is not compliant.
You can try this code on either g++(linux) or Dev C++(windows).
No need to. I know if I set the right switches, the code will fail to compile. And BTW, the Dev C++ is not a compiler -- it uses g++ 3.x underneath the hood, so you see the same issues with both g++ under Linux and g++ using Dev-C++.
Here is the code that must compile with all ANSI C++ compilers.
Re: Error when using volatile object in overload assignment operator.
@Paul: I have to use a pointer to volatile object, as memory allocation function i am using (which is not mentioned here) returns pointer to volatile object.
before it was getting compiled without volatile qualifier, problem introduced after adding volatile qualifier.
After compiling with options -Wall -pedantic, i do get below warnings.
warning: ISO C++ prohibits anonymous structs.
thanks for mentioning this.
Re: Error when using volatile object in overload assignment operator.
constvolatile.cpp: In function ‘void add(uint32_t, uint32_t, uint32_t, uint64_t, uint64_t, bool)’:
constvolatile.cpp:82: warning: object of type ‘volatile GENERIC_COMMAND&’ will not be accessed in statement
constvolatile.cpp: In function ‘int main()’:
constvolatile.cpp:93: warning: object of type ‘volatile GENERIC_COMMAND&’ will not be accessed in statement
ritul@ritul-laptop:~/Desktop$
Bookmarks