Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
For me, a type cast on the result of malloc() looks more like an attempt of obfuscation. I avoid it for the sake of clarity.
I disagree with you on this. I think it improves readability
I think it doesn't. Usually, you need to use a type cast to cheat on the compiler's type checking - that is, if you want to do something that isn't entirely "legal" in terms of the logics of C.
Assigning a pointer to a freshly allocated chunk of memory, however, is absolutely legal. There's no need for cheating here.
But as soon as I see an explicit type cast in an expression, there's an alarm bell in my programmer's mind: "Watch out! There's something strange, something unusual!" And then I look twice and see: Oh, just an exaggeration in being explicit.
And that doesn't improve readability, it ruins it. And it reduces your sensitivity for really complex expressions that would really deserve special attention.
That's my conviction.
If you cast it (at malloc() ) it to the type you want to use it for later (and double * <> char * for instance), then you can use it naturally later in multiple places without having to cast it at each use...
It does also tell the next guy in advance what to expect in terms of usage and also the compiler...
If you cast it (at malloc() ) it to the type you want to use it for later (and double * <> char * for instance), then you can use it naturally later in multiple places without having to cast it at each use...
It does also tell the next guy in advance what to expect in terms of usage and also the compiler...
all that you say is taken care of by the type of the variable that receives the pointer from malloc() - without explicitly writing a type cast.
Code:
#define BLOCKSIZE 0x1000
int i, char *scratch;
...
if (scratch = malloc(BLOCKSIZE))
{ for (i=0; i<BLOCKSIZE; scratch[i++]=0xFF);
...
};
From that sample it becomes clear that inside the if block, scratch now points to a 4k block of memory. Since scratch was declared as char *, I can use it directly anywhere later in the code. I don't need a cast, neither at the malloc() call, nor later.
I don't know if it makes sense to fill a 4k block with 0xFF, but it's just an example.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.