473,698 Members | 2,754 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Declaring war on macros


const unsigned short int AmountHumanAdul tTeeth = 32;

inline unsigned short int AddFive(unsigne d short int Numbr)
{
return Numbr + 5;
}
USE THESE!!
I've heard a stupid argument before that:

#define AmountHumanAdul tTeeth 32
is better because it saves memory, ie. there's no variable declared which
takes up memory.

Enlightenment time:

Upon execution, the ENTIRE program is loaded into memory, all the code for
all of the functions, all of the numbers and strings littered throughout the
code, eg.:

cout << "Hello";
are all loaded into memory. Thus, somewhere in memory you will have "Hello".

Thus, concordantly and therefore, you most certainly _are_ taking up memory
by using a macro instead of a const global variable. Furthermore, you may
even use MORE memory with a macro; For example, if you have the number "5"
littered throughout your program, you're not necessarily guaranteed that all
of the "5"'s will be taken from the same place in memory! There may be a
unique "5" in memory for every single seperate time you use it. Const global
variables are superior in EVERY WAY, BAR NONE.

As for macro functions: Similarly, there is absolutley NO JUSTIFICATION for
their use. Inline functions are superior in every way, BAR NONE!
Jul 22 '05 #1
28 1631
JKop wrote:

const unsigned short int AmountHumanAdul tTeeth = 32;

inline unsigned short int AddFive(unsigne d short int Numbr)
{
return Numbr + 5;
}
USE THESE!!
I've heard a stupid argument before that:

#define AmountHumanAdul tTeeth 32
is better because it saves memory, ie. there's no variable declared
which takes up memory.

Enlightenment time:

Upon execution, the ENTIRE program is loaded into memory, all the code
for all of the functions, all of the numbers and strings littered
throughout the code, eg.:

cout << "Hello";
are all loaded into memory. Thus, somewhere in memory you will have
"Hello".

Thus, concordantly and therefore, you most certainly _are_ taking up
memory by using a macro instead of a const global variable.
Furthermore, you may even use MORE memory with a macro; For example,
if you have the number "5" littered throughout your program, you're
not necessarily guaranteed that all of the "5"'s will be taken from
the same place in memory! There may be a unique "5" in memory for
every single seperate time you use it.
Actually, they might not even be taken from memory at all, or let's
better say from data memory. If you have a variable, it resides in
memory somewhere, and the compiler might need to create an instruction
that loads it from there. On assembler level that means there is an
instruction that says something like "load the value from memory
address 0xdeadbeef". If it's a macro, the literal 5 is usually directly
included in the instruction on assembler level, so the assembler
instruction would say "load the value 0x00000005". As you can see, the
instructions can be (and often are) of the same size, but in the first
case, you additionally need a memory location to save the value to (in
my example at address 0xdeadbeef). However, a constant can be used
"inline", just like the macro, so any decent compiler can also directly
put the literal 5 into the assembler instruction unless you try to take
its address, in which case the compiler needs to put it into data
memory to ensure it actually has an address. Since constants have
internal linkage by default, it would mean that each translation unit
that needs the address of the constant would need to store its own copy
of it in memory.

Anyway, in most cases, it's just not worth thinking about whether your
program will need a few bytes more. There are lots of much more
important reasons to use constants instead of macros.
Const global variables are superior in EVERY WAY, BAR NONE.
Some would say "global variables are evil".
As for macro functions: Similarly, there is absolutley NO
JUSTIFICATION for their use. Inline functions are superior in every
way, BAR NONE!


Right.

--
How come Superman could stop bullets with his chest,
but always ducked when someone threw a gun at him?

Jul 22 '05 #2
On Wed, 28 Apr 2004 09:29:28 GMT, JKop <NU**@NULL.NULL > wrote:
As for macro functions: Similarly, there is absolutley NO JUSTIFICATION for
their use. Inline functions are superior in every way, BAR NONE!


Rolf's commented on the rest of it, but I take slight issue with the
above. Inline functions are superior only when it is possible to write
an inline function to do what the macro was doing. In many cases it
isn't. (For an example of advanced usage of preprocessor macros, check
out the boost library at www.boost.org)

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #3
JKop <NU**@NULL.NULL > wrote in message news:<YP******* ***********@new s.indigo.ie>...
As for macro functions: Similarly, there is absolutley NO JUSTIFICATION for
their use. Inline functions are superior in every way, BAR NONE!


inline int BYTES_NEEDED_FO R_INTS ( int no_ints )
{
return no_ints * sizeof(int);
}
typedef char[ BYTES_NEEDED_FO R_INTS( 5 ) ] int5;

Does your compiler accept this?

Regards,
Michiel Salters
Jul 22 '05 #4
JKop wrote:
[snip stuff that I don't want to comment on]
As for macro functions: Similarly, there is absolutley NO JUSTIFICATION for
their use. Inline functions are superior in every way, BAR NONE!


Please describe how to implement the (standard library) offsetof macro in terms
of an inline function that is 'far superior' (or at least equally inferior!).
Jul 22 '05 #5
JKop wrote:

const unsigned short int AmountHumanAdul tTeeth = 32;

inline unsigned short int AddFive(unsigne d short int Numbr)
{
return Numbr + 5;
}

USE THESE!!

I've heard a stupid argument before that:

#define AmountHumanAdul tTeeth 32

is better because it saves memory, ie. there's no variable declared which
takes up memory.

Enlightenment time:

Upon execution, the ENTIRE program is loaded into memory, all the code for
all of the functions, all of the numbers and strings littered throughout the
code, eg.:

cout << "Hello";

are all loaded into memory. Thus, somewhere in memory you will have "Hello".

Thus, concordantly and therefore, you most certainly _are_ taking up memory
by using a macro instead of a const global variable. Furthermore, you may
even use MORE memory with a macro; For example, if you have the number "5"
littered throughout your program, you're not necessarily guaranteed that all
of the "5"'s will be taken from the same place in memory! There may be a
unique "5" in memory for every single seperate time you use it. Const global
variables are superior in EVERY WAY, BAR NONE.

As for macro functions: Similarly, there is absolutley NO JUSTIFICATION for
their use. Inline functions are superior in every way, BAR NONE!


Do you consider:

#if defined (CODE_1)
// some code
#else if defined (CODE_2)
// other code
#endif

the use of macros?
Jul 22 '05 #6
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:c6******** *****@news.t-
JKop wrote:
Thus, concordantly and therefore, you most certainly _are_ taking up
memory by using a macro instead of a const global variable.
Furthermore, you may even use MORE memory with a macro; For example,
if you have the number "5" littered throughout your program, you're
not necessarily guaranteed that all of the "5"'s will be taken from
the same place in memory! There may be a unique "5" in memory for
every single seperate time you use it.


Actually, they might not even be taken from memory at all, or let's
better say from data memory. If you have a variable, it resides in
memory somewhere, and the compiler might need to create an instruction
that loads it from there. On assembler level that means there is an
instruction that says something like "load the value from memory
address 0xdeadbeef". If it's a macro, the literal 5 is usually directly
included in the instruction on assembler level, so the assembler
instruction would say "load the value 0x00000005". As you can see, the
instructions can be (and often are) of the same size, but in the first
case, you additionally need a memory location to save the value to (in
my example at address 0xdeadbeef). However, a constant can be used
"inline", just like the macro, so any decent compiler can also directly
put the literal 5 into the assembler instruction unless you try to take
its address, in which case the compiler needs to put it into data
memory to ensure it actually has an address. Since constants have
internal linkage by default, it would mean that each translation unit
that needs the address of the constant would need to store its own copy
of it in memory.


Very good answer. In my experience it is very rare to use address of a
constant, such as these:

const double pi=3.1415926535 8979323846;
namespace physics
{

const double hbar=1.05457259 0e-34; // Joule*second
const double hbarsquared=1.1 12123347e-68; // (Joule*second)^ 2
const double boltzman=1.3806 58000e-23; // Joule/Kelvin
const double elemcharge=1.60 2177330e-19; // Coulomb
const double lightspeed=2997 92458.0; // meter/second
const double dielectric0=8.8 54187817e-12; // Coulomb^2/Newton/meter^2
const double electron_mass=9 .1093897e-31; // kilogram

}

How come Superman could stop bullets with his chest,
but always ducked when someone threw a gun at him?


Good question.
Jul 22 '05 #7
"Julie" <ju***@nospam.c om> wrote in message
news:40******** *******@nospam. com...
JKop wrote:

As for macro functions: Similarly, there is absolutley NO JUSTIFICATION for their use. Inline functions are superior in every way, BAR NONE!


Do you consider:

#if defined (CODE_1)
// some code
#else if defined (CODE_2)
// other code
#endif

the use of macros?


Don't know about JKop, but this seems fine to me. How else to have code
compiled for Windows only, or Borland only, or debug mode only?
Jul 22 '05 #8
"Julie" <ju***@nospam.c om> wrote in message
news:40******** *******@nospam. com...
JKop wrote:
As for macro functions: Similarly, there is absolutley NO JUSTIFICATION for their use. Inline functions are superior in every way, BAR NONE!


Please describe how to implement the (standard library) offsetof macro in

terms of an inline function that is 'far superior' (or at least equally

inferior!).

Use the C++ pointer to member mechanism. The offsetof is only for C.
Jul 22 '05 #9
"Michiel Salters" <Mi************ *@logicacmg.com > wrote in message
JKop <NU**@NULL.NULL > wrote in message news:<YPKjc.587 8

As for macro functions: Similarly, there is absolutley NO JUSTIFICATION for their use. Inline functions are superior in every way, BAR NONE!


inline int BYTES_NEEDED_FO R_INTS ( int no_ints )
{
return no_ints * sizeof(int);
}
typedef char[ BYTES_NEEDED_FO R_INTS( 5 ) ] int5;

Does your compiler accept this?


BTW, is int5 above misplaced?

Anyway, that should be rewritten using structs and static const so that you
can use compile time constants. So basically I agree with JKop.

template <size_t N>
struct bytes_needed_fo r_ints
{
static const size_t result = N * sizeof(int);
};

typedef char int5[ BYTES_NEEDED_FO R_INTS<5>::resu lt ];
Jul 22 '05 #10

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

Similar topics

21
2984
by: Chris Reedy | last post by:
For everyone - Apologies for the length of this message. If you don't want to look at the long example, you can skip to the end of the message. And for the Python gurus among you, if you can spare the time, I would appreciate any comments (including words like evil and disgusting, if you think they are applicable :-}) on the example here. Kenny -
699
33941
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
37
2801
by: michele.simionato | last post by:
Paul Rubin wrote: > How about macros? Some pretty horrible things have been done in C > programs with the C preprocessor. But there's a movememnt afloat to > add hygienic macros to Python. Got any thoughts about that? "Movement" seems quite an exaggeration. Maybe 2-3 people made some experiments, but nobody within the core Python developers seems to be willing to advocate the introduction of macros. > Why should you care whether the...
3
1350
by: Peng Yu | last post by:
Hi, I'm trying to define inline_test::test() to be inline. But I always got errors. I know that if I define inline_test::test() in inline_test.h, there are no errors. But I still would rather to put the member functions in *.cc files not the *.h files. Do you know how to do that? The following paragraphs are the compile error and the source codes.
8
2773
by: Chris Barts | last post by:
Let's say we have a library with an opaque data type implemented as a struct in the library's C source file. The definition of the struct isn't duplicated in the header included by the programs that make use of this library. Is it legal for the header and the programs that include it to declare pointers to this struct for which no definition is in scope and to pass those pointers into functions declared in that header?
12
3878
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that looked sensible, but it didn't work right. Here is a simple example of what I'm trying to accomplish: // I have a hardware peripheral that I'm trying to access // that has two ports. Each port has 10 sequential // registers. Create a...
33
8885
by: Robert Seacord | last post by:
When writing C99 code is a reasonable recommendation to use inline functions instead of macros? What sort of things is it still reasonable to do using macros? For example, is it reasonable to write type generic functions macros? #define CUBE(I) ( (I) * (I) * (I) ) Is there some more concise set of things where inline functions should be used instead of macros? Multi-statement macros, for example?
5
8829
by: Rahul B | last post by:
Hi, I have very little knowledge about creating Procedures/functions in DB2. When i tried to create the test function like CREATE FUNCTION GET_TEST (P_TEST_ID INTEGER, P_SEL_OR_SORT INTEGER,
8
4588
by: nguillot | last post by:
Hello. If I have the following classes: class B {}; typedef B tB; if A is: class A
0
8609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8901
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7739
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5862
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.