dynamic memory allocation question
Mark Mara
Posts: 64
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?
But I get this
Thanks for your help.
--markM
#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
As per http://www.cplusplus.com/reference/cstdlib/malloc/, I would try adding to your program.
http://man7.org/linux/man-pages/man3/malloc.3.html
Also void* is probably not the type you want to use.
So, your example should be although a more useful and more typical example would be for example 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