Skip to content

Conversation

yegorich
Copy link

The cast is needed when compiling this code with C++ compiler:
error: invalid conversion from 'void_' to 'char_' [-fpermissive]

Signed-off-by: Yegor Yefremov yegorslists@googlemail.com

The cast is needed on some systems to avoid following compile error:
error: invalid conversion from 'void*' to 'char*' [-fpermissive]

Signed-off-by: Yegor Yefremov <yegorslists@googlemail.com>
@falconindy
Copy link
Contributor

This is not "some systems" -- this is the result of trying to compile a C source file with a C++ compiler...

@yegorich
Copy link
Author

You're right.I'm developing an application with mixed C/C++ code and gather experience in solving such issues :-) Nevertheless I think the patch would make sense for devs like me, who copy&paste the code and then get strange compile errors.

I'll fix the commit log.

@ptor
Copy link

ptor commented Sep 17, 2013

  • chunk.memory = malloc(1); /* will be grown as needed by the realloc above */
  • chunk.memory = (char )malloc(1); / will be grown as needed by the realloc above */

[Edit: I don't know what's up with Github's comment system, but it doesn't seem possible to quote a diff. The first line is a minus, the second a plus.]

This patch is in my opinion just wrong. In ANSI C the void pointer was introduced just so that the old K&R practice of always having to cast (among others) malloc could be disposed of. Worse, casting malloc can in some cases hide errors (I have an example of that stuck away somewhere).
malloc(3) is defined as "void *malloc(size_t size);" in <stdlib.h> so that it will match any type, that's the whole point.

-Tor

@yegorich
Copy link
Author

@PureAbstract
Copy link

I'm inclined to agree with @ptor here - the cast is entirely unnecessary in C.

And whilst I can't conjure up a reasonable circumstance where it would cause any harm, it's excess code for the sake of excess code, and More Code Is Bad.

If you're copy&pasting this into C++, then static_cast<char *> is the right way to do this not (char *)malloc(...

C & C++ are different, albeit related, languages. C is not a proper subset of C++. C++ is not a proper superset of C.

@bagder
Copy link
Member

bagder commented Sep 18, 2013

I too vote for not using the cast. We're using C, we don't need it. It is an example, it is important to keep it simple and easy-to-read.

@yegorich
Copy link
Author

Then let's close this issue.

@yegorich yegorich closed this Sep 19, 2013
bagder added a commit that referenced this pull request Aug 24, 2015
bagder added a commit that referenced this pull request Oct 9, 2015
It makes it a clearer message for developers reaching that point without
the necessary support.

Thanks-by: Jay Satiro

Closes #78
jgsogo pushed a commit to jgsogo/curl that referenced this pull request Oct 19, 2015
jgsogo pushed a commit to jgsogo/curl that referenced this pull request Oct 19, 2015
It makes it a clearer message for developers reaching that point without
the necessary support.

Thanks-by: Jay Satiro

Closes curl#78
@lock lock bot locked as resolved and limited conversation to collaborators Jan 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants