A question regarding two dimenional array
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: A question regarding two dimenional array

  1. #1
    Join Date
    Jul 2005
    Posts
    907

    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?

  2. #2
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,256

    Re: A question regarding two dimenional array

    Probably something to do with a variable going out of scope.

  3. #3
    Join Date
    Jul 2005
    Posts
    907

    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.
    Quote Originally Posted by Arjay View Post
    Probably something to do with a variable going out of scope.

  4. #4
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,256

    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?

  5. #5
    Join Date
    Jul 2005
    Posts
    907

    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?
    Quote Originally Posted by Arjay View Post
    Does data point to b or a?

    What happens if you make both arrays static? Does the problem go away?

  6. #6
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,256

    Re: A question regarding two dimenional array

    Quote Originally Posted by LarryChen View Post
    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.

  7. #7
    Join Date
    Jul 2005
    Posts
    907

    Re: A question regarding two dimenional array

    Quote Originally Posted by Arjay View Post
    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?

  8. #8
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,256

    Re: A question regarding two dimenional array

    Quote Originally Posted by LarryChen View Post
    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.

  9. #9
    Join Date
    Jul 2005
    Posts
    907

    Re: A question regarding two dimenional array

    Thanks for your explainations.
    Quote Originally Posted by Arjay View Post
    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.

  10. #10
    Join Date
    Oct 2009
    Posts
    577

    Smile 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.

  11. #11
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb 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.

  12. #12
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: A question regarding two dimenional array

    Quote Originally Posted by kempofighter View Post
    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center