Thread: implicit declaration of malloc

  1. #1
    cheezwhiz
    Guest

    Question implicit declaration of malloc

    Hi, I am learning C and the book made me type in the included program. Its teh part about using malloc/free functions.

    It compiles fine & works but my compiler gives me the following warnings:

    1 - implicit declaration of malloc
    2 - assignment makes pointer from integer

    Above are both about the line which has malloc in it and the last warning is:

    3: implict declaration of function free

    What exactly is a implicit declaration ? I know malloc returns an adres to allocated memory but why is it implicit ?

    The program :

    #include <stdio.h>

    int main()
    {
    int index, aant;
    float *vec, som;

    // invoer arraylengte
    printf("Aantal:\n"); scanf("%d",&aant);

    // geheugen reserveren
    vec = malloc(aant * sizeof(float));
    if (!vec)
    {
    printf("geen geheugen!");
    return 1;
    }

    // gegevens invoeren
    for(index = 0; index < aant; index++)
    {
    printf("floatgetal:\n");
    scanf("%g",&vec[index]);
    }

    // som berekenen
    for(index = 0, som = 0; index < aant; index++)
    som = som + vec[index];

    // resultaat weergeven
    printf("som: %g\n",som);

    // geheugen vrijgeven
    free(vec);

    return 0;
    }

  2. #2
    Unregistered
    Guest
    add this line at the top and you'll be sorted
    #include <stdlib.h>

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    1. #include <stdlib.h>

    2a. Don't cast malloc - see
    http://www.eskimo.com/~scs/C-faq/q7.7.html

    2b. Casting would have suppressed this warning
    > 2 - assignment makes pointer from integer

  4. #4
    Unregistered
    Guest
    Thanks vVv, I already tried a float cast but didnt use the *.

    Unregistered, thank you too, it got rid of the implicit warnings, and now that I seen this, I also remembered the chapter about functions & I understand (its because of the mssing functionprototype right ?)

    Still why is it compiling succesfull although I missed the include of stdlib.h ?

  5. #5
    Unregistered
    Guest
    ... stdlib.h include does it all ? why no cast ?
    I do not understand the explanation on that url exactly Salem.
    What is ment with a generic pointer ? Does this mean that certain functions return a generic pointer which can be appointed to whatever type of pointer ?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Still why is it compiling succesfull although I missed the include of stdlib.h ?
    It was giving you warnings - for all the compiler knows, malloc returns int, and all would be well. Except it doesn't, and you should know that such a warning is a bit more serious than that.

    And yes, stdlib.h contains the function prototype for malloc.

    Any compiler warning should really be fixed IMO

    > What is ment with a generic pointer ?
    void* is the generic pointer type (in C), and since malloc returns void*, you can assign the result of malloc to any pointer variable without any casting.

  7. #7
    Unregistered
    Guest

    Smile

    I see, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM