[Solved] How to use ‘new’ insted of ‘malloc’ in code [closed]


The equivalent of

SP->xs = malloc(size * sizeof(double));

is

SP->xs = new double[size];

This does not require any #includes.

To free the allocated array, use delete[]:

delete[] SP->xs;

The square brackets are important: without them, the code will compile but will have undefined behaviour.

Since you’re writing in C++, consider using std::vector<double> instead of managing memory allocation by hand.

2

solved How to use ‘new’ insted of ‘malloc’ in code [closed]