If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

The advantage to using a define in this case, as opposed to a function, is that it's significantly simpler to do this than it is to create a malstr(char**) function and return the right addresses. This is more familiar and easy to understand as a novice programmer.

Re: #define vs const - where to use #define?

If thats C, then you dont cast the return value of malloc, as doing so may hide stdlib.h not getting included. If its C++ then an inline function is almost certainly a better idea than a macro which has no respect for scope.

Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors. Always use [code] code tags [/code] to make code legible and preserve indentation.Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

Re: #define vs const - where to use #define?

Originally Posted by Russco

If thats C, then you dont cast the return value of malloc, as doing so may hide stdlib.h not getting included. If its C++ then an inline function is almost certainly a better idea than a macro which has no respect for scope.

I've never had stdlib get lost with such a macro, and scope is only an issue if you poorly separate your classes.

Re: #define vs const - where to use #define?

Originally Posted by Hoobs

I've never had stdlib get lost with such a macro

It is not the macro: it is the cast. If <stdlib.h> is not included (or a prototype otherwise provided), then malloc would not have been declared, which means that in C it would be assumed to have a return type of int. This can prove problematic, but if you do not cast the return value of malloc, a compiler is likely to complain, and this serves as a warning to rectify what you forgot.

But removing the cast creates another potential pitfall: if say you later change myString from char* to wchar_t*, but forget to change the malloc, then you might end up allocating insufficient memory. You should thus write:

Code:

str = malloc(sizeof(*str) * size);

That would make more sense than writing sizeof(char) anyway, since sizeof(char) is always 1. But this is a common idiom that beginners should learn, rather than hiding it in a macro that they (and everyone else) would also have to learn, as Lindley pointed out.

Originally Posted by Hoobs

and scope is only an issue if you poorly separate your classes.

It is not scope that is the issue: it is the lack of scope that is the issue, i.e., macro names do not obey the rules of scope. If you followed the common convention of fully capitalising macro names and named your macro MALSTR then this problem would be somewhat mitigated, but you did not.

Re: #define vs const - where to use #define?

So I'm now wondering when should #define be used, if ever?

In the case of constants, pretty much never. You'll really only use it for macros in cases where an inline function won't work. For example, it's common for exception classes to take the filename and line number where the error occurred in addition to a message. So you might have something like

Re: #define vs const - where to use #define?

I typically don't use defines for much, however I have had a few cases where they have been useful.

In C++ (not C) enums do not have automatic intrinsic bit operators, so I defined a macro that would define the bit operators for a specific enum type.

Also, to enforce full scope resolution and get around non-standard extension accessing a union member by fully qualified name, and to avoid clashing names in unions (Default is one that happens with me a lot) I defined a macro to start an enum definition in a namespace, and a macro that ends the enum definition and typedef's the inner type to the specified.

Re: #define vs const - where to use #define?

One case when I've found macros to be "superior" to inline functions is when one of the arguments isn't just a statement, but an entire chunk of code. If you've got several bits of code which are almost identical but need to do difficult-to-parameterize things differently in one specific place, a macro taking the code snippet might be appropriate.

Of course, the utility of that is reduced now that C++1x has incorperated boost::function and lambda expressions into the standard.