void ** ?? sizeof(void*)?? what is this?

This is a discussion on void ** ?? sizeof(void*)?? what is this? within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to use a program that has the a variable of type void** but I don't understand ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    Question void ** ?? sizeof(void*)?? what is this?

    Hi,
    I am trying to use a program that has the a variable of type void** but I don't understand well what is meaning of this.
    Does any knows what is the meaning of the following code?
    I specifically would like to know how many bytes are allocated in pdata and data.(and the reason)
    Al least one of them is data of pixels (since imgtmp is an image structure)
    imgtmp->pdata is void** type and
    imgtmp->data is void* type,
    I replaced some vars for constants as you can see.

    This a fragment of code that creates an image structure.
    Code:
            void		**pdata;
    	if ( NULL == (pdata = (void **)malloc(sizeof(void *) * 4)) ){ //4 is the image height
    		//free data, function failed
    		return();
    	}
    	imgtmp->pdata  = pdata;
    	if ( NULL == (imgtmp->data = (char *)malloc(12)) ){ //12 is image hole buffer size
    		//free data, function failed
    		return();
    	}
    	for (int i=0 ; i<4 ; i++ ) //4 is the image height
    		pdata[i] = (void *)(imgtmp->data + i*3); //3 is bytes per line in the image
    As you can see I am trying to allocate a 3*4 image.

    BTW: I tried sizeof(void *) and it gives me a long unsigned 8, so pdata should be 32, but is always 8 , why?
    Mac OS 10.6 Snow Leopard : Darwin

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    sizeof tells you the size of a type (void* or void**), it couldn't possibly tell you how much memory you allocated with malloc (sizeof is evaluated by the compiler, not at runtime).

    Your only way to know how much memory has been allocated for something, is to store that in a variable.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    949
    All I know is, you're not supposed to do this:
    (void **)malloc(sizeof(void *) * 4)
    or this:
    (char *)malloc(12)

  4. #4
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    949
    Why would void return a nonzero size anyway?

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

    Question

    sizeof(void*), sizeof(void**), sizeof(any_pointer) is most likely the same size. Pointers are pointers they just store addresses. So no suprise they are both 8 bytes

    imgtmp->data seems to be char* not void*

    void** is a pointer that points to a pointer of type void*.

    Code explanation. Lets say a pointer is 8bytes
    You allocate 4 x void* pointers of space. So 32 bytes of space.
    You assign that to pdata. Pdata is still 8 bytes but it points to a memory that is 32 bytes.

    Now imgtmp->pdata = pdata means that imgtmp->pdata points to that memory as well.
    imgtmp->data points to a memory worth of 12 bytes.

    Now, since imgtmp->pdata is type of void** it is can be seen as an array of void* pointers. You have 4 such pointers. The first will point on the first byte of the 12 bytes that imgtmp->data points, the second on the 4th, the third on the 8th and the fourth on the 12th byte. That way you can use imgtmp->pdata with indexes to get whichever byte you mean. Like imgtmp->pdata[1][2] will give you 2nd row, 3rd column byte.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126
    Quote Originally Posted by Epy View Post
    All I know is, you're not supposed to do this:
    (void **)malloc(sizeof(void *) * 4)
    or this:
    (char *)malloc(12)
    even though i got normal resulta when running sample data with this
    Mac OS 10.6 Snow Leopard : Darwin

  7. #7
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    949
    gotcha, I was thinking about plain void instead of a pointer, my mistake.

  8. #8
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    949
    Quote Originally Posted by nacho4d View Post
    even though i got normal resulta when running sample data with this
    I'm sure.

    FAQ: Casting malloc?

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,198
    Quote Originally Posted by nacho4d View Post
    even though i got normal resulta when running sample data with this
    You will. The conversions you're doing are legal C, but people generally consider them to be a bad idea as they can obscure some programming errors.

    Note that, if your compiler is a C++ compiler, the conversions are necessary - even if your code is C - because C++ does not allow an implicit conversion from (void *) to other pointer types.
    Right 98% of the time, and don't care about the other 3%.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    Thanks C_nua

    Thank you very much C_nua for your kind explanation
    i think I got the idea, thanks again.

    ...

    This board is great since I register like 3 years ago!. Is the best for c questions
    Mac OS 10.6 Snow Leopard : Darwin

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  3. game window rejected painting !
    By black in forum Windows Programming
    Replies: 4
    Last Post: 03-27-2007, 01:10 AM
  4. msvc just ate one of my source files
    By Eber Kain in forum C++ Programming
    Replies: 6
    Last Post: 07-01-2004, 05:40 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21