LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 01-05-2012, 02:09 PM   #16
Doc CPU
Senior Member
 
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,099

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344

Hi there,

Quote:
Originally Posted by qwerty4061 View Post
Quote:
Originally Posted by Doc CPU View Post
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.

[X] Doc CPU
 
Old 01-05-2012, 06:08 PM   #17
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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...
 
Old 01-06-2012, 02:54 AM   #18
Doc CPU
Senior Member
 
Registered: Jun 2011
Location: Stuttgart, Germany
Distribution: Mint, Debian, Gentoo, Win 2k/XP
Posts: 1,099

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Hi there,

Quote:
Originally Posted by chrism01 View Post
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.

[X] Doc CPU
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pipes jayant17 Linux - Newbie 3 02-27-2010 03:04 PM
how to use pipes? rabbit2345 Linux - Software 2 03-29-2008 01:52 PM
[C] stdout and stdin replace by pipes and execve the child chudzielec Programming 6 01-27-2008 05:52 AM
about pipes kpachopoulos Programming 1 10-15-2005 12:37 PM
redirecting stdin and stdout with pipes: convincing apps they're for a terminal? prell Programming 1 09-02-2004 06:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration