PDA

View Full Version : memory allocation! still problems!



sandrew
01-17-2002, 04:18 PM
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 i = 0; i < 5; i++ )
{

a[i] = new int[10];

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

/* for( int k = 0; k < 5; k++ )
{
delete[] a[k];
}
*/

}


[This message has been edited by sandrew (edited 01-17-2002).]

mikael_aronsson
01-17-2002, 09:35 PM
OpenGL does not have support for this ;o)

You are only allocating a one dimensional array, but try to use it as a two dimensional array.

int** a = new int[ 2];
a[ 0] = new int[ 10];
a[ 1] = new int[ 10];

Something like that should do it.

Mikael

Platinum
01-17-2002, 10:40 PM
Instead of a[j][i] try to write a[i][j]

mikael_aronsson
01-18-2002, 12:05 AM
Ooops, I wasn't awake there, that's true swap i and j.

Mikael

sandrew
01-18-2002, 12:15 AM
Sorry! My point is to fill in the first array "j" from the left with some values not the right one "i"!!

eg.

a[i] = 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][i] = 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][i] << endl;

[This message has been edited by sandrew (edited 01-18-2002).]

Eric
01-18-2002, 01:31 AM
Originally posted by sandrew:

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][i] << endl;





int **a;
int i,j;
a=new int*[5];
for (i=0;i<5;i++)
{
a[i]=new int[10];
for (j=0;j<10;j++)
a[i][j]=j;
}


or




int a[5][10];
int i,j;
for (i=0;i<5;i++)
for (j=0;j<10;j++)
a[i][j]=j;


Regards.

Eric

sandrew
01-18-2002, 03:15 AM
ok Eric! thanks!!
but... in your example ie.

int **a;
int i,j;
a=new int*[5];
for (i=0;i<5;i++)
{
a[i]=new int[10];
for (j=0;j<10;j++)
{
a[i][j]=j;
}
}
does the same thing as my!
you increment j by 1 and drop each new value of j into twodimensional array (right side) I am trying to do similar thing but I want to be a[j][i]=j ie. I want to drop all these new values into left array cell as I want this side to be of various lengths whereas the right side i I want to be constant ie size 5. eg. I might need the array to be a[1234][5] or a[4000][5] not [5][4000]!
your example achieves the unwanted effect.
I swapped i and j like some people advised but then I get errors because the right array cell is initialized to size 5. it cannot be greter than 5.


I am not sure if my explanation is ok!

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??

[This message has been edited by sandrew (edited 01-18-2002).]

endo
01-18-2002, 03:17 AM
Originally posted by sandrew:
a[i] = new int[10]; //CONFUSED!!

This line isn't really confusing. All you are doing is making each pointer in the 1st array point to a new array of 10 ints. Doing this you get your 5 rows & 10 columns like in your diagram.

Now you will have a[5] pointing to 5 dynamic int[10] arrays.

Hope thats cleared things up a bit...

sandrew
01-18-2002, 03:27 AM
Ok! endo!! but How do I do the other way round!! I want to make each pointer in the 2nd (right cell) array point to a new 1st array of 10 ints. eg. just the post above this one!

DFrey
01-18-2002, 04:20 AM
I'm getting the distinct impression that somebody is wanting other people to do their CS homework. And suprisingly, some appear to be complying, in an OpenGL forum no less. http://www.opengl.org/discussion_boards/ubb/rolleyes.gif

endo
01-18-2002, 04:28 AM
This can get very complicated very quickly http://www.opengl.org/discussion_boards/ubb/wink.gif

What you can do to achieve that affect is use a linked list for the first array to make it dynamic, then make each pointer in the list point to an int array of 5 elements.

This is one way, probably not the simplest, but there are many others. You could probably use vectors for example for the 1st array...

sandrew
01-18-2002, 04:29 AM
DFrey!! you're wrong as its not my cswk!!! http://www.opengl.org/discussion_boards/ubb/smile.gif
I just want to understand it!!
Look!! you dont have to answer my questions if you think you would help me too much!! http://www.opengl.org/discussion_boards/ubb/wink.gif

sandrew
01-18-2002, 04:32 AM
Thats it!! thats what I wanted!! thanks endo!!
Thanks for help everybody!