Skip to main content
gamedev.net
🔒 Locked

Memory allocation problems again!

Started by sandrew Jan 17, 2002 at 8:36 PM 5 replies 500+ views
Original Post
sandrew
sandrew
hi! I am trying to display 10 numbers in 5 columns! please help!! first array must display character line (j=10) and second row number i=5. the program crashes! void main() { int *a[5]; for( int ii = 0; ii < 5; ii++ ) { a[ii] = new int[10]; for( int j = 0; j < 10; j++ ) { a[j][ii] = j; cout << a[j][ii] << " "; } cout << endl; } /* for( int k = 0; k < 5; k++ ) { delete[] a[k]; } */ } Edited by - sandrew on January 17, 2002 10:00:57 PM
ZeroBit
ZeroBit
You''ve misplaced j and ii when filling the array.


for( int ii = 0; ii < 5; ii++ )
{

a[ii] = new int[10];

for( int j = 0; j < 10; j++ )
{
a[ii][j] = j;
cout << a[ii][j] << " ";
}
cout << endl;
}


That should work.
Havent tried it though.
sandrew
sandrew
Sorry! My point is to fill in the first array "j" from the left with some values not the right one "i"!!

eg.
a[ii] = new int[10]; //CONFUSED!! am I allocating size 10 for left or right array? I thought is for left!! as right is already defined to be 5 so what is wrong!!??
for( int j = 0; j < 10; j++ )
{
a[j][ii] = j; //want to fill j with values 0-10
}
cout << endl;
}

can I do it like that? (see code in my first post)
when I declare the array at the top I want to have the first array to be dynamic and second of size 5. this means that I want to have 5 rows where each rows length could be changed dynamically! so every value in the inside for() loop should be placed into left array the dynamic one not into the right one as its size is already defined to be 5. this is the row number!
So how do I fill j array with those 10 values? (note: it must be dynamica the left array)


at the end I want to end up with

j j j j j j j j j j
i0123456789
i0123456789
i0123456789
i0123456789
i0123456789

when using cout << [j][ii] << endl;


Edited by - sandrew on January 18, 2002 5:35:36 AM
Dormeur
Dormeur
The idea of "rows" and "columns" is purely arbitrary. It''s the programmer who decides whether to use the row/column approach, or the column/row approach.

But I can understand your confusion about the arrays. Cells in an array are referenced through their index. If it''s a multidimensional array, their "index" consists of more than one number. To reference an element in a twodimensional array, you need two numbers, to reference an element in an n-dimensional array, you need n numbers.

What the meaning is of the indices is entirely up to you. You can even put your matrix in a one-dimensional array, and get to the desired cell by calculating the index:
  
const int dim = 5;
int multiDimArray[dim][dim];
int row = 4, col = 4;
// Set the "bottom right" element of the matrix

multiDimArray[row][col] = 10;
// Or do you store columns first, then rows?

multiDimArray[col][row] = 10;

int singleDimArray[dim*dim];
// Same, but with a one-dimensional array

singleDimArray[row*dim + col] = 10;
// Or columns first?

singleDimArray[col*dim + row] = 10;


I''m sure that after reading this piece of code, you''re even more confused then you were before . Just to say that arrays aren''t that simple, especially multidimensional ones. Even expert programmers still have to sit down and think their code over when working with multidimensional arrays.

But you have made it extra difficult by using an array of pointers, and then allocating another array to the pointers. So what you have made is this: an array that has room for 5 objects of some type. In this case, the object type is int* (pointer to int). Then, for each of those 5 cells, you allocate an array of 10 integers to it (which you call a "row"). So if you want to access the 5th element of the second row, you have to address it by using the row index first, then the column. So: a[1][4] would give you the right element.

It''s not because the "int *a" piece is before the "[5]" piece, that you have to address them in that way. The "int *" part is just the type definition, it does not tell you how to address the cells.

ZeroBit was right, you just have to swith the j and the ii to make it work.

Dormeur
sandrew
sandrew
Ok Dormeur!
How should I do in order to achieve array size like eg.

a[1234][5] or a[200][5] or a[512][5].

note here right cell should be always 5. the left array cell could be of different sizes like 1234 or 512 therefore I want to use dynamic allocation for the left cell and for the right just size 5! I dont want to do the other way even though its the same thing!! I just want to know how to do this way!
Any ideas??
OldGuy
OldGuy
Looks good, except the array indices are off by 1. If you want an array of 400x5, you refer to the last element as ''elem[399][4]'', not ''elem[399][5]''.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.