Set an entire array to NULL

Okay so I've declared an array like this.

 
Foo *Blocks[100][100][10000] = {0};


and as far as my understanding goes This creates an array with every member set to NULL.

and then later on in my code some of these get given values using:

 
Blocks[a][b][c] = new Foo;


and then when I want to unload I would think that I could just go
 
Blocks = {0};


But obviously this doesn't work.

So I was wondering if there was a way of doing This, with out creating a loop and changing every one to NULL manually.
Hey when you want to unload value at Blocks[a][b][c] you should delete it like this delete(Blocks[a][b][c]) otherwise it would cause memory leak.
In C++ every new must have a corresponding delete.

This is because C++ is not garbage collected, as are other languages (for example Java) where you don't need to bother about deallocating memory.

So if you just set everything to NULL, without delete'ing what needs to be deallocated, your program will have memory leaks.

So I was wondering if there was a way of doing This, with out creating a loop and changing every one to NULL manually.

Well if you know what a, b and c are, you could do this:

1
2
delete Blocks[a][b][c];
Blocks[a][b][c] = 0; // NULL and nullptr would be better choices than 0 

> NULL and nullptr would be better choices than 0

Should I use NULL or 0?

In C++, the definition of NULL is 0, so there is only an aesthetic difference. I prefer to avoid macros, so I use 0. Another problem with NULL is that people sometimes mistakenly believe that it is different from 0 and/or not an integer. In pre-standard code, NULL was/is sometimes defined to something unsuitable and therefore had/has to be avoided. That's less common these days.

If you have to name the null pointer, call it nullptr; that's what it's called in C++11. Then, "nullptr" will be a keyword.

- Stroustrup's C++ Style and Technique FAQ http://www.stroustrup.com/bs_faq2.html#null
Okay, thank you for your quick replies.
I reckon I'll just create a loop that goes through every member in my array and change them back to NULL.


Hey when you want to unload value at Blocks[a][b][c] you should delete it like this delete(Blocks[a][b][c]) otherwise it would cause memory leak.


one last small question:
is delete(Blocks[a][b][c]) equivalent to Blocks[a][b][c] = 0

I'm currently using the latter.
one last small question:
is delete(Blocks[a][b][c]) equivalent to Blocks[a][b][c] = 0

I'm currently using the latter.


As @Catfish666 said above; every new must have a corresponding delete. So if Blocks[a][b][c] is != NULL then you must delete and then assign NULL or 0 to it. Just assigning 0 to a valid pointer will not reclaim the memory allocated but merely lose the access to that memory!
delete is an operation that frees the memory allocated by new.
Assigning zero to a pointer doesn't free the memory.

Think of pointers like this: they are variables that store a number, which is a memory address.

When you use new, the new allocates memory and returns its address, address which you store in a pointer.

By setting the pointer to 0, you don't affect the memory that was allocated, instead you simply "forget" about it. And forgetting about allocated memory is bad in C++, because you are expected it to deallocate it yourself.
Last edited on
FWIW, manual deletion is error prone. If you want to use more modern C++ techniques, use a smart pointer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <memory>  // <- for unique_ptr

typdef std::unique_ptr<Block>  BlockPtr;


// Foo *Blocks[100][100][10000] = {0};  <-  get rid of this
FooPtr Blocks[100][100][10000];  // <- replace with this


// allocate elements with this:
Blocks[a][b][c] = BlockPtr( new Block(...) );

// NO NEED to clean up.. .unique_ptr does it automatically
// no need to set to null after deleting... unique_ptr does it automatically
// no need to initialize to null prior to allocating... unique_ptr does it automatically 



Though 100 * 100 * 10000 is an extremely large array and it's probably a very bad idea to have an array so large. Do you really need that many blocks allocated at once? I mean that is at least 95 MB of RAM in pointers alone.
Thank you, The smart pointers sound great!
Topic archived. No new replies allowed.