Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++; What does the warning "enabled by default" mean during compile?

My code compiles without error, but what does this mean?

 etherate.cpp:432:11: warning: deleting array ‘unsigned char broadMAC [6]’ [enabled by default]

Its from this line, where I am deleting an unsigned char array;

delete [] broadMAC;

Is it OK to leave this as it is, if not, how could I improve upon this?

Thanks.

UPDATE

How is broadMAC declared?

From about 10 lines previous;

unsigned char broadMAC[6] = {destMAC[0], destMAC[1], destMAC[2], destMAC[3], destMAC[4], destMAC[5]};

destMAC is also an unsigned char array with values stored inside it. I needed to copy them out, do something with destMAC, then restore the original values; so I have declared broadMAC and wish to delete it after.

like image 671
jwbensley Avatar asked Oct 29 '12 13:10

jwbensley


3 Answers

broadMAC is allocated in automatic memory, like so:

unsigned char broadMAC[6];

and then you call

delete[] broadMAC;

which is wrong, since you didn't allocate it with new[].

Using delete/delete[] on memory not allocated with new/new[] results in undefined behavior, and your compiler is smart enough to tell in this case.

You needn't worry about freeing the memory, it will be freed automatically.

like image 128
Luchian Grigore Avatar answered Sep 27 '22 22:09

Luchian Grigore


G++ allows to enable and disable many warnings (e.g. -Wctor-dtor-privacy, -Woverloaded-virtual, -W...) by specifying them on the command line. Some warnings are enabled, without the need to do so on the command line, others must be explicitly requested.

So, some warnings are:

"enabled by default"

and some are:

"disabled by default"

like image 40
Olaf Dietsche Avatar answered Sep 28 '22 00:09

Olaf Dietsche


You're trying to free an array which you allocated in the stack, not heap. So when the variable becomes out of scope, it will free itself and you shouldn't/can't free it explicitly.

like image 32
JosephH Avatar answered Sep 28 '22 00:09

JosephH