2

While looking for informations about calloc, I founded in the source code:

char *malloc();

in the calloc function.

Does it cast the void *malloc(size_t) into a function pointer returning a char* ?

This syntaxe does not compile for me.

nsvir
  • 8,781
  • 10
  • 32
  • 47

2 Answers2

4

In the old days, before ANSI C provides void * as the generic pointer type, char * was used for this purpose.

The code is from the source of Version 7 Unix, it's released in 1979 (before ANSI C).

And this is the reason it's necessary to cast the return value of malloc() in the old pre-ANSI code.

Reference: C FAQ

Community
  • 1
  • 1
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 1
    I think it's DEC's version of Version 7 UNIX from the V7M at the start of the path. It suggests on the next line that you compare this to the 4.2 BSD equivalent. But either way, it's certainly really old. – Nigel Harper Feb 14 '14 at 01:27
  • Thanks! Is there as newest version ? I mean, readable, cause glibc/malloc.c is very hard to understand. – nsvir Feb 14 '14 at 01:28
  • @nsvir: There is no single current version of the implementation of the C standard library. The C standard only specifies the header names, function declarations, and so forth. There are a number of different implementations, of which glibc is probably the most popular. (Library code tends to be optimized for factors other than readability.) – Keith Thompson Feb 14 '14 at 01:34
3

That's from some very old source code. You're looking at code from UNIX Version 7, which was released in 1979. The C language has changed substantially since then.

It's not a cast; it's a function declaration. A cast consists of a parenthesized type name followed by an expression, such as (int)foo.

Furthermore, it's an old-style function declaration, a non-prototype that doesn't specify the type(s) of the parameter(s). (It's still valid syntax, though.)

It declares that malloc is a function returning a result of type char*. (It doesn't define the malloc function; that has to be done elsewhere.)

In modern C (since 1989), malloc return a result of type void* and has a single parameter of type size_t, so the declaration would be:

void *malloc(size_t);

but that declaration is provided by the standard <stdlib.h> header, so there's no need to provide the declaration yourself.

It's legal to have function declarations inside other functions, but it's seldom a particularly good idea.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • Thanks! Ok if char *malloc(); is a non-prototype for function declaration. Malloc has already been a char *malloc(size_t) even in 1979. Why a char *malloc() could compile without conflicts ? – nsvir Feb 14 '14 at 01:34
  • @nsvir: Back in 1979, `void` didn't exist; that code would have compiled without error *in the environment for which it was written*. As of 1989, the declaration is `void *malloc(size_t);`. A declaration that's inconsistent with that might still compile if the correct declaration isn't visible. Redefining standard library functions has undefined behavior -- but the code you're looking at was part of the standard library itself. – Keith Thompson Feb 14 '14 at 01:37