ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
hello,
I want to know if a fuction say malloc is declared as void *malloc() then whats the significance of void here.
Does void * i used when function has the flexibility to return any type of value by casint that functions with appropriate data type?
void * means that the function returns a pointer of some unspecified type. You'll need to cast the return value to be a pointer to the correct type for it to be of much use.
In C, you want to avoid typecasting the return value of malloc(). As long as you #include <stdlib.h> then the compiler will automatically convert the void * pointer. For C++ you'll need to typecast it, but you shouldn't be using malloc() in C++ anyway. You should be using new.
Originally posted by trevelluk void * means that the function returns a pointer of some unspecified type. You'll need to cast the return value to be a pointer to the correct type for it to be of much use.
One should not cast the return value of malloc() when writing C.
When stdlib.h has been included, malloc()'s declaration will be visible. Its return type is void*, and void* can be silently converted to other pointer types.
If one should forget to include the declaration, malloc will be assumed to return int.
ints will be converted to pointer types -- but typically that conversion will not be silent. Most, if not all, C compilers will produce a warning to the effect of "you are converting an int into a pointer without explicitly casting -- are you sure you want to?". This alerts one to the fact that one has omitted to include a header.
If you put the cast in, that message will be quashed -- after all, the point of casts is to say to the compiler, "Ignore what you think you know and do what I say" -- and a [potential] problem be left latent in your code.
As you see casting the returning value of malloc may lead to bugs that are really hard to track down.
This is something that is been discussed in this forum again. I did not know that until recently either...
Last edited by perfect_circle; 03-09-2005 at 10:50 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.