-
August 22nd, 2010, 05:44 PM
#1
A question regarding two dimenional array
Here is the code,
Code:
class A
{
public:
A()
{
static int a[][3] = {1,2,3,4,5,6,7,8,9};
int* b[3] = {a[0], a[1], a[2]};
data = &b[0];
}
private:
int** data;
};
Unfortunately, data doesn't point to two-dimensional array a and actually points to garbage. I wonder why?
-
August 22nd, 2010, 06:56 PM
#2
Re: A question regarding two dimenional array
Probably something to do with a variable going out of scope.
-
August 22nd, 2010, 07:03 PM
#3
Re: A question regarding two dimenional array
It looks like the variable is out of scope but I declare the two dimenional array static. Why is it still out of scope? Thanks.
Originally Posted by Arjay
Probably something to do with a variable going out of scope.
-
August 22nd, 2010, 07:26 PM
#4
Re: A question regarding two dimenional array
Does data point to b or a?
What happens if you make both arrays static? Does the problem go away?
-
August 22nd, 2010, 07:52 PM
#5
Re: A question regarding two dimenional array
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?
-
August 22nd, 2010, 08:02 PM
#6
Re: A question regarding two dimenional array
Originally Posted by LarryChen
Why'd I have to declare both static?
So they don't go out of scope. When the arrays go out of scope, the pointers that data points to are meaningless. It's that simple.
-
August 22nd, 2010, 08:10 PM
#7
Re: A question regarding two dimenional array
Originally Posted by Arjay
So they don't go out of scope. When the arrays go out of scope, the pointers that data points to are meaningless. It's that simple.
What is the difference between these two static variables?
-
August 22nd, 2010, 08:33 PM
#8
Re: A question regarding two dimenional array
Originally Posted by LarryChen
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.
-
August 22nd, 2010, 09:02 PM
#9
Re: A question regarding two dimenional array
Thanks for your explainations.
Originally Posted by Arjay
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.
-
August 23rd, 2010, 11:40 AM
#10
Re: A question regarding two dimenional array
To add to above explanations:
The a is a local array which takes 9 int. Say its address in memory is 0x00112233.
Code:
0x00112233: 01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 ... 09 00 00 00
where the non-zeros are the values 1 ... 9 .
Then
Code:
&a[0] = 0x00112233
&a[1] = 0x0011223f
&a[2] = 0x0011224b
The b is a local array which takes 3 pointers. Say, its address in memory is 0xaabbccdd and assume 32-bit pointers:
Code:
0xaabbccdd: 33 22 11 00 3f 22 11 00 4b 22 11 00
Then
&b[0] = 0xaabbccdd
&b[1] = 0xaabbcce1
&b[2] = 0xaabbcce5
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.
-
August 24th, 2010, 12:59 PM
#11
Re: A question regarding two dimenional array
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.
-
August 24th, 2010, 01:17 PM
#12
Re: A question regarding two dimenional array
Originally Posted by kempofighter
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
...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|