C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-02-2009, 06:04 AM   #1
Registered User
 
Join Date: Nov 2006
Location: japan
Posts: 104
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
nacho4d is offline   Reply With Quote
Old 10-02-2009, 06:13 AM   #2
The larch
 
Join Date: May 2006
Posts: 3,082
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.

Quote:
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).
anon is offline   Reply With Quote
Old 10-02-2009, 06:14 AM   #3
Epy
I like turtles
 
Join Date: Sep 2009
Location: Ohio
Posts: 179
All I know is, you're not supposed to do this:
(void **)malloc(sizeof(void *) * 4)
or this:
(char *)malloc(12)
__________________
Jake, that CAD guy
Hazudra Fodder
Epy is offline   Reply With Quote
Old 10-02-2009, 06:21 AM   #4
Epy
I like turtles
 
Join Date: Sep 2009
Location: Ohio
Posts: 179
Why would void return a nonzero size anyway?
__________________
Jake, that CAD guy
Hazudra Fodder
Epy is offline   Reply With Quote
Old 10-02-2009, 06:28 AM   #5
Registered User
 
C_ntua's Avatar
 
Join Date: Jun 2008
Posts: 1,134
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.
C_ntua is offline   Reply With Quote
Old 10-02-2009, 06:31 AM   #6
Registered User
 
Join Date: Nov 2006
Location: japan
Posts: 104
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
nacho4d is offline   Reply With Quote
Old 10-02-2009, 06:32 AM   #7
Epy
I like turtles
 
Join Date: Sep 2009
Location: Ohio
Posts: 179
gotcha, I was thinking about plain void instead of a pointer, my mistake.
__________________
Jake, that CAD guy
Hazudra Fodder
Epy is offline   Reply With Quote
Old 10-02-2009, 06:33 AM   #8
Epy
I like turtles
 
Join Date: Sep 2009
Location: Ohio
Posts: 179
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?
__________________
Jake, that CAD guy
Hazudra Fodder
Epy is offline   Reply With Quote
Old 10-02-2009, 06:45 AM   #9
Registered User
 
Join Date: Jun 2005
Posts: 1,343
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%.
grumpy is offline   Reply With Quote
Old 10-02-2009, 07:22 AM   #10
Registered User
 
Join Date: Nov 2006
Location: japan
Posts: 104
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
nacho4d is offline   Reply With Quote
Reply

Tags
malloc, sizeof(void *), void **

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Another syntax error caldeira C Programming 31 09-05-2008 01:01 AM
30something GSOH seeks help with the basics of a minor programme promsan C Programming 3 05-13-2007 08:55 AM
game window rejected painting ! black Windows Programming 4 03-27-2007 01:10 AM
msvc just ate one of my source files Eber Kain C++ Programming 6 07-01-2004 05:40 AM
Half-life SDK, where are the constants? bennyandthejets Game Programming 29 08-25-2003 11:58 AM


All times are GMT -6. The time now is 08:25 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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