Not really - NULL is a constant, but its use in general programming practice can be temporary.Quote:
NULL is constant, not temporary.
Printable View
Not really - NULL is a constant, but its use in general programming practice can be temporary.Quote:
NULL is constant, not temporary.
party pooper... if you think about it, NULL (along with all other 'constant' variables) is temporary, because it only exists as long as the program is running :DQuote:
Originally Posted by quzah
and no, I'm not talking about NULL as a universal thing, just as it applies to a C/C++ program.
more fun:Code:#include<iostream>
#undef NULL
int main()
{
#ifdef NULL
int*ptr=NULL;
std::cout<<"NULL defined as "<<ptr<<std::endl;
#else
std::cout<<"NULL not defined"<<std::endl;
#endif
return 0;
}
There aren't any white space rules other than the usual. The only "newline" issue is that you escape multi-line macros. Like so:Quote:
And...aren't there rules about white space, new lines, commas, semi-colons, etc. when defining macros? Could somoene explain these a little? Isn't it that you never use semi-colons, comments, or double-quotes?
The last line doesn't have a \, all others do.Code:#define FOO(bar) line1 \
line2
A macro is simply a compile time text subsutution. The ## macros smash things together. Like so:
Then you do something like this:Code:#define FOO(bar,baz) bar ## baz
int var1, var2, var3
FOO( var, 1 ) = 10;
They're multi-line just for the hell of it. They don't need to be. The only thing about white space is you need your #define or #include or what not to start on the left margin. Some compilers will let you get away with it indented for some of the things, but IIRC, th're supposed to start on the left most character.Code:#include<iostream>
#define FOO(bar,baz) bar ## \
baz
#define BAR(foo,baz) foo # \
baz
int main( )
{
int var1, var2, var3;
FOO( var, 1 ) = 10;
FOO( var, 2 ) = 20;
FOO( var, 3 ) = 30;
std::cout << BAR("var",1) << " is: " << FOO( var, 1 ) << std::endl;
std::cout << BAR("var",2) << " is: " << FOO( var, 2 ) << std::endl;
std::cout << BAR("var",3) << " is: " << FOO( var, 3 ) << std::endl;
return 0;
}
Code:#include <GOOD>
Code:#include <bad>
Quzah.
NULL is a constant. You've just said so. Your use of it is temporary, but NULL remains constant. There's no two ways about it.Quote:
Originally Posted by laserlight
Waxing philisophical...
Quzah.
We're just playing with language :pQuote:
NULL is a constant. You've just said so. Your use of it is temporary, but NULL remains constant. There's no two ways about it.
I didnt say NULL is a temporary, I said NULL is temporary.
I didnt say NULL is constant, I said NULL is a constant.
Macros, if used correctly and documented correctly, can save a lot of work. And no, if the macro isn't simple, it cannot be replaced by an inline function.
Challenge 1: Write an inline function that does the same as
orCode:#define PRINT_NAME_AND_NUMERICAL_VALUE( name ) printf("%s is %d", #name, name );
Looks ugly for a little class, but write a class with 100 variables and accessors and we will see.Code:#define DECLARE_VARIABLE_AND_ACCESSORS( name ) int m_i##name; \
int GetNumber##name() { return m_i##name; }\
void SetNumber##name( int val ) { m_i##name = val; }
Or write an inline function, that will be compiled conditionally
This might be the job of a profiler... but you don't want to have a profiler on your production system.Code:#ifdef _PERF_
MEASURE_PERFORMANCE( statement, description ) [performance measuring and output code]
#else
MEASURE_PERFORMANCE( statement, description ) statement;
#endif
I don't see any alternatives to Macros. You probably frown upon them while others get work done using them :)
Actually the first macro:
Wouldn't be to hard - not that I've tried - as you can surely use a stringstream to get the numerical value of name? You can use a stringstream to write an itos function.Code:#define PRINT_NAME_AND_NUMERICAL_VALUE( name ) printf("%s is %d", #name, name );
Actually I'm going to try that now, I'll post it up if I have any joy.