473,472 Members | 2,148 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

conditional operator again

Hi guys,

I posted a query yesterday and got some good responses. However,
before posting
I did some simplications in my code. Now, I am posting what I exactly
want and what
I have done. I want your views if I have done it correctly or not ?
Are there any loopholes ?
Or can it be done in a better way ?

My objective is to convert a function func() to macro for efficiency.

The function func() is:

char ** func( int var, char **ptr)
{
if(var == 1)
return(ptr);
if(var == 2) {
if(*ptr == NULL)
return NULL;
else
return ptr;
}
func2(var, ptr);
}

I am using this function as follows:

int main(void)
{
char **ptr, **res;
int flag;
/* some code */
res = func(flag,ptr);
}

I have written the following macro.

#define func(flag, ptr) \
((flag == 1 || flag == 2) \
? (flag == 1) \
? (char **)ptr \
: ( flag == 2 && (*ptr == NULL) )
? NULL \
: (char **)ptr \
: (char**)func2(flag, ptr))
I want to know, if this would work fine or not ? Is there a better way
of doing this ?
thanks a lot in advance for any help .....

Jun 8 '06 #1
3 1402
Hi

ju**********@yahoo.co.in wrote:
My objective is to convert a function func() to macro for efficiency.
Is it necessary?
Have you tried to make the function static and to put it in your header
file?
The function func() is:

char ** func( int var, char **ptr)
{
if(var == 1)
return(ptr);
if(var == 2) {
if(*ptr == NULL)
undefined as soon as ptr == NULL.
return NULL;
else
return ptr;
}
func2(var, ptr);
}
did you mean "return func2(var, ptr)"?
I have written the following macro.

#define func(flag, ptr) \
((flag == 1 || flag == 2) \
? (flag == 1) \
? (char **)ptr \
: ( flag == 2 && (*ptr == NULL) )
? NULL \
: (char **)ptr \
: (char**)func2(flag, ptr))
I want to know, if this would work fine or not ? Is there a better way
of doing this ?


a) You lose some type-safety.
b) With macros, you should _always_ enclose the parameters in parenthesis:
#define func(flag, ptr) (( (flag) == 1 || (flag) == 2) ...
c) Your parameters may be evaluated more than once, which might lead to
problems (especially as you call your macro "func"):
ptr2 = func(++flag, ptr); // undefined
d) It's terrible to read.

All in all, don't do it.

Markus

Jun 8 '06 #2

Markus Moll wrote:
a) You lose some type-safety.
b) With macros, you should _always_ enclose the parameters in parenthesis:
#define func(flag, ptr) (( (flag) == 1 || (flag) == 2) ...
c) Your parameters may be evaluated more than once, which might lead to
problems (especially as you call your macro "func"):
ptr2 = func(++flag, ptr); // undefined
d) It's terrible to read.

All in all, don't do it.


Thanks for your suggestions. Just to clarify, nowhere in my code func()
will be
called as
ptr = func(++flag, ptr). In fact, flag is not a integer variable. It is
always called as
ptr = func(HASH_DEF_VALUE, ptr). where HASH_DEF_VALUE may be 1, 2 or
3.
And, also *ptr will never be NULL.

Jun 8 '06 #3
ju**********@yahoo.co.in wrote:
My objective is to convert a function func() to macro for efficiency.
Well, the first question I have is what makes you think that this
will be worth it? You have done the measurements, yes? You have
done refactoring? You don't have a working `inline`?
The function func() is:

char ** func( int var, char **ptr)
{
if(var == 1)
return(ptr);
if(var == 2) {
if(*ptr == NULL)
return NULL;
else
return ptr;
}
func2(var, ptr);
}
Really? Are you /sure/ this is the code you have? You didn't
miss out a `return` before that last call? [And I truly hope
that your actual functions aren't called `func` and `func2`.
I know this sounds picky, but knowing what this is /about/
can help a lot.]

So the /first/ line of attack, if you're hell-bent on this
transformation, is the obvious:

return
var == 1 ? ptr
: var == 2 ? (*ptr == NULL ? NULL : ptr)
: func2( var, ptr )
;

All your tests should still pass. Then the middle three
lines can become your macro body, with suitable guards:

#define FUNC( var, ptr ) \
((var) == 1 ? (ptr) \
: (var) == 2 ? (*(ptr) == NULL ? NULL : (ptr)) \
: func2( (var), (ptr) ) \
)
I have written the following macro.

#define func(flag, ptr) \
((flag == 1 || flag == 2) \
? (flag == 1) \
? (char **)ptr \
: ( flag == 2 && (*ptr == NULL) )
? NULL \
: (char **)ptr \
: (char**)func2(flag, ptr))
Hell's teeth, that looks complicated. What are all those casts for?
Why fiddle around with the tests?
I want to know, if this would work fine or not ? Is there a better way
of doing this ?


Dunno. I don't know what it's /for/. But I'd lay odds it'll make the
program go at pretty much the same speed as it already does.

--
Chris "seeker" Dollin
"A facility for quotation covers the absence of original thought." /Gaudy Night/

Jun 8 '06 #4

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

Similar topics

4
by: TheKeith | last post by:
I just wrote the following script for something I'm working on: ---------------------------------------------------------------------------- ------------------- <html> <head> <script...
6
by: Mahesh Tomar | last post by:
Please see the code below :- void func() { unsigned char x,y,z=1; (z==1) ? (x) : (y) = 1; /* Compiles OK */ ((z==1) ? (x) : (y)) = 1; /* Compiler generates an error "Variable expected" */ }
30
by: bnp | last post by:
Hi, Is possible to use functions in conditional operator as ashown below. ..... ..... int fun1() { // body }
4
by: mux | last post by:
Hi I found out that the following piece of code throws an error. 1 #include "stdio.h" 2 3 int main() 4 { 5 int a,b; 6 a= 10;
6
by: Chris Dunaway | last post by:
Consider this code (.Net 2.0) which uses a nullable type: private void button1_Click(object sender, System.EventArgs e) { DateTime? nullableDate; nullableDate = (condition) ? null :...
9
by: Marty | last post by:
Hi, Does using the the conditional operator (?:) instead of the common "if" statement will give a performance gain in a C# .NET 2003 application (even in C# .NET 2005?). What is the advantage...
5
by: paulo | last post by:
Can anyone please tell me how the C language interprets the following code: #include <stdio.h> int main(void) { int a = 1; int b = 10; int x = 3;
15
by: Nicholas M. Makin | last post by:
I was just thinking that I understood the conditional operator when I coded the following expecting it to fail: int a= 10, b= 20, c= 0; ((a < b) ? a : b) = c; // a=0 a=20; b= 10; ((a < b) ? a...
3
by: somenath | last post by:
Hi All, I have one question regarding the conditional operator. In the draft C99 standard it is mentioned that "1 The following are the sequence points described in 5.1.2.3: -- The call to a...
13
by: Neal Becker | last post by:
In hindsight, I am disappointed with the choice of conditional syntax. I know it's too late to change. The problem is y = some thing or other if x else something_else When scanning this my...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.