• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Servant of the Lord

NULL vs 0


44 posts in this topic

NULL specifies intent.

There are also a few edge cases (especially on embedded hardware) where 0 != (void*)0 and the automatic conversion doesn't quite work right.
2

Share this post


Link to post
Share on other sites
I'm using [font=courier new,courier,monospace]0[/font] even though my compiler supports [font=courier new,courier,monospace]nullptr[/font], and every single time I have a bad conscience (knowing that [font=courier new,courier,monospace]nullptr [/font]is the correct-correct thing to use). However, [font=courier new,courier,monospace]0[/font] is quick to type, and it's an idiom I've gotten used to during the years. The C++ standard makes the conversion from a zero literal to the null pointer of a type well defined too, so meh... one cannot even argue that it's wrong to use [font=courier new,courier,monospace]0[/font]. Actually, it's quite correct.

[font=courier new,courier,monospace]NULL [/font]on the other hand has two properties that turn me off. It's a macro, and it's an all-caps name. I really, really, really hate reading all-caps text.
-2

Share this post


Link to post
Share on other sites
Zero (0). Because [url="http://www.stroustrup.com/bs_faq2.html#null"]what would Stroustrup do[/url]? Edited by smr
1

Share this post


Link to post
Share on other sites
0 because I never know what header has the definition for NULL. I know it's a lame argument, but it's true. Then I read that Stroustrup prefers 0 also, so 0 it is.

Also, it doesn't really matter much for how I write code these days. For the most part I only use null pointers to pass them to functions like time(), or some Unix system calls.
0

Share this post


Link to post
Share on other sites
[font=times new roman,times,serif]NULL all the way. My thoughts:[/font]

[font=times new roman,times,serif]NULL [i]does[/i] imply intent in a way that is far superior to 0. It is clean, concise, and like its C roots, is consistent with macro naming conventions. It is much easier to accidentally create ambiguity with 0 as in the OP's example. While I agree that "nullptr" might also adequately solve the semantic problem, it is redundant to NULL's precedent, longer to type, and doesn't stand out as much. Also, it is not supported everywhere yet--and while I appreciate that that means you should get a better compiler, why do we even need a new keyword when we already have one?[/font]

[font=times new roman,times,serif]As a macro, it can easily be changed for platform-specific purposes, and does not incur the overhead of a full variable, even with a crappy compiler. I suppose pathologically contrived macro redefinitions could cause a problem, but this could be solved in C++ by making NULL a keyword instead of a macro--[i]not[/i] making a completely [i]new[/i] keyword that does exactly the same thing.[/font]

[font=times new roman,times,serif]Further, macros are not inherently evil. When used incompetently, sure, they can cause many many problems. But macros were [i]invented[/i] to make coding easier and more understandable. Whether using macros to write most of your code is good practice is a different argument, but in the case of NULL it's basically what macros were invented [i]for[/i]--"to abstract away magic numbers without additional runtime overhead".[/font]

[font=times new roman,times,serif]C and C++ are different languages, but there's no fundamental reason why they should be more different than they need to be. Regularly, I write small modules in C, refactoring into C++ when a more elaborate design becomes necessary. NULL and nullptr serve the same purpose, so why can't we use the one that everyone already uses? Using nullptr implies that tons of perfectly good, readable code would need to get rewritten to handle the new standard--unless of course NULL and nullptr end up coexisting side-by-side, which would be [i]worse[/i].[/font]
1

Share this post


Link to post
Share on other sites
0 because easier to write.
No need read too much in to this simple issue.

nullptr in c++11 is nice tho.
0

Share this post


Link to post
Share on other sites
SiCrane - I don't think it's true.
GCC (also Visual Studio) defines NULL like this: "#define NULL ((void *)0)" in stddef.h.
So:
[code]c:\ test>type a.cpp

#define NULL ((void*)0)
void foo(int);
void foo(void*);
int main()
{
foo(NULL);
}

c:\ test>g++ a.cpp
C:\Users\XXX\AppData\Local\Temp\ccAUKoxR.o:a.cpp:(.text+0x16): undefined reference to `foo(void*)'
collect2.exe: error: ld returned 1 exit status[/code]

foo(NULL) correctly calls foo(void*). Edited by Martins Mozeiko
0

Share this post


Link to post
Share on other sites
You know, I've always wondered, when they created the C++11 spec, why did they create "nullptr" instead of just using the reserved but functionless "null" keyword that already exists?

Was it because people were creating their own functionality for it via macros or what have you?
-1

Share this post


Link to post
Share on other sites
I used NULL in the past, but now I'd just define nullptr if it wasn't available. So that when you transition to a modern compiler, your code base stays consistent.
1

Share this post


Link to post
Share on other sites
[quote name='Flimflam' timestamp='1355524803' post='5010765']
You know, I've always wondered, when they created the C++11 spec, why did they create "nullptr" instead of just using the reserved but functionless "null" keyword that already exists?

Was it because people were creating their own functionality for it via macros or what have you?
[/quote]

"null" is a keyword in C++? I didn't know that... Or are you thinking of a different language?
1

Share this post


Link to post
Share on other sites
[quote name='Álvaro' timestamp='1355525475' post='5010768']
[quote name='Flimflam' timestamp='1355524803' post='5010765']
You know, I've always wondered, when they created the C++11 spec, why did they create "nullptr" instead of just using the reserved but functionless "null" keyword that already exists?

Was it because people were creating their own functionality for it via macros or what have you?
[/quote]

"null" is a keyword in C++? I didn't know that... Or are you thinking of a different language?
[/quote]
No, not officially. My wording was a bit misleading. In fact thinking back, I'm just confusing the functionality of an old compiler that had reserved it. I'm unaware if any others have since.
0

Share this post


Link to post
Share on other sites
[quote name='SiCrane' timestamp='1355525563' post='5010770']
((void *)0) is not a legal definition for NULL in C++. It is a valid definition for NULL in C. So MSVC's NULL definition looks like:[/quote]
Yes, you're right. Sorry, I misread that #ifdef...
0

Share this post


Link to post
Share on other sites
I actually was unpleasantly surprised recently when I was writing templates with no header inclusions and I discovered that NULL is not a keyword. (I always thought it was part of the language for some reason.)

In such files I took to saying:
[source lang="cpp"]#ifndef NULL
#define NULL 0
#endif[/source]

But I'm not using C++11 at the moment, so I don't have nullptr. There's some interesting things to consider in this thread, though. Thanks for starting this, SotL.
0

Share this post


Link to post
Share on other sites
Always NULL.
There are plenty of reasons, I listed some reasons in my early blog.
http://cpgf.org/blog/why-i-prefer-null-over-0-as-null-pointer.html

My primary three points,
1, Reason 1, NULL is self explained.
2, Reason 2, C++ standard saying NULL is 0 doesn't mean null pointer is 0
3, Reason 3, C++11 supports nullptr, everyone will prefer nullptr to 0, then why don't we use NULL than 0 in pre-C++11 era?

And I have tough time to conver C++ code with 0 as pointer to Lua code, but maybe only I do it :)

However, nothing is correct or wrong, it is very subjective opinion, IMHO.
0

Share this post


Link to post
Share on other sites
I would say it depends, but for things that remain mostly static, I use 0, [b][i][u]BUT[/u][/i][/b] keep everything to do with it well commented.
0

Share this post


Link to post
Share on other sites
[quote name='Servant of the Lord' timestamp='1355513391' post='5010709']
But if you are absolutely intent on avoiding macros, use a constant:
[code]const size_t MyNull = 0;[/code]
[/quote]

Ew, please no. I'd rather have someone just use 0 instead of a global variable like that, because at least 0 conveys more information than MyNull does (for example, can I trust that if I set a pointer to MyNull, that if (ptr) will be false?)
0

Share this post


Link to post
Share on other sites
0 obviously gives the correct result, because NULL is defined as 0. The reason people use NULL over 0 is because it's self documenting -- a in-source comment saying this is a "zero pointer", whereas the literal "0" means many things depending on context. Generally, I like self-documenting code like this...

However, IMHO, there's usually enough context around to know whether [font=courier new,courier,monospace]myVar = 0[/font] means "[i]set [font=courier new,courier,monospace]myVar[/font] to a null pointer value[/i]" or "[i]set [font=courier new,courier,monospace]myVar[/font] to the integer value of zero[/i]", so I don't really see the need for the macro.

The same problem comes up in other situations BTW. As well as the type of literal "0" being ambiguous, there's many similar situations that a C++ programmer should be aware of. e.g. what's the type of the literal "1" below, and does the assertion fail?
[code]unsigned long long mask = 1<<48;
assert( mask == 0x1000000000000 );[/code]
[quote name='Álvaro' timestamp='1355518336' post='5010730']0 because I never know what header has the definition for NULL.[/quote]I'm in the same boat.
I like the fact that [font=courier new,courier,monospace]nullptr[/font] is now a part of the language, so it works everywhere... but [font=courier new,courier,monospace]NULL[/font] has the problem that it's not part of the language; if you don't include some specific header ([i]possibly one of many headers that conditionally define it[/i]), then this "[i]keyword[/i]" doesn't exist.
I don't like having to include unnecessary headers, so I instead evaluate the macro in my head and write [font=courier new,courier,monospace]0[/font].
2

Share this post


Link to post
Share on other sites
[quote name='Hodgman' timestamp='1355549999' post='5010854']
The same problem comes up in other situations BTW. As well as the type of literal "0" being ambiguous, there's many similar situations that a C++ programmer should be aware of. e.g. what's the type of the literal "1" below, and does the assertion fail?
[code]unsigned long long mask = 1<<48;
assert( mask == 0x1000000000000 );[/code]
[/quote]
The assert() will be triggered since mask will be 0.

I prefer self-documenting code and always use NULL. It is theoretical that NULL could be defined as something other than 0 but:
#1: It wouldn’t change the integrity of my code as long as I am always using NULL to check for invalid pointers as apposed to mixing between if ( ptr == NULL ) and if ( !ptr ).
#2: 0 is the only literal constant that is defined by the standard to be implicitly castable to a pointer of any kind, thus the idea that NULL could be something other than 0 is only theoretical, and anyone who ends up in such a situation brought it upon him- or her- self.


L. Spiro Edited by L. Spiro
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0