Use stack-allocated ctx_t array instead of heap allocation

- Initialize ctxs[i] directly with aggregate initialization
- Pass address of stack object to CreateThread
- Use dot notation instead of arrow for member access
- Remove delete calls since no heap allocation

Co-authored-by: Maurice Heumann <momo5502@gmail.com>
This commit is contained in:
Elias Bachaalany
2025-12-02 15:53:37 -08:00
parent 4a905d562a
commit 651c020b87

View File

@@ -111,8 +111,8 @@ namespace
for (int i = 0; i < thread_count; i++)
{
ctx_t* ctx = ctxs[i] = new ctx_t{5 * (i + 1), i - 2};
threads[i] = CreateThread(nullptr, 0, thread_proc, ctx, 0, nullptr);
ctxs[i] = {5 * (i + 1), 0};
threads[i] = CreateThread(nullptr, 0, thread_proc, &ctxs[i], 0, nullptr);
if (!threads[i])
{
return false;
@@ -124,12 +124,11 @@ namespace
const int expected_results[thread_count] = {5, 10, 15, 20, 25};
for (int i = 0; i < thread_count; i++)
{
if (ctxs[i]->result != expected_results[i])
if (ctxs[i].result != expected_results[i])
{
return false;
}
CloseHandle(threads[i]);
delete ctxs[i];
}
return true;