'Assertion timeptr != nullptr
I am working on logging in a DLL but getting the date and time causes an assertion.
The function is supposed to return a time like 3/3/2022 8:00:00.
But it causes an assertion, and a warning "'tm_v' could be '0'".
Code:
char* date_and_time()
{
time_t timer = time(NULL);
char buffer[26];
struct tm* tm_v = malloc(sizeof(struct tm));
tm_v = localtime_s(tm_v, &timer);
strftime(buffer, 26, "%m/%d/%Y %H:%M:%S", tm_v);
return buffer;
}
Assertion Message:
File: minkernel\crts\ucrt\src\appcrt\time\strftime.cpp
Line: 135
Expression: timeptr != nullptr
Solution 1:[1]
I fixed it by changingtime_t timer = time(NULL); to time_t timer; and struct tm* tm_v = malloc(sizeof(struct tm)); to struct tm tm_v;
This is the function now:
char* date_and_time()
{
char buffer[26];
time_t timer;
struct tm tm_v;
time(&timer);
localtime_s(&tm_v, &timer);
strftime(buffer, 26, "%m/%d/%Y %I:%M:%S %p ", &tm_v);
return buffer;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ShxShlix |
