473,401 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

rvalue

Hi all, It's been a while!
I'm working on a very buggy piece of code that uses a lot of macros.
A bunch of them look like
#define POSITION(mw,row) (mw.positions[row])
In order to debug the thing I'd like to modify these macors so they can only be
used as rvalues and not as lvalues. If they are used as lvalues I want my
compililation to fail. Unfortunately I can't remember a nice way to do that.
As always thanks for the help!
Tobias.

Nov 14 '05 #1
12 1368
Stefan Ram wrote:
Tobias Oed <to****@physics.odu.edu> writes:
In order to debug the thing I'd like to modify these macors so
they can only be used as rvalues and not as lvalues.


You might cast them to their type. (A cast does not yield an
lvalue.)


True. But my compiler (gcc) requires special options (-pedantic) before
the following fails

int main(void){

int a;
(int) a = 3;

return 0;
}

If possible, I'd like to avoid that. Also, I'd have to be carefull
modifying all these macros not to screw up somewhere between an int
and short, and I'm no good at that kind of manual labour.
A while back there was a discussion here (or was it fclc?) about
web pages exposing exteme measures for defensive programming. One
part of it was the above. At the time I thought the author was insane.
Now that's just about what I need but google doesn't turn up anything...
I'll try the cast for select macros until something better comes up.
Thanks, Tobias.

Nov 14 '05 #2
Tobias Oed wrote:
Stefan Ram wrote:
Tobias Oed <to****@physics.odu.edu> writes:
In order to debug the thing I'd like to modify these macors so
they can only be used as rvalues and not as lvalues.


You might cast them to their type. (A cast does not yield an
lvalue.)


True. But my compiler (gcc) requires special options (-pedantic) before
the following fails

int main(void){

int a;
(int) a = 3;

return 0;
}

If possible, I'd like to avoid that. Also, I'd have to be carefull
modifying all these macros not to screw up somewhere between an int
and short, and I'm no good at that kind of manual labour.
A while back there was a discussion here (or was it fclc?) about
web pages exposing exteme measures for defensive programming. One
part of it was the above. At the time I thought the author was insane.
Now that's just about what I need but google doesn't turn up anything...
I'll try the cast for select macros until something better comes up.


All right, so my googles are dirty!
http://users.bestweb.net/~ctips/
He recomends something like
((void) 0, a) = 3;
For which gcc also needs -pedantic before giving a warning instead of
an error with your cast to type trick.
Any other idea?
Tobias.


Nov 14 '05 #3
Tobias Oed wrote:
#define POSITION(mw,row) (mw.positions[row])
In order to debug the thing I'd like to modify these macors so they
can only be used as rvalues and not as lvalues.


#define RVALUE(x) ((x)+0)
#define POSITION(mw,row) RVALUE(mw.positions[row])

Or you could just sprinkle '+0' around your macros instead of using an
RVALUE macro, but then someone who reads the code later might wonder why
you have been doing so.

--
Hallvard
Nov 14 '05 #4
On Thu, 08 Apr 2004 22:18:00 -0400, Tobias Oed
<to****@physics.odu.edu> wrote in comp.lang.c:
Stefan Ram wrote:
Tobias Oed <to****@physics.odu.edu> writes:
In order to debug the thing I'd like to modify these macors so
they can only be used as rvalues and not as lvalues.


You might cast them to their type. (A cast does not yield an
lvalue.)


True. But my compiler (gcc) requires special options (-pedantic) before
the following fails

int main(void){

int a;
(int) a = 3;

return 0;
}

If possible, I'd like to avoid that. Also, I'd have to be carefull
modifying all these macros not to screw up somewhere between an int
and short, and I'm no good at that kind of manual labour.
A while back there was a discussion here (or was it fclc?) about
web pages exposing exteme measures for defensive programming. One
part of it was the above. At the time I thought the author was insane.
Now that's just about what I need but google doesn't turn up anything...
I'll try the cast for select macros until something better comes up.
Thanks, Tobias.


Perhaps something like:

#define POSITION(mw,row) (mw.positions[row] + 0)

?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #5
Tobias Oed <to****@physics.odu.edu> wrote in message news:<c5*************@ID-97389.news.uni-berlin.de>...
Hi all, It's been a while!
I'm working on a very buggy piece of code that uses a lot of macros.
A bunch of them look like
#define POSITION(mw,row) (mw.positions[row])
In order to debug the thing I'd like to modify these macors so they can only be
used as rvalues and not as lvalues. If they are used as lvalues I want my
compililation to fail. Unfortunately I can't remember a nice way to do that.
As always thanks for the help!
Tobias.


Maybe remove the macros if you can. It should be fairly quick to do
so by searching for "POSITION" etc. If the code looks clearer with
the macros removed, then leave them out.
Nov 14 '05 #6
Tobias Oed <to****@physics.odu.edu> writes:
Hi all, It's been a while!
I'm working on a very buggy piece of code that uses a lot of macros.
A bunch of them look like
#define POSITION(mw,row) (mw.positions[row])
In order to debug the thing I'd like to modify these macors so they
can only be used as rvalues and not as lvalues. If they are used as
lvalues I want my compililation to fail. Unfortunately I can't
remember a nice way to do that.


Assuming the result is numeric, I think this will work.

#define POSITION(mw,row) (+mw.positions[row])

I usually prefer to enclose all reference to macro arguments in
parentheses to avoid operator precedence problems, but I don't *think*
it's necessary in this case (though it won't hurt to do it anyway).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #7
Keith Thompson <ks***@mib.org> spoke thus:
#define POSITION(mw,row) (+mw.positions[row]) I usually prefer to enclose all reference to macro arguments in
parentheses to avoid operator precedence problems, but I don't *think*
it's necessary in this case (though it won't hurt to do it anyway).


It is necessary, I believe:

mynum=my_num+POSITION(a,b);

The parentheses are necessary for the above line to compile (assuming
all variables are correctly declared, of course).

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #8
Tobias Oed wrote:

Hi all, It's been a while!
I'm working on a very buggy piece of code that uses a lot of macros.
A bunch of them look like
#define POSITION(mw,row) (mw.positions[row])
In order to debug the thing I'd like to modify these macors so they can only be
used as rvalues and not as lvalues. If they are used as lvalues I want my
compililation to fail. Unfortunately I can't remember a nice way to do that.
As always thanks for the help!
Tobias.


#define POSITION(mw,row) (0 , mw.positions[row])

Works for numeric and for non-numeric types.

--
Er*********@sun.com
Nov 14 '05 #9
Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
Keith Thompson <ks***@mib.org> spoke thus:
#define POSITION(mw,row) (+mw.positions[row])

I usually prefer to enclose all reference to macro arguments in
parentheses to avoid operator precedence problems, but I don't *think*
it's necessary in this case (though it won't hurt to do it anyway).


It is necessary, I believe:

mynum=my_num+POSITION(a,b);

The parentheses are necessary for the above line to compile (assuming
all variables are correctly declared, of course).


(I hope you didn't really intend to use the identifiers "mynum" and
"my_num" in the same expression. 8-)})

The outer parentheses surrounding the entire macro definition are
necessary (and the original version of the macro had them). I was
referring to parentheses around each parameter reference within the
macro definition:

#define POSITION(mw,row) (+(mw).positions[(row)])

I'm fairly sure that the parentheses around row are unnecessary, since
it's already immediately surrounded by square brackets. I'm less
certain about the parentheses surrounding mw; I can't think of any
possible problems, but I could be missing something.

I would probably add the parentheses if I were writing this myself,
but this is a borderline case.

Here's a case where the lack of parentheses causes real problems;
it prints "6 * 9 = 42":

#include <stdio.h>

#define SIX 1+5
#define NINE 8+1

int main(void)
{
printf("%d * %d = %d\n", SIX, NINE, SIX * NINE);
return 0;
}

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #10
In article <news:ln************@nuthaus.mib.org>
Keith Thompson <ks***@mib.org> writs:
The outer parentheses surrounding the entire macro definition are
necessary (and the original version of the macro had them). I was
referring to parentheses around each parameter reference within the
macro definition:

#define POSITION(mw,row) (+(mw).positions[(row)])

I'm fairly sure that the parentheses around row are unnecessary, since
it's already immediately surrounded by square brackets. I'm less
certain about the parentheses surrounding mw; I can't think of any
possible problems, but I could be missing something.


There is one case where they are required.

For any such macro to work, the macro parameter "mw" must name a
structure object that has a "positions" field. This field is either
an array of size N, or a pointer that points to the first of N
sequential elements.

Suppose we redefine the macro as just:

#define POSITION(mw, row) (+mw.positions[row])

Typically this might be used as:

struct S var;

... POSITION(var, k) ...

which works as desired. The macro expands to:

(+var.positions[k])

which parses as "access the k'th element of the array or pointer
named via the positions member of the variable `var', then apply
the unary + operator". But suppose instead we have a pointer,
e.g.:

struct S *ptr = ...;

... POSITION(*ptr, k) ...

In this case, the version without parentheses expands to:

(+*ptr.positions[k])

which parses the same as:

(+(*(ptr.positions[k])))

This attempts to apply the unary "*" and binary "." operators
to the wrong operands.

Parenthesizing "mw" in the expansion gives, instead:

(+(*ptr).positions[k])

which now applies unary "*" to "ptr" as desired, and binary "."
to the structure found via *ptr.

The parentheses around "row" are in fact unnecessary. (A good
proof requires exhaustive enumeration, but a hand-wavey version
simply notes that the square brackets function identically to
parentheses, provided the expression in "row" is syntactically
legal to begin with. Writing POSITION(*ptr,]) causes problems,
but these problems are not fixable via parentheses.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #11
"Keith Thompson" <ks***@mib.org> wrote in message
The outer parentheses surrounding the entire macro definition are
necessary (and the original version of the macro had them). I was
referring to parentheses around each parameter reference within the
macro definition:

#define POSITION(mw,row) (+(mw).positions[(row)])

I'm fairly sure that the parentheses around row are unnecessary, since
it's already immediately surrounded by square brackets. I'm less
certain about the parentheses surrounding mw; I can't think of any
possible problems, but I could be missing something.


x = POSITION(*pmv, row);

Peter
Nov 14 '05 #12
Thanks for all the replies! I'll go with Jack's/Hallvard's idea:

#define RVALUE(x) ((x)+0)
#define POSITION(mw,row) RVALUE(mw.positions[row])

as it fit my purpose best.
Tobias.

Nov 14 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Romeo Colacitti | last post by:
Hi, Does anyone here have a strong understanding for the meanings of the terms "lvalue" and "rvalue" as it pertains to C, objects, and different contexts? If so please share. I've been...
1
by: Hemant Mohan | last post by:
Consider the following program snipet: <snip> typedef struct { unsigned char a ; unsigned char b ; unsigned char c ;
9
by: Kavya | last post by:
These were the questions asked to me by my friend 1. Operator which may require an lvalue operand, yet yield an rvalue. 2. Operator which may require an rvalue operand, yet yield an lvalue. ...
25
by: SRR | last post by:
Consider the following code: #include <stdio.h> #include <string.h> struct test{ char a; } funTest( void ); int main( void ) {
6
by: Lighter | last post by:
How to read "The lvalue-to-rvalue, array-to-pointer, and function-to- pointer standard conversionsare not applied to the left expressions"? In 5.18 Comma operator of the C++ standard, there is a...
6
by: Yarco | last post by:
I've alway thought lvalue means Left Value and rvalue means Right Value before i've read someone's article. It is said "lvalue = location value" and "rvalue = read value". Which one is right, then?
0
by: Frank Bergemann | last post by:
Using gcc-4.3.0 it silenty(!) invokes the Test::operator=(Test const& t) for Test a; Test b = std::move(a); Test c = std::moved(T()); - if i disable the Test::operator=(Test&& t) in source...
0
by: Jerry Coffin | last post by:
In article <9f60e411-a5b1-4571-9d3d-005432378cd4@ 56g2000hsm.googlegroups.com>, aitorf666@gmail.com says... That's not the real reason for rvalue references. There are two primary reasons for...
9
by: usao | last post by:
Does anyone have an example of how to create a class which describes and array, such that I can use the subscript operator on both the left and right side of an assignment statement? This is as...
2
by: Chad | last post by:
The following question actually stems from an old Chris Torek post. And I quote from the following old CLC url ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.