Question:
C malloc funtion...void pointer?
Ruth Rolyo
2011-11-09 05:51:27 UTC
Hello guys! I am learning C and I am curious about this function malloc. I have come across lessons about this function and I noticed that everytime they would point out that the malloc function returns a void pointer. Now what I wanna know is there a difference between an ordinary pointer and a void pointer? Are they so different that they keep emphasizing that it is void?

Please guys I don't accept short answers. Thanks!
Three answers:
oops
2011-11-09 06:28:17 UTC
What Chris B said is in no way applicable to C. void pointers can hold the address of perfectly valid objects. There is no such thing as "reference types". He's either a Java or a C# programmer, and he's confusing void for null.



The difference between an ordinary pointer and a void pointer is just the type that it points to. A void pointer points to nothing, so if you dereference it, you get nothing. A void pointer can be implicitly cast to any other pointer type. Since malloc doesn't know what type of object you intend to store in the memory it allocates, it just returns void pointer, which you can assign to whatever other pointer type you need.



void pointers are also used for generic programming. It's not a great, but it's the best that C's got, it's a very primitive language. For example, see qsort: http://en.cppreference.com/w/cpp/algorithm/qsort
Chris B
2011-11-09 06:03:52 UTC
Ok conceptually programming has two places where data is stored. One is called the heap, the other the stack. Your base types are stored within the stack and the software interrogates the stack when you attempt to access something. Your reference types are stored in the heap and are only ever accessed by a pointer maintained by the stack. So you could say the stack is ordered storage and the heap like its namesake is just a bunch of data. A void pointer is a pointer that has been initialized in the stack but not yet set to address a heap location, thus it is a pointer to null. Once a value has been placed into the heap and its address assigned to the pointer, then the pointer will become an "ordinary pointer"



****** Edit ********



I stand corrected, or sit corrected at the moment. The above describes a null pointer not a void pointer, I defer explaination to my betters Oops tnx for correction.



?
2016-10-14 02:48:31 UTC
The coder of somefunction() desires to allocate a heap-staying power pointer variable that is used to show to a character or an array of characters. (unusual element to do, via itself.) to try this, they use malloc(), it is the type you allocate heap-staying power variables of any style. malloc() desires to properly known how plenty area to allocate. on account that "variable" is a character pointer, the programmer used sizeof(char*) so as to compute the form of bytes required for that variable. malloc() returns a pointer to the allotted memory. although, on account that malloc() can no longer in all probability comprehend what it is returning (it in simple terms is familiar with how huge, no longer what form), it returns all such memory regulations as (void *). C expects you to tell it what form of pointer is being back. So the (char*) solid is needed. C demands that a void* pointer (it is what malloc() returns) be address aligned for any form of variable. C additionally demands that a void* pointer be sufficiently huge (length smart) that advice that could desire to different varieties could be converted to it without loss of alignment or bits. C additionally demands that char* and void* be extra or less equivalent, yet while "variable" pointed to a minimum of a few thing different than char it is achieveable that the pointer varieties could choose some form of conversion. (oftentimes no longer, even though it is achieveable.) So the forged is needed. On a sixteen-bit linear address device, sizeof(char*) oftentimes is two. On a 32-bit linear address device, sizeof(char*) oftentimes is 4. etc. it is oftentimes found out at collect time, too. So there's no computation time in touch. It in simple terms gets replaced with a relentless.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...