|
-
Nov 1st, 2001, 07:33 PM
#1
Thread Starter
Addicted Member
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.
-
Nov 1st, 2001, 09:34 PM
#2
PowerPoster
Here is an example:
Creates a 2 dimensional array with 4 rows and 4 columns.
-
Nov 2nd, 2001, 02:12 AM
#3
transcendental analytic
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.
-
Nov 2nd, 2001, 06:56 AM
#4
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.
-
Nov 2nd, 2001, 09:02 AM
#5
transcendental analytic
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.
-
Nov 2nd, 2001, 12:49 PM
#6
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.
-
Nov 2nd, 2001, 01:24 PM
#7
transcendental analytic
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.
-
Nov 2nd, 2001, 09:03 PM
#8
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.
-
Nov 3rd, 2001, 06:46 AM
#9
transcendental analytic
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.
-
Nov 3rd, 2001, 07:46 AM
#10
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.
-
Nov 3rd, 2001, 08:06 AM
#11
transcendental analytic
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.
-
Nov 3rd, 2001, 01:52 PM
#12
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.
-
Nov 3rd, 2001, 08:06 PM
#13
Monday Morning Lunatic
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
-
Nov 3rd, 2001, 08:20 PM
#14
-
Nov 3rd, 2001, 09:04 PM
#15
transcendental analytic
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.
-
Nov 3rd, 2001, 09:22 PM
#16
I just demonstrated that it is possible, kedaman =P. Check the code =).
Z.
-
Nov 4th, 2001, 05:37 AM
#17
Monday Morning Lunatic
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
-
Nov 4th, 2001, 06:04 AM
#18
transcendental analytic
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.
-
Nov 4th, 2001, 06:08 AM
#19
Monday Morning Lunatic
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
-
Nov 4th, 2001, 04:26 PM
#20
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.
-
Nov 4th, 2001, 07:20 PM
#21
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.
-
Nov 5th, 2001, 09:50 AM
#22
transcendental analytic
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.
-
Nov 5th, 2001, 10:37 AM
#23
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.
-
Nov 5th, 2001, 11:15 AM
#24
transcendental analytic
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.
-
Nov 5th, 2001, 12:52 PM
#25
Monday Morning Lunatic
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
-
Nov 5th, 2001, 03:26 PM
#26
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.
-
Nov 5th, 2001, 03:34 PM
#27
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|