2

Im fairly new to C++.

So I learned that new allocates memory and returns a pointer of my datatype. But can I use this in a function and return the pointer? If so then where should I place the delete operator?

Is the following code legal?

int *makeArray(int size)
{
    int *result = new int[size];

    delete[] result;
    return result;
}

int main()
{
    int *pointer = makeArray(10);
    /* code ... */
    return 0;
}

It is compiling and working but logically it makes no sense because I deleted my array.

So I tried the following:

int *makeArray(int size)
{
    int *result = new int[size];

    return result;
}

int main()
{
    int *pointer = makeArray(10);
    /* code ... do some stuff with pointer */
    delete[] pointer;
    return 0;
}

Is this safe or does it cause a memory leak? Is there a better way of returning the pointer?

I tried both versions and they are compiling and working but I'm sure at least one of them if not both are unsafe.

  • 1
    If you don't have a very specific reason, you should not use `new`. The default for the kind of code you are showing here should be to use `std::vector`. – user17732522 Mar 25 '22 at 17:24
  • You delete the *memory you allocated* (not the pointer) when you don't need it any more. You tell me, when that is. – user253751 Mar 25 '22 at 17:27
  • Thanks for the answers that helped me a lot. As I mentioned I am new to C++ and my lessons didn't covered vector nor smart pointer yet so I don't know about them. – ComputerScientist101 Mar 25 '22 at 17:32
  • 1
    @ComputerScientist101 This order of teaching C++ is really unfortunate, since `std::vector` is much simpler to use than `new`/`delete` and `new`/`delete` is something one would try to avoid as much as possible in practice. It is not something one should make a habit of using. – user17732522 Mar 25 '22 at 17:54
  • See also: [Is the pointer guaranteed to preserve its value after `delete` in C++?](https://stackoverflow.com/a/5002201/1563833) (spoiler alert: NO) – Wyck Mar 25 '22 at 18:01

1 Answers1

4

Is the following code legal?

int *makeArray(int size)
{
    int *result = new int[size];

    delete[] result;
    return result;
}

int main()
{
    int *pointer = makeArray(10);
    /* code ... */
    return 0;
}

Definitely not! This is Undefined behavior because you return a deleted pointer. After using the delete operator you're telling your OS that it can release the memory and use it for whatever it wants. Reading or writing to it is very dangerous (Program crashing, Bluescreen, Destruction of the milky way)

int *makeArray(int size)
{
    int *result = new int[size];

    return result;
}

int main()
{
    int *pointer = makeArray(10);
    /* code ... do some stuff with pointer */
    delete[] pointer;
    return 0;
}

Is this safe or does it cause a memory leak?

Yes this is the correct way of using the new and delete operator. You use new so your data stays in memory even if it gets out of scope. Anyway it's not the safest code because for every new there has to be a delete or delete[] you can not mix them.

Is there a better way of returning the pointer?

YES. It's called Smart Pointer. In C++ programmers shouldn't use new but smart pointer at all. There is a C++ community Coding Guideline to avoid these calls Guideline R11

AidenDean
  • 318
  • 2
  • 14
  • For `new` there has to be a `delete` and for `new[]` there has to be a `delete[]`. Mixing them causes UB as well. In this case `std::vector` is probably the more straight-forward choice over smart pointers. – user17732522 Mar 25 '22 at 17:25
  • On a modern OS, a BSOD due to such UB would indicate an OS bug (unless you're writing a driver/kernel module/...). – HolyBlackCat Mar 25 '22 at 17:33