-2

Is this allocation done on the stack and do i need to make delete on this cLastDateToRun code? Its inside a function.

wchar_t cLastDateToRun[9] = { 0 };
wcsncpy_s(cLastDateToRun, SerialNumber, 8);
cLastDateToRun[8] = L'\0';
int LastDateToRun = _wtoi(cLastDateToRun);
delete[] cLastDateToRun;

3 Answers3

1

Since you have your array on stack no need to free() it. Only memory which is allocated by you using malloc() calloc() or realloc() should be freed. Freeing memory explicitly on stack will lead to undefined behavior.

Gopi
  • 19,784
  • 4
  • 24
  • 36
1

For C(can also be used with C++):
You can only free() something you malloc,calloc or realloc

For C++ :
You can use delete or delete[] something you new or new[]

Freeing or deleting something not dynamically allocated will cause issues like undefined behavior. For more knowledge you better refer more about dynamic memory

Dinal24
  • 3,162
  • 2
  • 18
  • 32
0

IMHO the other comments are too weak. It's not you need not it's you must not. Freeing is for dynamically allocated memory. The thing you have is reserved by the compiler and vanishes after leaving the function (but in the case of static)

I've not standard at hand, but I'd assume hat freeing a local variable not dynamically allocated invokes undefined behaviour. So that would mean it's free to to any harm to whatever it steers.

Friedrich
  • 5,916
  • 25
  • 45