Skip to content

Commit

Permalink
Make result consistent if memory allocation fails
Browse files Browse the repository at this point in the history
  • Loading branch information
nbelakovski committed Mar 25, 2024
1 parent aefb7c9 commit 6460871
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions c/prima.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,32 +93,12 @@ int prima_init_result(prima_result_t *const result, const prima_problem_t proble

memset(result, 0, sizeof(prima_result_t));

// x: returned point
result->x = (double*)malloc(problem.n * sizeof(double));
if (!result->x)
return PRIMA_MEMORY_ALLOCATION_FAILS;
for (int i = 0; i < problem.n; i++)
result->x[i] = NAN;

// f: objective function value at the returned point
result->f = NAN;

// cstrv: constraint violation at the returned point (COBYLA and LINCOA only)
result->cstrv = NAN;

// nlconstr: nonlinear constraint values at the returned point, of size m_nlcon (COBYLA only)
if (problem.m_nlcon <= 0)
result->nlconstr = NULL;
else {
result->nlconstr = (double*)malloc(problem.m_nlcon * sizeof(double));
if (!result->nlconstr) {
free(result->x);
return PRIMA_MEMORY_ALLOCATION_FAILS;
}
for (int i = 0; i < problem.m_nlcon; i++)
result->nlconstr[i] = NAN;
}

// nf: number of function evaluations
result->nf = INT_MIN;

Expand All @@ -128,6 +108,22 @@ int prima_init_result(prima_result_t *const result, const prima_problem_t proble
// message: exit message
result->message = NULL;

// x: returned point
result->x = (double*)malloc(problem.n * sizeof(double));
if (!result->x)
return PRIMA_MEMORY_ALLOCATION_FAILS;
for (int i = 0; i < problem.n; i++)
result->x[i] = NAN;

// nlconstr: nonlinear constraint values at the returned point, of size m_nlcon (COBYLA only)
result->nlconstr = (double*)malloc(problem.m_nlcon * sizeof(double));
if (!result->nlconstr) {
free(result->x);
return PRIMA_MEMORY_ALLOCATION_FAILS;
}
for (int i = 0; i < problem.m_nlcon; i++)
result->nlconstr[i] = NAN;

return 0;
}

Expand Down

0 comments on commit 6460871

Please sign in to comment.