Thread: sizeof(void *)*1

  1. #1
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314

    sizeof(void *)*1

    This is from an OpenGL tutorial on NeHe's site.
    Code:
    memset(TextureImage, 0, sizeof(void *)*1);
    It's commented as "Set The Pointer To NULL" but what does it exactly do? MSDN lists memset as "void *memset( void *dest, int c, size_t count );". I understand it as: memset fills the memory at the target of pointer <dest> for the size of <count> bytes with the value <c>.

    That means <count> is sizeof(void *)*1 but what does THAT mean?

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    sizeof(void *) on a 32 bit system is 4

    if they really meant to multiply it by 1 then you are setting 4 bytes of TextureImage to 0. Simple as that.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    What exactly he was doing in this example baffles me! Wont a simple assignment to zero do exactly the same - but more clear and a little more efficiently???

    I notice that he has abandoned his old ways of coding and has now put some efforts into building a C++ library for his tuts.....and that is better than the old stuff

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    *(unsigned long *)TextureImage = 0;

    would have made more sense probably.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    I was never big on NeHe's stuff for newbies, although its getting better and he does have some nice examples.

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by RoD
    I was never big on NeHe's stuff for newbies, although its getting better and he does have some nice examples.
    I like his OpenGL content.....lot's of nice examples

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    memset(TextureImage, 0, sizeof(void *)*1);

    Yuck.

  8. #8
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    So, basically what you are saying is, you have no clue either, right ?

    sizeof(void *)*1

    Sizeof gets the size of something. Void means no return value. Is it a real data type? That would mean a void func would return something which is .. nothing ?

    What does void * mean ? * Has something to do with pointers, but behind void ? Pointer of what ?

    *1 means multiply with 1 ? Why would he do that ?

    This C stuff is makeing me feel more and more dizzy...

  9. #9
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    >>Why would he do that ?


    Have u tried asking him?

    Yea he has some decent OpenGL content but i always found his tutorials a little hard to follow for newbies. Mine arent perfect in any way, but 90% of the emails i get are a combination of praise and coder-error, not RoD error. Not to say i havent messed up, i have lol.

  10. #10
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Originally posted by RoD
    >>Why would he do that ?


    Have u tried asking him?

    Yea he has some decent OpenGL content but i always found his tutorials a little hard to follow for newbies. Mine arent perfect in any way, but 90% of the emails i get are a combination of praise and coder-error, not RoD error. Not to say i havent messed up, i have lol.
    I tried your tutorials, but they are not really a prime example of good portability by using "stdafx". DevC++ also produced a few warnings about non standard code in
    Code:
    int APIENTRY WinMain
    . Basically, I didn't get past the 5th tutorial because of functions like SetTextColor etc.

    Don't get me wrong, I'm sure your tutorials are great. It's just that I use a poor compiler, so they didn't help me. A skilled C++ coder could probably easily work around this, but then, a skilled coder wouldn't need a tutorial.
    The tutorials I found on NeHe's site worked on DevC++ after I found out that I have to link libraries manually.

    Anyway, as for that specific piece of code: If nobody understands it's meaning, it's probably just messy code. I'm fine with not understanding it as long as nobody else does

  11. #11
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by darksaidin
    So, basically what you are saying is, you have no clue either, right ?

    sizeof(void *)*1

    Sizeof gets the size of something. Void means no return value. Is it a real data type? That would mean a void func would return something which is .. nothing ?

    What does void * mean ? * Has something to do with pointers, but behind void ? Pointer of what ?

    *1 means multiply with 1 ? Why would he do that ?

    This C stuff is makeing me feel more and more dizzy...
    No, they told you, it sets the first 4 bytes pointed at by TextureImage to 0. It's not actually setting TextureImage itself to a NULL pointer; it's setting the memory pointed at to 0.

    People may not be sure what it's accomplishing, but it's certain what the command does.

    a void * is a variable which stores the address of something. A void pointer can point to anything, be it an int, a struct, whatever. sizeof(void *) is the size of a generic pointer, and *1 just means multiplied by one. This syntax is common... say you wanted to set an array of 32 ints to zero, you could use:

    memset(array,0,sizeof(int)*32);


    On a 32 bit OS, the line is equivalent to:

    memset(TextureImage,0,4)

    although it would be better to write the whole command as:

    * (int *) TextureImage = 0;

    as was mentioned earlier.
    Last edited by Cat; 07-24-2003 at 10:20 AM.

  12. #12
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Yea theres a few downsides to my tutorials. Mainly the speed at which i put them out (been rlly busy recently) and that i only use VC++ and that causes some problems for those who dont. Eventually i will solve this problem, but at the present time i dont have the time to do both.

  13. #13
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Now, that makes a lot of sense.

    My main problem with that piece of code was (void *). The * is listed behind "void", but thats obviously just the way it's done in C. It even makes sense when used in void *varname.

    Thanks cat!

  14. #14
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    darksaidin you're confusing. you don't understand void *?

    * after a type is pointer notation. It means that you have a pointer to that type. in the case of void * it basically means it can be a pointer to anything really. sizeof(void *) will return 4. sizeof(int *) will also return 4. It's just the size that a pointer on a 32 bit system is.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  15. #15
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help on pointer to VOID
    By dv007 in forum C Programming
    Replies: 19
    Last Post: 07-09-2002, 06:15 PM