01/10/2018, 13:25

Tại sao dòng 'delete pa;' lại gây lỗi?

Ai giải thích giúp mình tại sao dòng [4] trong đoạn này lại gây lỗi với:

int A[5] = {10, 20, 30, 40, 50};
int *pa = A;
pa += 15;
delete pa;
Trần Hoàn viết 15:29 ngày 01/10/2018

delete chỉ dùng với new

https://msdn.microsoft.com/en-us/library/h6227113.aspx

Using delete on a pointer to an object not allocated with new gives unpredictable results. You can, however, use delete on a pointer with the value 0. This provision means that, when new returns 0 on failure, deleting the result of a failed new operation is harmless. See The new and delete Operators for more information.

TiCan viết 15:38 ngày 01/10/2018

cám ơn @Trần Hoàn nhé

Bài liên quan
0