Lambda can't access a 2d array

I have a "normal" 2d array and a lambda to fill it up. Looks something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
void fun1(){
    double arr[10][2]; // this works

    std::function<bool(int)> lam = [&,this](int x,int y){
        arr[x][y] = 0;
    };
    lam(1,2);
}

void fun2(){
    double xdim = 10;
    double arr[xdim]; // 1d also works

    std::function<bool(int)> lam = [&,this](int x){
        arr[x] = 0;
    };
    lam(1);
}

void fun3(){
   int xdim = 10;
    int ydim = 2;
    double ** arr = new double*[xdim];
    for(int i = 0; i < xdim; i++)
        arr[i] = new double[ydim](); // this works fine

    std::function<bool(int,int)> lam = [&,this](int x,int y){
        arr[x][y] = 0;
    };
    lam(1,2);
    for(int i = 0; i < xdim; i++)
        delete[] arr[i];
    delete[] arr;
}
}
void fun3(){
    int xdim = 10;
    int ydim = 2;
    double arr[xdim][ydim]; // this doesn't compile

    std::function<bool(int,int)> lam = [&,this](int x,int y){
        arr[x][y] = 0;
    };
    lam(1,2);
}


So yeah, I get a compiler error saying

warning: '<anonymous>' may be used uninitialized in this function [-Wmaybe-uninitialized]
arr[xdim][ydim] = 0;
^
internal compiler error
Error1

I mean both xdim and ydim are set, array size is set, why is it being so difficult?
Last edited on
None of those functions will compile, since either the type of lambda differs sufficiently from the type specified to the std::function template (func1, func3) or the definition of the arrays are illegal (func2, func3.)

Array sizes must be specified with a compile time constant. You may want to disable the non-standard compiler extension that is allowing it.

When you have a question, provide code that actually reproduces the problem. These functions capture this but in your snippet they are not member functions and there is no this for them to capture.

[Edit: And after refreshing, I see you've made some edits so this post isn't quite accurate in the specifics any more, but -- close enough.]
Last edited on
Topic archived. No new replies allowed.