2

I don't know a lot about C or C++ but I think with C when you create an array you use malloc to ask for the memory and then you check that the memory was allocated before assigning values. In C++ you implement an array using new instead. Would check the allocation of memory for the array in C++ the same as in C by checking that the array is not null?

For example,

int main()
{
      int* myArr = new int[10];
      if(myArr!=NULL)
      {
           //DO SOMETHING
      }
}

I get that most computers have a lot of memory, making running out of memory less likely today, but I also understand that failing to do things like this can lead to unexpected bugs later on down the road.

UPDATE: I was trying to keep my example simple. As mentioned in the comments I was referring to dynamic memory allocation. I am trying to implement a heap data structure. As part of the heap there exists an array to store the values of the heap. When the heap is full the array has to be expanded by design by the next power of 2. Thus i may initialize the heap as 10 but when I reach 10 I will need to expand to 16. Given enough items the heap will reach size 2^n. Therefore I am calling a function that has a parameter for the HEAP pointer. Then I create a new array and copy the values from the existing array to the new array. While doing this I was thinking about what happens if the new array cannot be created because of being out of memory. I may want to write the values to a file before exiting.

AxGryndr
  • 2,274
  • 2
  • 22
  • 45
  • If `new` fails it throws an exception (unless you tell it otherwise). – Galik Mar 15 '18 at 23:22
  • Unless you use `std::nothrow`, operator `new` will, well, throw an exception if it doesn't have enough memory. The docs meniton it: http://en.cppreference.com/w/cpp/language/new – HolyBlackCat Mar 15 '18 at 23:22
  • Related: https://stackoverflow.com/questions/16158981/how-can-i-check-if-there-is-enough-heap-memory-available – scohe001 Mar 15 '18 at 23:22
  • @Galik Why are you answering in a comment? – juanchopanza Mar 15 '18 at 23:25
  • 1
    Actually, in modern C++ you normally shouldn't mess with raw `new` and `delete`, and in case of arrays you should normally use `std::vector` instead. (Or `std::array` if you're *absolutely* sure you know the size before-hand and there's no possibility you will ever need to adjust the size.) – Daniel Schepler Mar 15 '18 at 23:25
  • Yes you must check whether operator `new` succeeded in allocating memory or not. `new` doesn't fail on allocating small number of elements but what about a huge program running for a long time and with a small memory space? – Raindrop7 Mar 15 '18 at 23:26
  • 2
    If an ordinary `new` expression fails it throws a `std::bad_alloc` exception. You can check for that by using a `try-catch`, but since memory exhaustion usually is a fatal condition it can be best to just propagate that exception all the way up to `main`, and terminate. Anyway, instead of a raw array allocated via a `new`-expression, for simplicity and safety just use a `std::vector`. – Cheers and hth. - Alf Mar 15 '18 at 23:28
  • Don't use `new` and `delete`. Say, `std::vector myArr(10);` If all you can do if there's no memory available is to exit the program, you need do nothing else. – Jive Dadson Mar 15 '18 at 23:36
  • Based on your current edit, the suggestion of `std::vector` is even more applicable. It's smart, manages your memory for you, and resizes as required or on demand. The only reasons not to use `std::vector` are some neanderthal is demanding you not use it and you are writing your own vector implementation for educational purposes. – user4581301 Mar 15 '18 at 23:54
  • So with a vector can I have an array of defined structs? – AxGryndr Mar 15 '18 at 23:56
  • `std::vector myvectorofmystruct(10);` just made 10 elements of `mystruct` for you. Mind you since you have added that the expansion requirements are quite strict, allowing vector to self-resize is not exactly what you want, but [that's what the `resize` method is for](http://en.cppreference.com/w/cpp/container/vector/resize). – user4581301 Mar 16 '18 at 00:00
  • Alright, thanks that helps. – AxGryndr Mar 16 '18 at 00:01
  • If you need the array represented by the `vector` as an array to interact with something that demands a `mystruct *` [`std::vector::data` has that covered](http://en.cppreference.com/w/cpp/container/vector/data). – user4581301 Mar 16 '18 at 00:01

1 Answers1

-2

No, to make an array you do this:

int main()
{
   int myArr[10];
   // DO SOMETHING
}

If you must use dynamic allocation, use new without the std::nothrow specifier, so that it throws a std::bad_alloc exception if the allocation fails. That is ultimately the equivalent of checking for malloc returning a NULL pointer.

If you do use the std::nothrow specifier then, yes, check for a NULL pointer. In modern C++, that's nullptr. In antique C++, that's NULL (or 0) as you've shown.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055