Thread: dinamic memory question

  1. #1
    Registered User madsmile's Avatar
    Join Date
    Feb 2002
    Posts
    26

    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])
    MADSmile
    ICQ #3653692
    (i'm running Borlad C++ Ver 3.1 under MSDOS)

  2. #2
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    ????

    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. #3
    Registered User madsmile's Avatar
    Join Date
    Feb 2002
    Posts
    26
    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); ]
    MADSmile
    ICQ #3653692
    (i'm running Borlad C++ Ver 3.1 under MSDOS)

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    363
    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 subscribe to a feed

Similar Threads

  1. heap vs stack memory question
    By donglee in forum C++ Programming
    Replies: 4
    Last Post: 01-23-2009, 04:34 PM
  2. Pointer memory question
    By Edo in forum C++ Programming
    Replies: 5
    Last Post: 01-21-2009, 03:36 AM
  3. Memory question
    By John_L in forum Tech Board
    Replies: 8
    Last Post: 06-02-2008, 10:06 PM
  4. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  5. c style string memory leak question
    By curlious in forum C++ Programming
    Replies: 7
    Last Post: 09-14-2003, 06:31 AM