Allocatin 2dimensional arrays using new

Started by
9 comments, last by Sleepwalker 24 years, 6 months ago
A question concerning C++ programming: How do I allocate a 2-dimensional array using new ? I knot that you''d use IntegerArray = new int[100]; to allocate a one dmensional array, but what about 2 (or even more) dimensions? using IntegerArray = new int[100][100] dioesnt''t work... Can anyone help me ? - Sleepwalker
- Sleepwalker
Advertisement
I''d try this.

IntegerArray = new int[100*100];



then access it like a two dimensional array.
- Ryan -
Use a struct. Put you vector in it and use new on the struct.

Gandalf the White
Gandalf the Black
Thats the right way :

// First the rows or cols
IntegerArray = new int[100];

// And then the rest
for( int x=0; x<100-1;x++ )
IntegerArray[x] = new int[100];

BYE
Philipp
Solotics
//// Static Allocation
int Toto[123][321];

//// Dynamic Allocation
int *Toto;

// Allocation of the array of (int) pointers
Toto = new int* [123]; // Never forget the ''*'' !

// Allocation of each sub-array
for(int i=0; i<123; i++)
{
Toto = new int [321];
}

// Use ...

// Delete
for(int i=0; i<123; i++)
{
delete [] Toto;<br>} <br>delete [] Toto;<br><br>Bye !<br>(sage celui, qui dit qu''il ne sait rien) </i>
BleuQui est d''une couleur voisine du rouge, mais pas très : un ciel bleu, des yeux bleus, les flots bleus, une Opel Kadett bleue. Fig. Bouch. : un steak bleu : s''emploie pour désigner un steak rouge. Fig. Mar. : bizut; "Faut pas me prendre pour un bleu" (Rackham-Le-Rouge).
// First the rows or cols
IntegerArray = new int[100];

// And then the rest
for( int x=0; x<100-1;x++ )
IntegerArray[x] = new int[100];

That is one way to implement an array, however you cannot access the array like this:

IntegerArray[x][y]

without creating an array class because the rows and cols may not be in a continious block of memory

- Ryan -
quote: Original post by RMack
// First the rows or cols
IntegerArray = new int[100];

// And then the rest
for( int x=0; x<100-1;x++ )
IntegerArray[x] = new int[100];

That is one way to implement an array, however you cannot access the array like this:

IntegerArray[x][y]

without creating an array class because the rows and cols may not be in a continious block of memory

::sigh::
RMack, that code won't compile, nor is it logically sound.

First off the type of a two-dimensional array is an (int **). The result of a new int[100] is an (int *) pointer. So there is an incompatible pointer assignment.

Second off when filling your array, your bounds check is 100-1. This is incorrect as well. This will result in memory for the array not being completely allocating. Probably a seg fault will occur.

Third, you *can* access a dynamically allocated two dimensional array with [][].

Compare the following:
typedef int * pint;int main(int argc, char* argv[]){	int ** intarray;	intarray = new pint[3];	for (int i = 0; i < 3; i++)	  intarray = new int[4];<br><br>	intarray[2][2] = 5;<br><br>	printf("%d\n", intarray[2][2]);<br>	return 0;<br>}<br> </pre> <br><br>Edited by - SiCrane on 2/17/00 10:40:25 AM    
To SiCrane:

1) You are correct.
2) You further proved my point.
3) That code wasn't mine, It was posted anonymously. My code was posted earlier.

Accually, the type of the two dimensional array would be int* not int** which is simply a pointer to a pointer.


Edited by - RMack on 2/17/00 12:00:56 PM
- Ryan -
No, in C++, a dynamically allocated two dimensional array is an int **. A pointer to an array of pointers to arrays.

An integer is of type int.
A pointer to an array of integers is of type (int *).
A pointer to an array of pointers to integers is of type (int **).

And your handle is attached to the code that I quoted. If you meant to quote the previous code, for clarity you should enclose the code in
blocks. As it stands it appears that you were the anonymous poster from before.
Sorry for not being clear. I''ve never heard of there being a difference in how c++ indexes arrays. I''ll have to look into that. If I''m wrong, I stand corrected.
- Ryan -

This topic is closed to new replies.

Advertisement