• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Static data?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Static data?


  • Subject: Re: Static data?
  • From: George Warner <email@hidden>
  • Date: Wed, 07 Jul 2004 13:37:21 -0700

On Wed, 7 Jul 2004 10:12:33 +0200, Markus Hitter <email@hidden> wrote:
> To be honest, I sometimes had the wish to create a multi-dimensional,
> dynamically sized array at runtime like:
>
>  int foo[i][j][k] = malloc(sizeof(foo));

Back in the days before OpenGL (and RAVE! ;-) I saw a lot of (game)
developers allocate (8-bit) video buffers like this:

typedef UInt8 pixel;
typedef pixel* pixLine;

typedef struct pixBuf_struct {
    int            fRows;
    int            fCols;
    pixLine*    fPixels;
}pixBuf_rec, *pixBuf_ptr;

static pixBuf_ptr malloc_pixbuf(int inRows, int inCols) {
    pixBuf_ptr tPixBuf = (pixBuf_ptr) malloc(sizeof(pixBuf_rec));
    if (NULL != tPixBuf) {
        tPixBuf->fRows = inRows;
        tPixBuf->fCols = inCols;

        // these are the actual pixels
        pixel* tPixels = (pixel*) malloc(inRows * inCols * sizeof(pixel));
        if (NULL != tPixBuf) {
            // these are pointers to the beginning of each scanline
            tPixBuf->fPixels = (pixLine*) malloc(inRows * sizeof(pixLine));
            int row;
            for (row = 0; row < inRows; row++) {
                tPixBuf->fPixels[row] = &tPixels[row * inCols];
            }
        }
    }
    return tPixBuf;
}

static void free_pixbuf(pixBuf_ptr inPixBuf)
{
    if (NULL != inPixBuf) {
        if (NULL != inPixBuf->fPixels[0]) {
            free((void*) inPixBuf->fPixels[0]);
            inPixBuf->fPixels[0] = NULL;
        }
        free((void*) inPixBuf);
    }
}

// Note how the following code uses two dimensional
// reference to array of pointers to pixels.
static void init_pixbuf(pixBuf_ptr inPixBuf)
{
    int row,col;
    for (row = 0; row < inPixBuf->fRows; row++) {
        for (col = 0; col < inPixBuf->fCols; col++) {
            inPixBuf->fPixels[row][col] = 0; // set to black
        }
    }
}

static void setup_pixbuf(void)
{
    pixBuf_ptr tPixBuf = malloc_pixbuf(480, 640);
    if (NULL != tPixBuf)
    {
        init_pixbuf(tPixBuf);
    }
}

--
Enjoy,
George Warner,
Schizophrenic Optimization Scientist
Apple Developer Technical Support (DTS)
_______________________________________________
xcode-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/xcode-users
Do not post admin requests to the list. They will be ignored.


  • Prev by Date: Re: How to install HeaderDoc 8?
  • Next by Date: images & nib files
  • Previous by thread: Re: Static data?
  • Next by thread: PRECOMP_DESTINATION_DIR
  • Index(es):
    • Date
    • Thread