static local variable

Can I use local static vairable as shown below to allocate dynamic memory. I tried this but the compiler is giving error that the
[error] expected type specifier before static.


1
2
3
4
5
6
7
8
9
10
11
12
13
static double **aP_u = NULL;
aP_u = new static double *[N];
for (i = 0; i < N; i++)
{
  aP_u[i] = new static double[N];
}
for (i = 0; i < N; i++)
{
   for (j = 0; j < N; j++)
   {
     aP_u[i][j] = 0.0;
   }
}
Don't use "static" in the "new" statements.
Actually this I am using inside a function, to be also used in the main function.
However can I use this dynamic memory allocation in main function to use in void function used inside of main?
1
2
3
4
5
aP_u = new static double *[N];
for (i = 0; i < N; i++)
{
  aP_u[i] = new static double[N];
}

There's no need for 'static' in either of those two new statements.

I trust you have some way to
- ensure aP_u is only initialised once
- ensure aP_u is fully deleted when no longer required.
1
2
3
4
5
6
7
// some function
{
    // an NxN matrix of double, initialised to all zeroes, with static storage duration
    static std::vector< std::vector<double> > aP_u( N, std::vector<double>(N) ) ;
    
   // ..
}
Topic archived. No new replies allowed.