After I declare b static as well, then it is fine. Since I already declare a static and b points to a, so it looks like I don't have to declare b static. Why'd I have to declare both static?
Originally Posted by Arjay
Does data point to b or a?
What happens if you make both arrays static? Does the problem go away?
What is the difference between these two static variables?
Essentially the two arrays are pointers to pointers. In order to dereference the second pointer, both pointers need to point to something.
If the first pointer goes out of scope, you'll get an error trying to access the second pointer. If the first pointer remains in scope, but the second pointer is out of scope, you'll get an error dereferencing the second pointer.
Therefore, both array variables need to remain in scope so **data doesn't point to garbage.
Essentially the two arrays are pointers to pointers. In order to dereference the second pointer, both pointers need to point to something.
If the first pointer goes out of scope, you'll get an error trying to access the second pointer. If the first pointer remains in scope, but the second pointer is out of scope, you'll get an error dereferencing the second pointer.
Therefore, both array variables need to remain in scope so **data doesn't point to garbage.
Though the pointers pointing to the a array are valid beyond the local scope, the b array is not valid then, i. e. the 12 bytes starting at 0xaabbccdd might get destroyed or newly used after the constructor has run. Hence the 0xaabbccdd assigned to data points to memory not valid anymore.
In short, &a[0] != &b[0]. Instead, b[0] == &a[0] because b is an array of pointers to integers but a is an array of integers. itsmeandnobodyelse said the same thing I think but I'm just trying to simplify that down to one sentence.
A better question is what does the OP want to do? Then come up with a solution that accomplishes that without the tricky, indirect memory addressing. What he did there looks like the kind of thing that assembly programmers do. For instance, why did you make static array within the constructor? Why not make a static array for the class and then use it more directly?
Last edited by kempofighter; August 24th, 2010 at 01:05 PM.
In short, &a[0] != &b[0]. Instead, b[0] == &a[0] because b is an array of pointers to integers but a is an array of integers. itsmeandnobodyelse said the same thing I think but I'm just trying to simplify that down to one sentence.
A better question is what does the OP want to do? Then come up with a solution that accomplishes that without the tricky, indirect memory addressing. What he did there looks like the kind of thing that assembly programmers do.
I would suggest the OP would like to map the 'data' to the static array a. But unfortunately an
'int a[][3]' couldn't be directly mapped to 'int** data'. The first is one memory block of 9 integers while the second only is a pointer pointing to another pointer. To make the 'data' act like an 2d-array you would need to define it as 'int* data[3]' like you did it with the b. Then the data defines an additional block of memory which contains the pointers pointing to each row of the original 2d-array. Alternatively you could use an 'int* data' pointing to a[0][0] where you have functions to use it as a 2D-Array:
Code:
class A
{
public:
A()
{
static int a[][3] = {1,2,3,4,5,6,7,8,9};
data = &a[0][0];
}
private:
int* data;
public:
int& operator()(int x, int y) { return *(data + 3*x + y); }
};
...
A a;
int z = a(1, 2); // = 6
...
Bookmarks