free store new double[]

H

Hicham Mouline

hi,

Is there a way to allocate an array and initialize in the same statement?

double* d = new double[n];
and assign immediately a value, say NaN

what i'm doing now is just:

for (size_t i=0; i<n; ++i)
d = NaN;

regards,
 
M

Maxim Yegorushkin

Is there a way to allocate an array and initialize in the same statement?

double* d = new double[n];
and assign immediately a value, say NaN

what i'm doing now is just:

for (size_t i=0; i<n; ++i)
  d = NaN;


Another way:

double* d = std::fill_n(new double[n], n, NaN) - n;

Or (save a subtraction):

double* d;
std::fill_n((d = new double[n]), n, NaN);
 
A

Andrey Tarasevich

Hicham said:
hi,

Is there a way to allocate an array and initialize in the same statement?

double* d = new double[n];
and assign immediately a value, say NaN

If you were looking for zeroes, the you could just do

double* d = new double[n]();
what i'm doing now is just:

for (size_t i=0; i<n; ++i)
d = NaN;


Otherwise, to meet your "one statement" requirement, the only thing you
can do is replace you explicit cycle with a standard (or your own)
function with equivalent functionality.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,167
Messages
2,570,910
Members
47,453
Latest member
MadelinePh

Latest Threads

Top