ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hmm ... my question would be why you would want too?
I assume you are learning and so using some kind of book / web page.
You seem to have jumped in a little deep and I think you need to start at the beginning.
Basically you are outside any defined function and therefore deemed to be setting global
variables. If you expand your #define on the line where you abc(i), what would you get?
Once expanded, if we assume it could work, what value would there be in executing that code there?
#include<iostream>
using namespace std;
#define my_abs(x) ( ( x < 0 ) ? ( -x ) : x )
int i=10;
int abs_i = my_abs(i) ;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
represents more like what you were trying to do, in the sense of
1) Not replace a standard "function"
2) Not throw away the chosen value
I'm still not quite clear on why you'd want to do what you're
doing, unless what you're showing us in incomplete, you intend
to change it later, you're just focusing on the error.
As far as the macro, it might be helpful for you to see what
happens when it's expanded. If you compile it this way:
Code:
gcc -E program_name.C -o program_name.Cpp
ignore most of the junk you'll see in the file, go to
the end of the file, in that area of the file, you'll see
actual expansion of your macro.
#include<iostream>
using namespace std;
#define abc(x) ( ((x) < 0) ? -(x) : (x) )
int i=-10;
int j = abc(i);
int main()
{
cout << j << endl;
cout << i << endl;
cout << abc(i) << endl;
cout << i << endl;
cout << "Hello World!" << endl;
return 0;
}
A #define can be used in the global space when initialising a global variable. But you can't put statements in the global space. Just try and put i++; before main, you'll get a compile time error.
Your line abc(i) in the global space was the same as putting a statement there, hence the errors that you received.
Finally, it's good to know about #define's but it is generally agreed that they shouldn't be used. C++ has a better alternative, namely inline'd functions which are less prone to bugs.
First of all thanks I got it that C++ is not allowing to do what basically I am trying to do.
Just for clarification, the program I put that is just for example not the actual one.
I even believe C have the same constrain too else C++ could have allowed that too ( b'coz we can compile C program with g++ too)
I have some other doubt (I don't know it is right to place to ask it) in kernel(2.6.23) code.
In mm/swapfile.c part of code looks like
Code:
#include <asm/tlbflush.h>
#include <linux/swapops.h>
DEFINE_SPINLOCK(swap_lock);
unsigned int nr_swapfiles;
long total_swap_pages;
static int swap_overflow;
In include/linux/spinlock_types.h DEFINE_SPINLOCK() is there as
Code:
#define SPIN_LOCK_UNLOCKED __SPIN_LOCK_UNLOCKED(old_style_spin_init)
#define RW_LOCK_UNLOCKED __RW_LOCK_UNLOCKED(old_style_rw_init)
#define DEFINE_SPINLOCK(x) spinlock_t x = __SPIN_LOCK_UNLOCKED(x)
#define DEFINE_RWLOCK(x) rwlock_t x = __RW_LOCK_UNLOCKED(x)
How this is getting resolved and getting compiled without any error etc ?
Thanks
Last edited by ravishekhar.82; 04-06-2010 at 06:03 AM.
This would be some of the trickiness that graemef was referring too, but essentially you need to track back through the
previous #defines as well to get the full picture. So if you use the following you should be able to workout what the final line looks like:
I am putting first code again which is in mm/swapfile.c :
Code:
#include <asm/tlbflush.h>
#include <linux/swapops.h>
DEFINE_SPINLOCK(swap_lock);
unsigned int nr_swapfiles;
long total_swap_pages;
static int swap_overflow;
Why there is no error at the line (Bold One ) where DEFINE_SPINLOCK(swap_lock);. It is also a preprocessor as was mine abc(x)( if I am not wrong ).
You have to do some of the work here otherwise we will end up with how long is a piece of string
(ie the questions will never end as this is the same concept just ongoing)
Why there is no error at the line (Bold One ) where DEFINE_SPINLOCK(swap_lock);. It is also a preprocessor as was mine abc(x)( if I am not wrong ).
This is because the #define expands to the definition of a variable. x is declared to be of type spinlock_t and it is assigned the value, which is returned by the expanding of the macro __SPIN_LOCK_UNLOCKED(x).
This is one of the reasons macros are frowned upon by many people, they hide what is really going on.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.