Results 1 to 27 of 27

Thread: dynamic 2 dimensionnal array

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Location
    Québec, Canada
    Posts
    131

    dynamic 2 dimensionnal array

    Hello, I was wandering if it was possible to create 2 dimensional array dynamicly. If it is possible, how can I do it.
    Khavoerm Irithyl

  2. #2
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Here is an example:

    VB Code:
    1. char me[4][4];

    Creates a 2 dimensional array with 4 rows and 4 columns.
    Baaaaaaaaah

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    AFAIK it's impossible, with known dimensions you can though use a 1d array like this:

    type* array=new type[firstdimension*second dimension];

    array[firstelement*seconddimension+secondelement]
    or
    array[firstelement+firstdimension+secondelement]
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4
    Zaei
    Guest
    Of course it's not impossible, kedaman =). Simple keep a pointer to std::vector, where the size of the vector is the number of rows. Then, just resize each vector to be the number of columns you want, and write a few simple overloaded [] operators. Should be a fairly easy one to make. Then, to resize rows, use realloc() (it will do any copying necessary for you), and for columns, just resize the vectors.

    Z.

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Come on Zaei! That's not the standard C++ array

    However I don't understand your reasoning, it sounds more like you are making an array of arrays.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6
    Zaei
    Guest
    That's all a 2D array is. An Array of an Array. So, use a vector of vectors to make a dynamic 2d array =).

    Z.

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    heyhey, thats not true, a 2d array has to be stored concecutively in memory just like regular arrays.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  8. #8
    Zaei
    Guest
    If it works like int x[10][10], and it's dynamic, it's a 2d array in my book. Thats what container classes are for, abstracting the interface away from the implementation, so that the user doesnt have to know whats going on under the covers =).

    Z.

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    It doesn't work like a 2d array, that's the point with distinction between datastructures. I thought you were for lowlevel stuff Zaei, now you show the same ignorance of a Java programmer
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10
    Zaei
    Guest
    I love low level as much as the next guy, but when you need a dynamic array, and dont know the numbers at run time, what are ya gonna do =)?

    Z.

  11. #11
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    You do what I did
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  12. #12
    Zaei
    Guest
    You said it was impossible =P.

    And, even without vectors, here is a solution:
    Code:
    void resize(int* mat, int nx, int ny, int ox, int oy)
    {
    	int* nmat = new int[nx*ny];
    	int trans;
    	for(int i = 0; i < nx; ++i) {
    		for(int j = 0; j < ny; ++j) {
    			if((i >= ox) || (j >= oy))
    				trans = 0;
    			else
    				trans = mat[j*oy+i];
    			nmat[j*ny+i] = trans;
    		}
        }
        realloc(mat, nx * ny * sizeof(int));
        memcpy(mat, nmat, nx * ny * sizeof(int));
        //delete [] nmat;
    }
    That should resize the array correctly, and still keep the consecutive memory requirement (kedaman =).

    Z.

    [edit]
    Found some errors in the code, fixed.

  13. #13
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You can't mix realloc and new...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  14. #14
    Zaei
    Guest
    why?

    Z.

  15. #15
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    AFAIK It is still impossible. Simply because dimensions are static, provided one of the dimensions you can use a 1d array to act like a 2d array
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  16. #16
    Zaei
    Guest
    I just demonstrated that it is possible, kedaman =P. Check the code =).

    Z.

  17. #17
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Ragged arrays are very possible. Just have an array of pointers, and allocate that. Then allocate the individual arrays off those elements.

    You can't mix realloc and new because they use different allocation methods (new[] stores extra data about the size of the array, and new might do something else), and might not even use the same allocation heap!

    The lack of a "renew" in C++ is something that Stroustrup mentioned and may make it into the new C++ standard
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  18. #18
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    No Zaei you just demonstrated my workaround It is still a 1d array.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  19. #19
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Are we talking technically or conceptually?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  20. #20
    Zaei
    Guest
    In an environment where everything is stored sequentially, technically, of course you cant have a 2d array, period =). Conceptually, you use the workaround, or you stick with normal C++ procedure, and create a container class (or create a regular 2d array with dimensions of the largest needed size).

    Z.

  21. #21
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    it is possible to dynamically allocate 2 dimensional arrayx if one dimension is known. We talked about that kedaman:
    Code:
    int (*ar)[5] = new int[dynarg][5];
    ar[3][4] = 234;
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  22. #22
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Yep btw did you make a fully resizable 2d array class?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  23. #23
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    No. I have many other things to do and don<#t need it. If I ever make one I'll post it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  24. #24
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Ok, I don't have a need of it either, if i happen to make one i'll post it here, unless someone else has posted it already
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  25. #25
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Why not just a simple matrix class?

    I'm sure the resizable bit's been done before.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  26. #26
    Zaei
    Guest
    The code I posted will do the resize part... It should be a matter of 5 minutes, and a few brain cells to throw it into a class =).

    Z.

  27. #27
    jim mcnamara
    Guest
    You know, I don't quite get why no one has mentioned sparse arrays.

    It's THE standard approach in writing something like a spreadsheet or montrous file index that can have 100 x 10K cells -so it may not fit in memory whole. Some horrible thing like:

    ie., char[100][10000][132];

    But a sparse array is only partly populated, so only the 'active' cells are in memory. This is a very strightforward way to deal with n-dimensional arrays as virtual entities. And you don't have to worry about memory leaks because you can find every entity and use delete or free() depending on how it was allocated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width