![]() |
| | #1 |
| Registered User Join Date: Nov 2006 Location: japan
Posts: 104
| 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
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 | |
| | #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:
| |
| anon is offline | |
| | #3 |
| 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) |
| Epy is offline | |
| | #4 |
| I like turtles Join Date: Sep 2009 Location: Ohio
Posts: 179
| Why would void return a nonzero size anyway? |
| Epy is offline | |
| | #5 |
| Registered User Join Date: Jun 2008
Posts: 1,134
| 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 | |
| | #6 |
| Registered User Join Date: Nov 2006 Location: japan
Posts: 104
| even though i got normal resulta when running sample data with this
__________________ Mac OS 10.6 Snow Leopard : Darwin |
| nacho4d is offline | |
| | #7 |
| I like turtles Join Date: Sep 2009 Location: Ohio
Posts: 179
| gotcha, I was thinking about plain void instead of a pointer, my mistake. |
| Epy is offline | |
| | #8 | |
| I like turtles Join Date: Sep 2009 Location: Ohio
Posts: 179
| Quote:
FAQ: Casting malloc? | |
| Epy is offline | |
| | #9 | |
| Registered User Join Date: Jun 2005
Posts: 1,343
| Quote:
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 | |
| | #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 | |
![]() |
| Tags |
| malloc, sizeof(void *), void ** |
| Thread Tools | |
| Display Modes | |
|
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 |