Jump to content
  • Advertisement
Sign in to follow this  
Aluthreney

Is it safe to NULL an array?

This topic is 2560 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on a program and I was wondering if it's safe programming to use the NULL macro as an array value?

eg.: char array[NULL][NULL];

Share this post


Link to post
Share on other sites
Advertisement

I'm working on a program and I was wondering if it's safe programming to use the NULL macro as an array value?

eg.: char array[NULL][NULL];


What exactly would be the point of that ?

Share this post


Link to post
Share on other sites
Null is just: #define NULL 0
So, you've actually written: [font="Courier New"]char array[0][0];[/font]

No, you can't have an array with a constant-sized dimension of zero.

Share this post


Link to post
Share on other sites

I'm working on a program and I was wondering if it's safe programming to use the NULL macro as an array value?
Yes absolutely. E.g.
int array[3] = {1, NULL, 2};What this does is fill the array with 1, 0, and 2.

eg.: char array[NULL][NULL];[/quote]That's something completely different. You're not trying to use NULL as an array value any more like you stated. You're now trying to use NULL as the array dimenstions. The result being an array of zero size which is illegal and would only compile on compilers that allowed such a thing as a language extension.

Share this post


Link to post
Share on other sites
An array of zero length would be like a room with zero width, length and height. Hard to imagine and certainly not useful for anything if it could exist. :blink:

Share this post


Link to post
Share on other sites
I'm trying to make a text-based game to see if I can learn anything new about c++. I'm building a class "Map" that allows me to build an area where the player ("P") will be able to move through. The reason I'm creating a class is because I want to create different areas, so I had decided that in the class the "Map" would be char grid[NULL][NULL] (wouldn't exist) and when I wanted to create a new area for my game, I would just type in the values for my "Map" object.

Share this post


Link to post
Share on other sites

I'm trying to make a text-based game to see if I can learn anything new about c++. I'm building a class "Map" that allows me to build an area where the player ("P") will be able to move through. The reason I'm creating a class is because I want to create different areas, so I had decided that in the class the "Map" would be char grid[NULL][NULL] (wouldn't exist) and when I wanted to create a new area for my game, I would just type in the values for my "Map" object.


I think what you are thinking of is actually char* grid = NULL, which is different from what you are doing here. To elaborate on what Hodgman said earlier, most of the time NULL is defined as 0, but in C it can be all sorts of crazy stuff. In C++ there are some other weird things specifically when dealing with pointers.


if ( ptr == 0 ) // this will check if ptr is a null pointer
if ( ptr == NULL ) // same
if ( !ptr ) //same

int blah = 0;
if ( ptr == blah ) //Can be very different depending on compiler optimizations to your code.


http://stackoverflow.com/questions/1296843/what-is-the-difference-between-null-0-and-0

Similarly, especially useful for debugging, null pointers are not always 0 value. The reason ptr == 0 works is because the const literal 0 compared with pointers always treats the 0 as a null ptr, not necessarily corresponding to the memory address at 0.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!