0

I'm trying to learn about malloc in C. I've done some java programming but want to learn the nuts and bolts of coding.

Let me explain what I know about this and hopefully someone can fill in the gaps.

In the line:

str = (char *) malloc(15);

I'm aware that what we're doing here is allocating memory to this string. 15 units of memory, I assume that the 15 units are 15 bytes, as 1 char takes up one byte (Is that right?)

Whats confusing me is the (char *) what does this mean? I know that * is the pointer and de-reference operator, but I'm lost as to what it means in this context.

From my limited experience in programming, my first guess is that (char *) is an explicit cast.. but I'm not sure exactly.

I apologise if this has been asked before, but I have searched, and I have also googled (char *) and variations like (int *) which I've seen in other examples, but can't seem to get an explanation.

Any help most appreciated.

C_Student
  • 13
  • 2
  • Do read [the link by shuttle87](http://stackoverflow.com/a/605858/296460) which addresses the benefits and drawbacks of the cast. – minghua Aug 16 '14 at 04:31
  • Check this http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Sagar Sakre Aug 16 '14 at 13:22

3 Answers3

7

That's a cast, converting the output of malloc to a char *.

It is not necessary or desirable in C to cast the return from malloc.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • where would it be necessary? C++? I keep seeing that cast on malloc tutorials for the C language? – C_Student Aug 16 '14 at 03:45
  • There are a lot of people in this world who for some reason think C and C++ are interchangeable, and then back-port stuff like this to a place where it doesn't belong. – Alex Celeste Aug 16 '14 at 03:46
  • 1
    +1 for addressing that casting the return from malloc in unnecessary (and dangerous if you don't include stdlib.h) – Don Shankin Aug 16 '14 at 03:48
  • @DonShankin: In C99+ that would be an error, but agreed, no one who knows C well is casting the return value of `malloc`. – Ed S. Aug 16 '14 at 03:55
  • 1
    @C_Student: Tutorials that cast the result of `malloc` are typically very old tutorials. The quality of such tutorials is no longer adequate. They are obsolete and should be avoided whenever possible. – AnT stands with Russia Aug 16 '14 at 04:29
  • @C_Student you don't cast the result of malloc in C but you need the cast in C++ http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – phuclv Aug 16 '14 at 06:13
  • ...then again, in C++, you should be using `new` which is automatically type-safe. – nneonneo Aug 16 '14 at 06:14
2

Malloc just attempts to allocates you a certain amount of memory in bytes and returns you a pointer to the start of that block of memory if it succeeds and NULL if it fails. In C a char is always one byte unlike what you would have seen in Java. The type that is returned from malloc is void* (ie. no type). The cast to char* is just making the type match what you want but is redundant as the type will automatically be converted for you. In fact you probably don't want to make an explicit cast in C, see this answer for more about that: https://stackoverflow.com/a/605858/296460

Note that unlike in C, in C++ void* is not compatible with other pointer types so you will get an error if you don't explicitly cast. This is an example of a situation where C and C++ are not compatible languages. The code example in the question seems to be taking a convention from C++ then using that in C which is something which is good to avoid.

Community
  • 1
  • 1
shuttle87
  • 15,466
  • 11
  • 77
  • 106
  • 2
    The phrase "implicit cast" is contradictory. A *cast* is an operator consisting of a type name in parentheses; it's always explicit. A cast operator specifies a *conversion*. A conversion may be either explicit (specified with a cast) or implicit. – Keith Thompson Aug 16 '14 at 04:05
  • @KeithThompson, sloppy choice of language on my part edited that. – shuttle87 Aug 16 '14 at 04:08
0

In C, you don't need to cast the return value of malloc. The pointer to void returned by malloc is automagically converted to the correct type.

str = (char *) malloc(15);

it's just casting and for C it's not necessary. But people do cast, because if you want your code to compile with a C++ compiler.

Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73