Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: What about the free store? :)

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2001
    Location
    California
    Posts
    22

    What about the free store? :)

    I hope this isn't too off topic...

    void main()
    {
    float bitmapA[200][200][32/8];
    float bitmapB[200][200][32/8];
    }

    This program crashes unless the second line is commented out. So I guess I need to allocate the memory somehow before I use it, but how?

  2. #2
    Member Newbie
    Join Date
    May 2001
    Posts
    39

    Re: What about the free store? :)

    These variables are allocated in the stack, long variables is better allocated in the heap (whith malloc function) or like global variables (out a function).

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2001
    Location
    California
    Posts
    22

    Re: What about the free store? :)

    Thanks, globalizing those arrays fixes that crash and my real program is running fine now.

    None of my C books seem to have explanations of malloc, what kind of book would go into that? Or maybe it would be in the index under something other than allocating or memory?

  4. #4
    Senior Member OpenGL Pro
    Join Date
    Oct 2000
    Location
    Fargo, ND
    Posts
    1,753

    Re: What about the free store? :)

    Are your books really C books or could they be C++ books? malloc/free is basically the C equivalent to the C++ new/delete operators.
    Deiussum
    Software Engineer and OpenGL enthusiast

  5. #5
    Guest

    Re: What about the free store? :)

    Simply 'malloc' takes one parameter that specifies the ammount of memory to allocate and returns a pointer to the memory (you need to type-cast this). 'free' takes a pointer to some malloc'd memory.
    eg to allocate and free an array of 100 ints:
    [CODE]
    // pointer for the memory
    int *pIntArray;

    // allocate the memory
    pIntArray = ( int* ) malloc( sizeof(int) * 100 );
    // some code that uses the memory

    // Now free the memory
    free( pIntArray );

  6. #6
    Junior Member Newbie
    Join Date
    Jan 2001
    Location
    California
    Posts
    22

    Re: What about the free store? :)

    Well Deiussum you're right, I'm all setup for C++ here. Thanks Anon. I've even used the new/delete operators before but I don't think I've ever created multi-dimensional arrays. I'd expect it to work like this:

    float* bmpA = new float [200][200];

    But that line won't compile with that second dimension on there.

  7. #7
    Senior Member Frequent Contributor
    Join Date
    Apr 2000
    Location
    Adelaide, South Australia, Australia
    Posts
    763

    Re: What about the free store? :)

    Code :
    float* bmpA = new float [200][200];
    [][] is really double in-direction, ie. a pointer to a pointer (to a value), so at the very least you'd need float **bmpA. But that won't compile, either.

    What you'll need to do is allocate the array of pointers, and THEN the data:

    float **bmp;

    bmp=new float*[200];
    for(int t=0; t<200; t++)
    bmp[t]=new float[200];

    ie. you're allocating one reference at a time.

    hope this helps,

    cheers,
    John

  8. #8
    Senior Member Frequent Contributor
    Join Date
    Apr 2000
    Location
    Adelaide, South Australia, Australia
    Posts
    763

    Re: What about the free store? :)

    Another thing:

    you MIGHT be better off to "manually" compress your double indirection into a single large vector. the store for bmp[][] described above is NOT contiguous, and it can't easily be cast to a float* that opengl wants when you're binding texture.

    explaining this further:

    float **bmp is a pointer to a pointer to a float. so, bmp points to an ARRAY of pointers. this array is contiguous, but each element in that array points to ANOTHER array, this time of floats. These additional arrays are not necessarily in a single block. ie. in the above example, bmp[1]-bmp[0] is not necessarily 200*sizeof(float).

    you might find it more useful to "pack" the image into a vector:

    Code :
    float *bmp=new float[200*200];
    bmp[x+y*200]=blah; /* set pixel (x,y) */
    cheers,
    John

  9. #9
    Guest

    Re: What about the free store? :)

    that is correct. In face bmp[0]-bmp[1] will be sizeof(float *)

  10. #10
    Guest

    Re: What about the free store? :)

    er bmp[1]-bmp[0] will be sizeof(float *)

Page 1 of 2 12 LastLast

Similar Threads

  1. [GLSL 420]imageStore store nothing.
    By laurent7601 in forum OpenGL: GLSL
    Replies: 3
    Last Post: 10-27-2015, 05:31 AM
  2. store a lot of textures for later use
    By LR123 in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 08-13-2009, 11:29 AM
  3. How to store hard edges?
    By Zoltan in forum COLLADA
    Replies: 3
    Last Post: 09-08-2007, 03:20 PM
  4. How does OpenGL store it's states?
    By WhatEver in forum OpenGL: Advanced Coding
    Replies: 42
    Last Post: 04-18-2001, 01:22 AM
  5. Fastest way to store objects
    By Wolfling in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 03-15-2001, 01:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean