-1

I've always declared my arrays using this method:

bool array[256];

However, I've recently been told to declare my arrays using:

bool* array = new bool[256];

What is the difference and which is better? Honestly, I don't fully understand the second way, so an explanation on that would be helpful too.

Ryan
  • 103
  • 1
  • 1
  • 6
  • 1
    This previous question covers some of the relevant issues: https://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap – paisanco Jun 09 '18 at 20:21
  • @paisanco Thanks for the link, but that's honestly above me. Is one of these declarations on the heap, and the other on the stack? If so, which is which? If not, how was the link relevant? – Ryan Jun 09 '18 at 20:27
  • What source are you learning C++ from? – melpomene Jun 09 '18 at 20:27
  • Briefly, the first declaration is on the stack, the second on the heap. Which is better is covered by the previous question. – paisanco Jun 09 '18 at 20:28
  • @melpomene I'm trying out the easiest Firecode exercises, and they recommend the second way, which is different from what I was originally taught. – Ryan Jun 09 '18 at 20:32
  • @paisanco Ok, thank you -- I'll take a closer look at that link then. – Ryan Jun 09 '18 at 20:32
  • There needs to *reasoning* behind these recommendations tho; why are they saying this? I find it problematic to recommend using pointers in this manner without context. The pointer is unnecessary; you're using a constant value for the size thereby making `new` pointless beyond adding additional responsibility for yourself. – ChiefTwoPencils Jun 09 '18 at 20:45

3 Answers3

3
bool array[256];

This allocates a bool array with automatic storage duration.

It will be automatically cleaned up when it goes out of scope. In most implementations this would be allocated on the stack if it's not declared static or global.

Allocations/deallocations on the stack are computationally really cheap compared to the alternative. It also might have some advantages for data-locality but that's not something you usually have to worry about. But you might need to be careful of allocating many large arrays to avoid a stack overflow.

bool* array = new bool[256];

This allocates an array with dynamic storage duration.

You need to clean it up yourself with a call to delete[] later on. If you do not then you will leak memory.
Alternatively (as mentioned by @Fibbles) you can use smart-pointers to express the desired ownership/lifetime requirements. This will leave the responsibility of cleaning up to the smart-pointer class. Which helps a lot with guaranteeing deletion, even in cases of exceptions.

It has the advantage of being able to pass it to outer scopes and other objects without copying (RVO will avoid copying for the first case too in certain cases, but storing it as a data-member and other uses can't be optimized in the first case).

PeterT
  • 7,981
  • 1
  • 26
  • 34
  • 1
    Since you touched on the possibility of memory leaks. It might be worth adding a line or two about smart pointers as a safer alternative to `new` and `delete[]`. – Fibbs Jun 09 '18 at 21:18
2

The first is allocation of memory on stack:

// inside main (or function, or non-static member of class) -> stack
int main() {
    bool array[256]; 
}

or maybe as a static memory:

// outside main (and any function, or static member of class) -> static
bool array[256]; 
int main() {
}

The last is allocation of dynamic memory (in heap):

int main() {
    bool* array = new bool[256];
    delete[] array; // you should not forget to release memory allocated in heap
}

The advantage of dynamic memory is that it can be created with variable number of elements (not 256, but from some user input for example). But you should release it each time by yourself.

More about stack, static and heap memory and when you should use each is here: Stack, Static, and Heap in C++

0

The difference is static vs dynamic allocation, as previous answers have indicated. There are reasons for using one over the other. This video by Herb Sutter explains when you should use what. https://www.youtube.com/watch?v=JfmTagWcqoE It is just over 1 1/2 hours.

My preference is to use

bool array[256]; 

unless there's a reason to do otherwise.

Mike

Michael Surette
  • 701
  • 1
  • 4
  • 12