PDA

View Full Version : Two-Dimensional array + pointers = problem

robert_s
01-19-2002, 05:53 AM
Hi Everybody! I have exactly the same problem as "Sandrew" had a few posts before!
This is an example of two-dimesional array which works fine!

int arr[10][1]; //x,y

for (int x = 0; x < 10; x++)
{
arr[x][0] = x;

cout << arr[x][0] << " ";
}

the output is:
0123456789

Here I increment x by one 9 times end drop its value into [x] array and second array stays all the time 0 just to make it easy for now (1 row of size 10).

Now I want to do exactly the same thing with using pointers and new/delete operators!
I'll call first array x and second y ie. [x][y]. Here I have to allocate space for 10 ints in [x] array and size 1 for y array ie. [10][1]
I dont want it to be the other way round ie. [1][10].
First array ie [x] must be dynamically allocated eg. size 10 and second [y] should stay size 1. this should looke like [10][1]
Anyone?

int **arr;

arr = new int*[10]; //create rows length = 10

now I need to create a row 1!

here >>?<< this line not sure where I have to create [y] array of size 1 by using new!!!
then...
for ( int x =0; x < 10; x++ ) //loop 10 times to fill in [x] array
{
arr[x][0] = x; // store each new x value (total 10 values) into arr [x] and y stays at 0
}

[This message has been edited by robert_s (edited 01-19-2002).]

endo
01-19-2002, 08:31 AM
I am a little confused - why would you want arr[10][1]? This is the same as arr[10] which is much simpler to handle.

robert_s
01-19-2002, 08:39 AM
I know!!!
this is just an example!!!
Ok! I will need bigger number than 1!!
eg. [10][3]. so? how do I do it?
I need to use 2D array!

[This message has been edited by robert_s (edited 01-19-2002).]

Inquisitor
01-19-2002, 09:00 AM
// Creation:

int **arr;
arr=new int*[10];
for(int t=0;t<10;++t) {
arr[t]=new int[1];
}

// Destruction:

for(int t=10-1;t>=0;--t) {
delete[] arr[t];
}
delete[] arr

endo
01-19-2002, 10:01 AM
The code posted by Inquisitor uses dynamic memory allocation, but I don't think it'll work if you want a 2d array that will grow and expand as you use it. Is that what you're after?

robert_s
01-19-2002, 10:45 AM
Yes!! Thats what I am after!!

I want only left array cell ie. [x] to be dynamically allocated and [y] array cell to be 2 from the start and never changed. array cell [y] does not need to be dynamically allocated!

I found out that I can use new to allocate both array cells at once but unfortunately there was no example and I have no idea how to do it!!

can i do it like that? eg.

float **arr; //declaration in a class as private member

int size; //another data member

then..............

// now we are in an implementation file in a class constructor

constructor::constructor()
{
size = object.getSize();

arr = new int[size][3]; //here 3 stays always same but the left array cell is unknown to me untill program executes therefore I need to use dynamic allocation here

}

so, at the end I wanna end up with 3 rows! why 3? because I know already the size of the second array cell which is [3] and each of the rows will be equal to same value but unknown to me length ( imagine youre reading bitmap and once you can load 256x256 and the other time you can load 512x512 and some function will return eg. 256 so that means that size will be equal to 256 which means that I need to allocate the left cell to 256!!

[This message has been edited by robert_s (edited 01-19-2002).]

endo
01-19-2002, 01:46 PM
Originally posted by robert_s:

constructor::constructor()
{
size = object.getSize();

arr = new int[size][3]; //here 3 stays always same but the left array cell is unknown to me untill program executes therefore I need to use dynamic allocation here

}

I'm pretty sure that won't work, you have to remember that any array has to have space allocated for it when it is compiled. If size was a const int it would work, but not as a variable like you suggest. You would probably need another data structure of some type for that kind of implementation.

robert_s
01-19-2002, 02:32 PM
Ok Thanks endo!!
But this is just an example!
Lets assume that this function will return a constant! NO!! lets forget about that function to not to make even more confusing!!
lets assume that the number is already there in the constructor eg. size = 256; then do u think it will work or I am missing something?

BTW: Do you know any good links where I could learn more about multidimensional arrays and allocating memory new/delete as I need to solve it quickly!! maybe simple example!?!! Thanks for all replays!!! http://www.opengl.org/discussion_boards/ubb/smile.gif

endo
01-19-2002, 03:23 PM
As far as I know functions cannot return constants, maybe someone else can confirm this?? I still dont think it'll work, but try it - I may be wrong.

Yes, the number can be in the constructor so long as the size of the array to be constructed is known at compile-time. If you look at Inquisitor's code he has declared 2 arrays, int*[10] and int[1], the size is known to the compiler before the code is executed.

What you cannot have is an array, eg a[10], then change it to a[15] or a[100].

Sorry, I dont know of any useful links , all my knowledge is from a London uni as well http://www.opengl.org/discussion_boards/ubb/wink.gif

robert_s
01-19-2002, 05:43 PM
SHIIIIIIIIIIIIIITTTTTT MAN!! endo I found out how to do it!
I am sure it will be useful to know for you guys too!!
OK! This is how you dynamically allocate a 2D array!!
in my class, private section I have

float (*a)[3]; //2D The brackets are important otherwise you'll be declaring an array of pointers! http://www.opengl.org/discussion_boards/ubb/wink.gif

and in my implementation file I have

constructor::constructor()
{
size = object.getSize(); //no need to be constant!!

a = new float[size][3]; //allocates as much memory in left cell as you need!! http://www.opengl.org/discussion_boards/ubb/smile.gif
}

endo! I think (though not 100%) you're right about functions returning constants but in my case that function getSize() does not even need to be a constant at all. you need constants when you have a plain array but for memory allocation there could be a plain int not necessarily a const! but anyway!!!
IT WORKS!!! I can't believe it took me so long!! arghhh.......
Thanks endo and others for help!!

BTW: to delete 2D arrays we dont need to use double square brackets ie. delete [] v[i] //wrong!!
all you need is delete[] v; //right!! http://www.opengl.org/discussion_boards/ubb/smile.gif

[This message has been edited by robert_s (edited 01-19-2002).]