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

醉酒当歌 提交于 2019-12-08 21:43:15

问题


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.


回答1:


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"




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/13122636/c-what-does-the-warning-enabled-by-default-mean-during-compile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!