Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 23rd, 2010, 9:41 PM   #1
him_over_there
Newbie
 
Join Date: Sep 2010
Posts: 15
Rep Power: 0 him_over_there is on a distinguished road
I have a #define question

I was looking for some snippets of code to try and understand my professor's code example, and I came across something very weird (to me).

#define sem_init(a,b,c) sema_init(a,b,c,NULL)

I can't seem to find anything that explains what this #define does. It's in the middle of an #ifdef....#endif

This is less about what those functions do and more about the statement itself.
him_over_there is offline   Reply With Quote
Old Oct 23rd, 2010, 9:45 PM   #2
Adak
Hobby Coder
 
Join Date: May 2006
Posts: 1,181
Rep Power: 13 Adak will become famous soon enough
Re: I have a #define question

Simple enough. If def (condition) is true, then the string:
sem_init(a,b,c) , will be replaced by the string: sema_init(a,b,c,NULL)

Looks like a compatibility fix, when a function needs no NULL parameter, and when it does need it.
Adak is offline   Reply With Quote
Old Oct 23rd, 2010, 9:53 PM   #3
Ancient Dragon
Achieved Level 70

 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 4,345
Rep Power: 10 Ancient Dragon will become famous soon enoughAncient Dragon will become famous soon enough
Re: I have a #define question

In the example you posted, a, b, and c are function parameters of unspecified type, and the preprocessor will replace all occurrences of semi_init with sema_init()

However it's frequently dangerous to pass parameters like that because sometimes they may be evaluated more than once. So most programmers will enclose each parameter in paranthes, like this:
#define sem_init(a,b,c) sema_init((a),(b),(c),NULL) to prevent side-affects errors.

Putting the #define within #ifdef and #endif block tells the preprocessor to ignore the #define if the #ifdef or #ifndef is false.
#ifndef sem_init
#define sem_init(a,b,c) sema_init((a),(b),(c),NULL)
#endif
__________________
PFO's FAQ is here
Forum Rules

There is no cow level (Diablo III)
If you never push yourself you will never improve (Diablo III)
Ancient Dragon is offline   Reply With Quote
Old Oct 23rd, 2010, 9:54 PM   #4
him_over_there
Newbie
 
Join Date: Sep 2010
Posts: 15
Rep Power: 0 him_over_there is on a distinguished road
Re: I have a #define question

Wow, I can't believe I'm just now finding out about this! I've been programming for 2 years and just now find this out? lol

Thanks a lot
him_over_there is offline   Reply With Quote
Old Oct 23rd, 2010, 11:50 PM   #5
Thanatopsis
Mainframe Developer
 
Join Date: Mar 2010
Location: Illinois
Posts: 214
Rep Power: 8 Thanatopsis is on a distinguished road
Re: I have a #define question

Its probably eluded you so long because #define is often discouraged because of all the little nasty monkey wrenches it can throw at you. But its so nice sometimes. Just keep in mind that it really is just a "brainless" replace that takes place before the "real" compiling happens.
__________________
"Beware of bugs in the above code; I have only proved it correct, not tried it."
-Don Knuth-
Thanatopsis is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
OpenGL Broax C++ 7 Jul 26th, 2005 7:47 PM
How to post a question nnxion C++ 10 Jun 3rd, 2005 12:53 PM
How to post a question nnxion C++ 0 Jun 3rd, 2005 9:55 AM
How to post a question nnxion C 0 Jun 3rd, 2005 9:55 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:03 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC