Results 1 to 8 of 8

Thread: memset question

  1. #1

    Thread Starter
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577

    memset question

    Hi

    Im following through some OpenGL tutorials and ive come up to a line

    memset(TextureImage,0,sizeof(void *)*1);

    now im not much of a c++ programmer, i only know the basics ( but i do know pointers )
    and what that line above does is, clears out the memory that TextureImage is located. I know what it does but i dont know HOW it does it.

    The thing that confuses me is the sizeof(void *)*1)
    I just cant work out what that is

    Can someone please explain this to me?

    Thanks

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Check your documentation for memset for an explanation.

    The sizeof(void*) * 1 basically resolves to the size of a pointer. So, the memset() sets the first 4 bytes after the memory location pointed to by TextureImage to 0.

    It's not that useful though, that line. I'd expect it to be used to clear a much larger buffer.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577
    Thanks,

    so

    memset(TextureImage,0,sizeof(void *)*1);
    and
    memset(TextureImage,0,4);

    are basically the same?

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Yes. But the size of a pointer is implementation dependent. What is that line of code actually trying to do?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577
    Heres the lines

    int LoadGLTextures()
    {
    AUX_RGBImageRec *TextureImage[1]; // Create Storage Space For The Texture
    memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL
    ...
    }

    What is does, is first that function creates some storage space for a texture, then using the memset, it clears the imagerec to make sure its empty.

    its only that memset line that has gotten me confused
    I was gonna just ignore it and continue, but i really do like to know what each and evey line does.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Why bother using an array for that first one? There's only space for one, so you might as well use:
    Code:
    int LoadGLTextures() {
        AUX_RGPImageRec *TextureImage = NULL;
    
        ...
    }
    If you had 5 textures, then it would be more useful:
    Code:
    int LoadGLTextures() {
        AUX_RGPImageRec *TextureImage[5]; // creates an array of five pointers
    
        memset(TextureImage, 0, sizeof(AUX_RGPImageRec*) * 5);
    
        ...
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7

    Thread Starter
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577
    dunno, im just following a tutorial lol

    VB Code:
    1. memset(TextureImage, 0, sizeof(AUX_RGPImageRec*) * 5);

    that makes

    VB Code:
    1. sizeof(void*)*1);

    more understanding now

    thanks

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    No. In your case, the sizes of the two pointers will be the same. In my example, it clears the five *pointers*, not anything they may point to.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width