Shop OBEX P1 Docs P2 Docs Learn Events
dynamic memory allocation question — Parallax Forums

dynamic memory allocation question

Mark MaraMark Mara Posts: 64
edited 2014-08-21 19:27 in Propeller 1
This is probably a newbie c question, but I am using propgcc. How should the arguments to malloc and free be defined as to not generate warning messages from the compiler? It seemed that this should work?
#include <propeller.h>


int main(void)
{
    void        *memptr;
    memptr = (void *) malloc(10);
    free(memptr);
    return 0;
}

But I get this
Project Directory: /Users/mam1/Git/play/


propeller-elf-gcc -v GCC 4.6.1 (propellergcc_v1_0_0_2224)
propeller-elf-gcc -I . -L . -o cmm/play.elf -Os -mcmm -Wall -m32bit-doubles -fno-exceptions -std=c99 play.c
play.c: In function 'main':
play.c:10:5: warning: implicit declaration of function 'malloc' [-Wimplicit-function-declaration]
play.c:10:23: warning: incompatible implicit declaration of built-in function 'malloc' [enabled by default]
play.c:11:5: warning: implicit declaration of function 'free' [-Wimplicit-function-declaration]
play.c:11:5: warning: incompatible implicit declaration of built-in function 'free' [enabled by default]
propeller-load -s cmm/play.elf
propeller-elf-objdump -h cmm/play.elf
Done. Build Succeeded!


propeller-load -S1 -Dreset=dtr -I /opt/parallax/propeller-load/ cmm/play.elf -r -p /dev/cu.usbserial-004213FAPropeller Version 1 on /dev/cu.usbserial-004213FA
Loading cmm/play.elf to hub memory
2648 bytes sent


Verifying RAM ... 
OK

Thanks for your help.

--markM

Comments

  • DavidZemonDavidZemon Posts: 2,973
    edited 2014-08-21 06:48
    I believe
    implicit declaration of function
    
    is the warning that says "you haven't included a header file!!!"

    As per http://www.cplusplus.com/reference/cstdlib/malloc/, I would try adding
    #include <stdio.h>
    
    to your program.
  • jazzedjazzed Posts: 11,803
    edited 2014-08-21 07:02
    #include <stdlib.h>
    


    http://man7.org/linux/man-pages/man3/malloc.3.html

    Also void* is probably not the type you want to use.
  • Mark MaraMark Mara Posts: 64
    edited 2014-08-21 07:03
    Thanks for the suggestion, but adding an include for stdio.h did not change the warning messages.
  • DavidZemonDavidZemon Posts: 2,973
    edited 2014-08-21 07:16
    Ah yea... stdlib, not io lol
  • Mark MaraMark Mara Posts: 64
    edited 2014-08-21 07:58
    It was stdlib.h that I needed. Thanks
  • TorTor Posts: 2,010
    edited 2014-08-21 19:27
    And one more thing - don't cast malloc(), if you need to cast (because you see warnings) it's telling you that the prototype is missing, i.e. missing <stdlib.h>.
    So, your example
       void  *memptr;
       memptr = (void *) malloc(10); 
    
    should be
       void   *memptr;
       memptr = malloc(10);
    
    although a more useful and more typical example would be for example
       char *a;
       struct mystruct *rec;
       a = malloc (10);
       rec = malloc (10 * sizeof(*rec)); /* for example. Some would use sizeof (struct mystruct) */
    
    In old K&R C, before the 'void' type was invented, 'malloc' was defined as returning a char pointer, and one would always have to cast malloc. In ANSI C 'malloc' returns a void pointer, it maps to anything and no cast is needed or wanted (and can even create subtle bugs if <stdlib.h> is missing)

    -Tor
Sign In or Register to comment.