4

C function malloc() is defined under stdlib.h.

It should give an error if we don't include this file, but this code works fine with a little warning.

My question is, if malloc() works without this header file, then why do we need to include it? Please help clarify my concepts.

# include <stdio.h>

int main()  
{
    int a, b, *p;
    p = (int*)malloc(sizeof(int)*5);
    for(a=0;a<5;a++)p[a]=a*9;
    for(b=0;b<5;b++)printf("%d ",p[b]); 
}
T J. Kim
  • 150
  • 1
  • 7
schrodinger
  • 247
  • 1
  • 2
  • 7
  • 11
    Don't cast the return value of malloc() - http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – gnud Jul 17 '11 at 10:42
  • 4
    @WTP: C++ and C99 explicitly specify that when the control is reached the end of main without any explicit return statement, it is equivalent to `return 0;` – Armen Tsirunyan Jul 17 '11 at 10:55
  • 2
    *"this code works fine with a little warning"* -- There is **no such thing!** – Cody Gray - on strike Jul 17 '11 at 11:02
  • 2
    You should really use `-Werror` when compiling you code. Then you "little warnings" are what they should be: errors which let the compilation fail. – ThiefMaster Jul 18 '11 at 07:29

5 Answers5

12

In C unfortunately you don't need pre-declaration for functions. If the compiler encounters with a new function it will create an implicit declaration for it ("mmm`kay, this how it is used so I will assume that the type of the arguments are..").

Do not rely on this "feature" and in general do not write code that compiles with warnings.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
  • 4
    C99 does not allow implicit function declarations – Christoph Jul 17 '11 at 15:19
  • The implicit declaration has nothing to do with an assumption about argument types, only about the return type. And this is `int`. Only prototypes in contrast to just a "plain" declaration help to handle argument types. – Jens Gustedt Jul 17 '11 at 15:21
  • @Christoph: While technically true, some compilers (GCC in particular) still allow implicit function declarations even when operating in C99 mode, for compatibility reasons. – Adam Rosenfield Jul 17 '11 at 20:03
  • 1
    this is a rare occasion when everybody is right. gcc allows this for backward compatibility ("don't break old code")` but you can enforce a compile time error with `-std=c99 -pedantic-errors` – Karoly Horvath Jul 18 '11 at 08:50
6

Read the warning. It says it's invalid. The compiler is simply too kind to you. In Clang this works, but it might not in other compilers.

At least include it to suppress the warning. Unnecessary warnings are annoying. Any program should compile with warnings treated as errors (I always enable that).

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
  • I always code in warning free manner. My question was why it's giving only warning and not error ? I was expecting an error. – schrodinger Jul 17 '11 at 10:42
  • I guess the compiler thinks: "this code is just written to simply try something out, I shouldn't fail because of this silly thing." The compiler knows which arguments that function provides because it's hard-coded into the compiler by the compiler makers. Calling functions in another library might give an error. –  Jul 17 '11 at 10:44
  • I assume because it's an external library it's up to the compiler how it deals with the issue, unlike a syntax error. If you want to make the warning into an error, compiler with -Werror (ideally gcc -Wall -Werror). – pwaring Jul 17 '11 at 10:45
  • 1
    @schrodinger: it's probably not an error for historical reasons (ie support for legacy code); you're free to use the `-Werror` flag to ensure warning-free compilation; it's probably also a good idea to increase the warning level, including at least the flags `-std=c99 -pedantic -Wall -Wextra` – Christoph Jul 17 '11 at 15:30
3

It appears that that's your compiler's magic. Not including the necessary headers may work on your compiler (which I suppose is by Microsoft), but it won't necessarily compile elsewhere(that includes future versions of the same compiler). Write standard-conforming, portable code.

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • Compiler details: tendua@Rhythm:~$ gcc --version gcc (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2 Copyright (C) 2010 Free Software Foundation, Inc. This is free software; see the source for copying conditions. – schrodinger Jul 17 '11 at 10:38
  • @schrodinger: Well, I just ***supposed*** it was by Microsoft.:) – Armen Tsirunyan Jul 17 '11 at 10:40
  • The compiler isn't necessarily Microsoft, gcc will also let you get away without stdlib.h, though it does produce a warning, even if -Wall isn't specified. I agree that you should write portable code though, and including stdlib.h is part of that. – pwaring Jul 17 '11 at 10:40
3

stdlib.h is of the general purpose standard header which includes functions of Dynamic Memory allocation and other Standard Functions.

For example if you want to display a message at the end of the execution of your program you will need to go for the getch() function,this functions reads a character from keyboard thus giving user the time to read the displayed Information.

The getch() function requires the stdlib header to be Included.

techno
  • 6,100
  • 16
  • 86
  • 192
2

Like many things in c the reason that an error isn't generated when there is no prototype is for historical reasons. In the early days people often didn't bother prototyping functions because pointers and integers were usually the same size and integral types smaller than an integer were promoted to an integer when passed as a parameter (and floating point was rarely used for systems programming).

If at any point they had changed the compiler to give an error if a function was not prototyped then it would have broken many programs and would not have gained widespread acceptance.

With 64 bit addressing we are now entering a period when integers and pointers are not the same size and programs will most likely break if you do not prototype functions like malloc() that return a pointer.

In gcc always set the following options for your own programs: -Werror -Wstrict-prototypes

Dipstick
  • 9,854
  • 2
  • 30
  • 30