Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > (FAQ details:) malloc(), void * and casts

Reply
Thread Tools

(FAQ details:) malloc(), void * and casts

 
 
santosh
Guest
Posts: n/a
 
      11-20-2007
In article <(E-Mail Removed)>, CJ <(E-Mail Removed)>
wrote on Tuesday 20 Nov 2007 4:29 am:

> On 19 Nov 2007 at 19:51, Ian Collins wrote:
>> CJ wrote:
>>> On 13 Nov 2007 at 13:20, Mark Bluemel wrote:

>>
>>>> Because according to the standard, pointer to void can be converted
>>>> to and from any other object pointer type.
>>>
>>> In many older implementations of the standard library, malloc is
>>> defined to return a char * rather than a void *, so including the
>>> cast makes your program more portable.
>>>

>> That's a very rare corner case. Such libraries probably require
>> malloc.h as well.

>
> I'm not sure I'd call it "very rare" - there's plenty of old iron out
> there.
>
> Writing in the common subset of K&R C, C89 and C99 where possible
> costs you very little, and buys you additional portability.


While I can understand the problem of maintaining old code, I'd be very
surprised of any current systems which have only a pre-ANSI C compiler
available. Can you give an example?

 
Reply With Quote
 
 
 
 
Old Wolf
Guest
Posts: n/a
 
      11-20-2007
On Nov 20, 8:43 am, CJ <(E-Mail Removed)> wrote:
> In many older implementations of the standard library, malloc is defined
> to return a char * rather than a void *, so including the cast makes
> your program more portable.


I wouldn't include other languages (or other versions
of the same language) in my definition of 'portable'.

A portable C89 program means it can be compiled by C89
compilers on any other system that has one.

Would you call it 'enhancing portability' to write your
program in the common subset of C and Perl? Such
programs certainly do exist.
 
Reply With Quote
 
 
 
 
Old Wolf
Guest
Posts: n/a
 
      11-20-2007
On Nov 20, 11:05 am, Julienne Walker <(E-Mail Removed)> wrote:
> On Nov 19, 2:41 pm, Paul Hsieh <(E-Mail Removed)> wrote:
> > What are you talking about? I write code like that, pretty much
> > exclusively nowadays. Its not challenging in the least. Certainly
> > consumers of the Better String Library appreciate it.

>
> I won't comment on your Better String Library, but a C library
> designed to work with C++ does fall under the exception I was talking
> about.


He doesn't write a C library to work with C++. (FWIW, almost every
C library works with C++, with minor header tweaks). He writes code
that can either be compiled as a C library, or as a C++ library.
Nobody can really explain why he does this.
 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      11-20-2007
"santosh" <(E-Mail Removed)> schrieb im Newsbeitrag
news:fhugev$iq5$(E-Mail Removed)...
> While I can understand the problem of maintaining old code, I'd be very
> surprised of any current systems which have only a pre-ANSI C compiler
> available. Can you give an example?

Not 100% sure, but I believe with HP/UX you get a pre ANSI Compiler as long
as you don't buy/license a 'real' one (that ancient compiler seems to be
needed/sufficient to create a new kernel)

Bye, Jojo


 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      11-20-2007
In article <fhuno9$i11$(E-Mail Removed)>, Joachim Schmitz
<(E-Mail Removed)> wrote on Tuesday 20 Nov 2007 7:03 pm:

> "santosh" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:fhugev$iq5$(E-Mail Removed)...
>> While I can understand the problem of maintaining old code, I'd be
>> very surprised of any current systems which have only a pre-ANSI C
>> compiler available. Can you give an example?

>
> Not 100% sure, but I believe with HP/UX you get a pre ANSI Compiler as
> long as you don't buy/license a 'real' one (that ancient compiler
> seems to be needed/sufficient to create a new kernel)


Understood, but I was asking for an example of a platform where a
pre-ANSI compiler is the _only_ option.

Certain embedded systems might have non-conformant compilers, but that's
(I think) different from a pre-ANSI compiler.

 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      11-20-2007
CJ <(E-Mail Removed)> wrote:

> On 19 Nov 2007 at 19:51, Ian Collins wrote:
> > CJ wrote:
> >> In many older implementations of the standard library, malloc is defined
> >> to return a char * rather than a void *, so including the cast makes
> >> your program more portable.
> >>

> > That's a very rare corner case. Such libraries probably require
> > malloc.h as well.

>
> I'm not sure I'd call it "very rare" - there's plenty of old iron out
> there.
>
> Writing in the common subset of K&R C, C89 and C99 where possible costs
> you very little, and buys you additional portability.


I'm sorry: writing

func()
short a;
float b;
{
...
}

instead of

int func(short a, float b)
{
...
}

costs very little? You might want to rethink that.

In the case under discussion, casting malloc() calls causes your brain
to rot. To be precise, it causes deterioration, frequently irreversible,
of the parts that let you think "Hey! A cast! Better be careful, there's
something iffy going on here."

Richard
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      11-20-2007
santosh wrote, On 20/11/07 14:26:
> In article <fhuno9$i11$(E-Mail Removed)>, Joachim Schmitz
> <(E-Mail Removed)> wrote on Tuesday 20 Nov 2007 7:03 pm:
>
>> "santosh" <(E-Mail Removed)> schrieb im Newsbeitrag
>> news:fhugev$iq5$(E-Mail Removed)...
>>> While I can understand the problem of maintaining old code, I'd be
>>> very surprised of any current systems which have only a pre-ANSI C
>>> compiler available. Can you give an example?

>> Not 100% sure, but I believe with HP/UX you get a pre ANSI Compiler as
>> long as you don't buy/license a 'real' one (that ancient compiler
>> seems to be needed/sufficient to create a new kernel)

>
> Understood, but I was asking for an example of a platform where a
> pre-ANSI compiler is the _only_ option.


You can also get gcc for HP/UX, so you do not even have to pay to have a
C89/C90/C95 compiler.

I believe there are still a few people using a few ancient machines (or
emulations of them) where the machine itself pre-dates the ANSI standard
by far enough that no one has written an compiler for the target that
supports the standard. I seem to remember someone on comp.std.c
mentioning that they were in this position. I can't remember why they
were using such ancient systems.

> Certain embedded systems might have non-conformant compilers, but that's
> (I think) different from a pre-ANSI compiler.


It is very different.
--
Flash Gordon
 
Reply With Quote
 
Lorenzo Villari
Guest
Posts: n/a
 
      11-20-2007

"Paul Hsieh" <(E-Mail Removed)> ha scritto nel messaggio
news:(E-Mail Removed)...
> On Nov 19, 7:38 am, "Lorenzo Villari" <(E-Mail Removed)> wrote:
> > "Paul Hsieh" <(E-Mail Removed)> ha scritto nel


> Urg. I shouldn't have put myself out there, now I can't dig up a
> concrete example. I am an avid user of the WATCOM C/C++ compilers.
> They decided to supply their C and C++ solutions as two separate
> compilers and at various times they have clearly put more effort into
> their C++ optimizer. I had some benchmarks somewhere long ago that
> demonstrated this.
>
> > 2) What do you mean by "mantained". Maybe that we have fewer C
>> compilers
> > than C++ ones? If this is what you meant, I won't be too happy
>> about that

>
> Well C compilers certainly still exist, and some vendors, like
> Microsoft, continue to use a single compiler that compiles both
> languages. But GNU, and Watcom have clearly taken the two compiler
> approach (but in different ways; GNU uses a common back end, at least
> for now.)


There's a section in your site in which you do compare the assembler
generated by the watcom compiler, in contrast with one done by hand by you.
Over the first assembler listing I can read:

"WATCOM C/C++ v10.0a output"

so one has the impression they give identical output... but why have you
used the C one anyway ?



 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the difference between void proba(); and void proba(void); ??? PencoOdStip@gmail.com C++ 1 05-23-2007 07:12 PM
Are implicit casts to void*& illegal? Ray Gardener C++ 3 04-19-2006 08:55 AM
what is the difference, void func(void) and void fucn() noblesantosh@yahoo.com C Programming 5 07-22-2005 04:38 PM
"void Method()" vs "void Method(void)" Ollej Reemt C++ 7 04-22-2005 03:47 AM
`void **' revisited: void *pop(void **root) Stig Brautaset C Programming 15 10-28-2003 09:03 AM



Advertisments