C ++: memory leaks

Question: at what value of the variable n the following code will cause a memory leak?

What code:


int* Bar(int n) { if (n == 1) throw "exception"; return new int[n]; } void Foo(int n) { int *a = Bar(n); if (n <= 2) return; delete[] a; } 

From 5.3.4 / 7

When the value of an expression in direct-new-declarator is zero, the distribution function is called to allocate an array without elements.

From 3.7.3.1/2

The effect of dereferencing a pointer is returned as a request of size zero undefined.

Besides

Even if the space size requested by [new] is zero, the request may fail.

This means that you can do this, but you can not legally (clearly defined on all platforms) play out the memory that you receive - you can pass it to the delete array - and you must delete it.

Here is an interesting note (ie it is not a normative part of the standard, but included for explanatory puppets) is attached to the proposal from 3.7.3.1/2

[32. The goal is to have the new () operator implemented by calling malloc () or calloc (), so the rules are essentially the same. C ++ is different from C, requiring a null query, return a non-null pointer.]

  • And if n is 1, we get:

int * a = Bar (1) and Bar (1) throws an exception. Will this be an exception in the constructor of the variable a? And will it cause a memory leak?

+4
source share
6 answers

He can call them if a == 0 or == 2.

If a == 1, an exception is thrown and memory allocated. If a> 2 memories are allocated and freed.

If memory == 0 should be allocated, as new is not allowed to return null pointers. You must free the allocated memory with delete [].

If memory == 2 is allocated and the function returns. This is an obvious leak.

+7
source
  If n <0 you'll more likely get exception std :: bad_alloc (because of n will be converted to size_t which is unsigned) - no memory leak.
 If n == 1 you'll get exception (invoked by `throw" exception "`) - no memory leak.
 If n == 0 ||  n == 2 you'll never call delete - memory leak.
 If n> 2 you'll call delete - no memory leak.
+2
source

No. If I understand your question correctly, the bar function will throw an exception, and the Foo function will never catch it, which means that it will also exit this function. But no, this should not cause memory leaks, because you are throwing before allocation.

0
source

Your assessment is basically correct - n = 2 will cause a memory leak, n = 0 will theoretically lead to a memory leak - n = 1 will throw an exception (therefore, a new int is never executed), so there is no memory leak.

Any value for n> 2 will NOT cause a memory leak.

Now, if n <0 - you have undefined behavior - and you can get a memory leak (this negative int can be converted to a large positive unsigned value), and bad things can happen.

0
source

Since the spectacle was not chosen for exclusion, it will continue on and return. But since we declared as an integer pointer, if you send 'n' as Zero, a default pointer will be created. You can also check the size of the pointer. But this will not lead to a memory leak. The reason that when you make a return, since it is a single integer pointer and is a local variable, the occupied memory will be freed by default. Thus, a memory leak will not occur in the case you specified.

0
source

Yes, with 0 and 2 you get memory leaks.


It is also a very bad practice for managing dynamic memory. In C ++, this is a very natural / best way to create a class that will manage memory (for example, in the constructor, it allocates some memory, and in the destructor, it releases it). That would be safer and safer.

0
source

All Articles