- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
char *SomeGlobalPointer {};
void foo()
{
SomeGlobalPointer = new char[1024];
}
int main()
{
foo();
if (!SomeGlobalPointer)
{
delete[] SomeGlobalPointer;
}
return 0;
}
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
0
char *SomeGlobalPointer {};
void foo()
{
SomeGlobalPointer = new char[1024];
}
int main()
{
foo();
if (!SomeGlobalPointer)
{
delete[] SomeGlobalPointer;
}
return 0;
}
Отсюдова:
https://pvs-studio.ru/ru/blog/posts/cpp/1068/
Вызывает подозрение строка двенадцать
delete[] на 0 это ж краш?
If expression evaluates to a null pointer value, no destructors are called, and the deallocation function may or may not be called (it's unspecified), but the default deallocation functions are guaranteed to do nothing when passed a null pointer.
https://en.cppreference.com/w/cpp/language/delete
Т. е. зависит от типа. Встроенные деаллокаторы обещают с нулём ничего не делать.
Алсо, тут vlaue инициализатор "{}" который явно закажет туда nullptr
https://govnokod.ru/24560
Насчет крестоговна - не уверен.