Do ( Casting ) of the return of Malloc(), Calloc() and Realloc() - C

Asked

Viewed 196 times

4

According to the discussion Do I cast the result of malloc?, in C is not recommended or correct to cast Malloc() return. This also applies to Calloc() and Realloc functions()?
Taking advantage, the correct is to speak Cast, Casting or both are correct?

2 answers

5

Yes, functions have the same questions, because in essence they return the same thing, only what is done internally is that different.

I don’t think language is our focus, but it always depends on the context to define which is the correct word, both can be used in the context of programming, each in a more specific context has a different grammatical meaning, but nothing that directly interferes with the concept of programming.

  • I suspected, but it’s always good to confirm. Thanks.

3


On the question of words cast and casting, C specification uses two words with different meanings.

A draft of the latest specification can be found in the link below.

http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf

In this document, in section 6.5.4 paragraph 5, the following is set out:

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.

Already in section 7.12.6.5, paragraph 2, it is said:

...they are equivalent to Calling the corresponding logb Function and casting the returned value to type int.

So I conclude that the type in parentheses that precedes an expression is called a cast and what it’s doing is a casting.

In other words, cast is this construction of language and casting is what she does.

  • 1

    Thank you, do not know how much it tormented me kkk. Thanks also for the Reference, certainly will be of great value.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.