# Thread: dinamic memory question

1. ## dinamic memory question

Sorry i know this is a stupid question but i don't know how to do this...

i need to create a dinamic matrix to capture some pixels of the screen...

i want something like this (i know this code doesn't work)

void capture_matrix (int x, int y)
{

int m[][] = new m[x][y] sizeof (int)

/*
the rest of my code
*/
}

how can i do that???? i really need to do that for capture a matrix of pixeles with getpixel(); it is for capture first a small matrix, then draw something over that and then restore the matrix with putpixel(m[cx][cy])

2. ????

Code:
void capture_matrix (int x, int y, int w, int h)
{
unsigned long matrix = new unsigned long [w][h];
for (int i = x; i < (x+w); ++i) {
for (int j = y; j < (y+h); ++j) {
matrix[i-x][j-y] = getpixel(screen, i, j);
}
}
}
You're not very clear. ... I didn't test that code either.

3. void capture_matrix (int x, int y, int w, int h)
{
unsigned long matrix = new unsigned long [w][h];
for (int i = x; i < (x+w); ++i) {
for (int j = y; j < (y+h); ++j) {
matrix[i-x][j-y] = getpixel(screen, i, j);
}
}
}
this gave me diferent errors:
- constant expression required (i don't know what the heck this is) [this error on line unsigned long matrix = new unsigned long [w][h]; ]
- Canoot convert 'unsigned long[1] *' to ' unsigned long' [this error on line unsigned long matrix = new unsigned long [w][h]; ]
- invalid indirection [this error on line matrix[i-x][j-y] = getpixel(screen, i, j); ]

4. This topic is being added to the faq, but I don't know if lightatdawn has put it up yet.
But here's the code you want:
Code:
int** Capture_Matrix(int x, int y);

int main(void)
{
int** Matrix = Capture_Matrix(4, 8);
int* Column;

for (int i = 0; i < 4; i++)
{
Column = Matrix[i];
for (int j = 0; j < 8; j++)
{
Column[j] = i + j;
}
}

delete []Matrix;
return 0;
}

int** Capture_Matrix(int x, int y)
{
//Pay attention to this bit
int** NewMatrix = new int*[y];
for (int i = 0; i < y; i++)
{
NewMatrix[i] = new int[x];
}
return NewMatrix;
}
All it does is create a 4 by 8 matrix and set each element to its column number plus its row number (although I didn't compensate for 0 based arrays as this would over complicate the example).

Popular pages Recent additions