malloc and free

  • Thread starter Joris Adriaenssens
  • Start date
R

RCollins

Keith said:
No, it's an implicit conversion. There's no such thing as an implicit
cast.

A conversion is an action that occurs during program execution. It
can be either explicit (i.e., specified by a cast operator in the
program source) or implicit (i.e., not specified by a cast operator).

A cast is a source construct, consisting of a parenthesized type-name
followed by an expression. It specifies an explicit conversion.

I admit it's a bit confusing. For most other constructs, we use the
same name for the syntactic construct as for the action that occurs
during execution. For example, we commonly refer to both the source
construct "foo(42)" and to the action that it specifies as a "function
call", and there can be implicit function calls that don't correspond
to any source-code function call. For casts and conversions, we use
two different terms.

It is confusing. To my mind, it is not a feature of the C language,
but more of a convention of how we use the English language.
 
R

RCollins

Jack said:
You are entitled to your opinion, however incorrect it is. The C
language is defined by an ANSI/ISO/IEC standard, however, not by your
opinions, even if they happen to be correct. Which they are not, in
this case.

A cast is something generated by a cast operator. Anything else is
not a cast.

========
6.5.4 Cast operators

Syntax
1 cast-expression:
unary-expression
( type-name ) cast-expression

Constraints

2 Unless the type name specifies a void type, the type name shall
specify qualified or unqualified scalar type and the operand shall
have scalar type.

3 Conversions that involve pointers, other than where permitted by the
constraints of 6.5.16.1, shall be specified by means of an explicit
cast.

Semantics

4 Preceding an expression by a parenthesized type name converts the
value of the expression to the named type. This construction is called
a cast. A cast that specifies no conversion has no effect on the type
or value of an expression.
========

Note specifically the first two sentences of paragraph 4. They are
quite clear. The only thing that the standard defines as being called
a cast is exactly defined in the first one.

So until and unless you can quote chapter and verse from the C
standard that anything else is called a cast, with or without the use
of the adjective 'implicit', STFU.

I'm sorry, but your point is not clear. The paragraphs you quoted
above do, indeed, give us a definition of what a cast is and how to
use it.

But I see no restriction denying the compiler the ability to perform
a cast wherever it might be necessary.

If a compiler converts one data type to another (for example, converting
a "void *" to a "struct foo *"), I would still call it an (implicit)
cast.
 
R

RCollins

Eric said:
Balderdash.

int main(void) {
return 0;
}

How many "implicit unary + operators" does this program use?

How many are needed?

On the other hand ...

....
struct foo *bar = malloc(100);
....

has at least one implicit cast. You may call it a "automatic
conversion", but if it looks like a cast, smells like a cast, and
walks like a cast .... it might just be a cast.
 
C

Chris Torek

On the other hand ...
struct foo *bar = malloc(100);
has at least one implicit cast. You may call it a "automatic
conversion", but if it looks like a cast, smells like a cast, and
walks like a cast .... it might just be a cast.

But it does not "look like" a cast, at least, no more than the
Eiffel Tower looks like my grandmother. A cast consists of a type
name stuck in between parentheses. Where is the parenthesized type
name?

Now, *this*, on the other hand:

*bar = (const struct foo) { 0, 5.5, "three" };

*does* look like a it contains cast -- but still does not have one.
The newfangled C99 "compound literals" use the same syntactic
construct -- a type-name in parentheses -- but put it in front of
a brace-enclosed list of values.
 
G

Gordon Burditt

struct foo *bar = malloc(100);
...

has at least one implicit cast.

An "implicit cast" is like "accidental murder". If it's accidental,
it's not murder. If it's implicit, it's not a cast.

Gordon L. Burditt
 
M

Michael Wojcik

Casts (use of the parentheses notation) are explicit. Therefore the same
operation without such notation can be called an "implicit cast". That's how
language works.

It can be called a "lavender rhinoceros", too. The question is not
whether it can be done, but what we gain by doing so.

In C, a "cast" is by definition an explicit operation. The phrase
"implicit cast" is an oxymoron. That doesn't mean it's meaningless;
it means it's infelicitous. We could call the integer promotions and
array-to-pointer decay "automatic casts". We could call the use of
type suffixes (such as eg "U") on numeric constants "numeric constant
suffix casts". We don't, because we prefer to use "cast" in the
specific sense employed by the standard, so we may speak precisely
and be clearly understood.

--
Michael Wojcik (e-mail address removed)

You brung in them two expert birdwatchers ... sayin' it was to keep us from
makin' dern fools of ourselfs ... whereas it's the inherent right of all to
make dern fools of theirselfs ... it ain't a right held by you official types
alone. -- Walt Kelly
 
M

Malcolm

Gordon Burditt said:
An "implicit cast" is like "accidental murder". If it's accidental,
it's not murder. If it's implicit, it's not a cast.
Most English speakers would accept the sentence "Manslaughter is the legal
term for murder under gross provocation, or accidental murder".
 
R

RCollins

Gordon said:
An "implicit cast" is like "accidental murder". If it's accidental,
it's not murder. If it's implicit, it's not a cast.

Gordon L. Burditt

Er ... no. If someone said
"the compiler will implicitly cast type A to type B"
then I would know exactly what they were talking about.

From this, I would say that an "automatic conversion" may be
called an "implicit cast"
 
J

Joe Wright

RCollins said:
Er ... no. If someone said
"the compiler will implicitly cast type A to type B"
then I would know exactly what they were talking about.

From this, I would say that an "automatic conversion" may be
called an "implicit cast"

Please pay attention. This is an English language, not a C language
issue. The "implicit cast" is a straw man. It doesn't exist. We are
talking about values converted from one type to another. This
conversion may be either implicit (implied by language rules) or
explicit, forced with a cast.
 
R

Richard Bos

RCollins said:
Er ... no. If someone said
"the compiler will implicitly cast type A to type B"
then I would know exactly what they were talking about.

From this, I would say that an "automatic conversion" may be
called an "implicit cast"

Ah - you probably also call a bend in the road an "implicit left turn
sign".

Richard
 
R

RCollins

Joe said:
Please pay attention. This is an English language, not a C language
issue. The "implicit cast" is a straw man. It doesn't exist. We are
talking about values converted from one type to another. This conversion
may be either implicit (implied by language rules) or explicit, forced
with a cast.

No, YOU pay attention. I am trying to find a definite difference
between "automatic conversion" and "implicit cast" beyond a *convention*
of English language.

If no such difference exists, then I fail to see why people insist that
there *is* a difference.
 
R

RCollins

Richard said:
Ah - you probably also call a bend in the road an "implicit left turn
sign".

Richard

Indeed ... if the car *automatically* turned to follow the road (say,
because of radar/computer control), then one might easily assume
an "implicit" turn sign existed in the controlling mechanism.
 
J

Joe Wright

RCollins said:
No, YOU pay attention. I am trying to find a definite difference
between "automatic conversion" and "implicit cast" beyond a *convention*
of English language.

If no such difference exists, then I fail to see why people insist that
there *is* a difference.

Again, there is no such thing as "implicit cast". A conversion may
be implicit or explicit. If it is explicit it is called a cast.
 
R

RCollins

Joe said:
Again, there is no such thing as "implicit cast". A conversion may be
implicit or explicit. If it is explicit it is called a cast.

So you are saying that the only difference is a convention of the
English language?

In the C89 standard there is a definition of "cast" and how it is used
by the programmer (posted up-thread by another reader). However, there
is no restriction in the standard saying the *compiler* cannot perform
a cast. I would call this type of conversion an implicit cast.
 
P

pete

RCollins wrote:
In the C89 standard there is a definition of "cast" and how it is used
by the programmer (posted up-thread by another reader).
However, there is no restriction in the standard saying
the *compiler* cannot perform a cast.

It looks like you didn't read what the standard says.
It also looks like you're determined not to.
A cast is something in source code.
The standard tells you what "explicit conversion" means.
Why don't you learn?
 
P

pete

pete said:
It looks like you didn't read what the standard says.
It also looks like you're determined not to.
A cast is something in source code.
The standard tells you what "explicit conversion" means.

and "implicit conversion" too.
 
F

Flash Gordon

So you are saying that the only difference is a convention of the
English language?

In the C89 standard there is a definition of "cast" and how it is used
by the programmer (posted up-thread by another reader). However,
there is no restriction in the standard saying the *compiler* cannot
perform a cast. I would call this type of conversion an implicit
cast.

Only in the same sense that there is no restriction on the compiler
putting in a printf call. A cast is a defined as an explicit operation
specified in the source code. The standard also defines when types get
promoted and when they get implicitly converted. It does not leave in
scope for the compiler to convert data at other times just because the
implementor wanted to.

Why can you not accept that certain terms have been defined by the
standard and use the terms that are defined? Why do you seem to insist
on inventing your own terminology instead?
 
R

Richard Bos

[ For Offler's sake, guys, learn to snip! ]
So you are saying that the only difference is a convention of the
English language?

In the C89 standard there is a definition of "cast" and how it is used
by the programmer (posted up-thread by another reader). However, there
is no restriction in the standard saying the *compiler* cannot perform
a cast.

Yes, there is. A cast is _not_ a conversion; a cast is an _operator_,
which happens to force a conversion. That is, a cast is part of your
source code. It consists of characters in a C source file, not of some
mythical vibration in the aether which somehow converts values. Since
_you_ write your source code, and the implementation reads and
interprets this source code you wrote but does _not_ modify it (it had
better not!), you are the only one who can put a cast in your source
code, and it is, by necessity, an explicit cast.

Richard
 
R

RCollins

Flash said:
Only in the same sense that there is no restriction on the compiler
putting in a printf call. A cast is a defined as an explicit operation
specified in the source code. The standard also defines when types get
promoted and when they get implicitly converted. It does not leave in
scope for the compiler to convert data at other times just because the
implementor wanted to.

Why can you not accept that certain terms have been defined by the
standard and use the terms that are defined? Why do you seem to insist
on inventing your own terminology instead?

I can find in the standard a definition of "cast" -- I read it to mean
a conversion of one type to another type. But I don't find a
restriction saying *only* the programmer can do this (using only this
term).

But I may be blinded by my own expectations (a common failing among
a lot of people).

Can someone give me the section of the standard that specifically says
an (implicit) type conversion should not be called a "cast" ?
 
S

Stephen Sprunk

RCollins said:
So you are saying that the only difference is a convention of the
English language?

In the C89 standard there is a definition of "cast" and how it is used
by the programmer (posted up-thread by another reader). However, there
is no restriction in the standard saying the *compiler* cannot perform
a cast. I would call this type of conversion an implicit cast.

You can call it that, but that term is not defined by the standard. A cast,
by definition, cannot be implicit.

An "implicit conversion" is done automatically when the compiler finds
differing but compatible types.

An "explicit conversion" is done when the programmer explicitly uses a cast,
typically when the types are not directly compatible.

S
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

a fast malloc/free implementation & benchmarks 0
Alternative to Malloc in C 0
malloc and maximum size 56
malloc and free 11
linked lists and free() 23
malloc 40
Malloc Query 86
malloc() 6

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top