Conflicting types for "free"

I get an error

Conflicting types for 'free'

to call the free() function below.

 int main ( ) { char fx [] = "x^2+5*x-1"; node * fxNode = buildTree(fx, sizeof(fx)/sizeof(char)); printf(deriveFromTree(fxNode)); // Should print "2*x+5" free(fxNode); return 0; } 

I can’t understand why. Not sure if this is the case, but above it

 #include <stdio.h> char opstack [5] = {'+','-','*','^', '\0'}; unsigned short int lowerOpPrecedence ( char, char, char * ); int stringToUnsignedInt ( char *, unsigned int * ); int stringToDouble ( char * , double * ); unsigned short int stringCompare ( char * , char * ); void stringCopy ( char * , char * ); typedef struct treeNode { char * fx; char * op; struct treeNode * gx; struct treeNode * hx; } node; unsigned short int getNodeState ( node * ); node * buildTree ( char *, int ); char * basicDerivative ( char * ); char * derivateFromTree ( node * ); 

and below it is a bunch of function implementations.

+3
source share
3 answers

You need to add #include <stdlib.h> to provide a prototype for free() .

In addition, the recommended signature for main() is int main (void) .

+7
source

You can implement your malloc and free above some operating systems the address space of primitives (changing the virtual memory of your process ), for example (on Linux) mmap (2) and munmap . Details relate to a specific operating system.

By the way, if your goal is to write a program using only <stdio.h> , most of its implementations internally use malloc , since the buffer inside each FILE usually represents some dynamically allocated byte zone (so specifically it is usually allocated through malloc ) In other words, the fopen implementation very likely uses malloc ; see also . Therefore, if you accept to include <stdio.h> , you must accept to include <stdlib.h> ...

Please note that several standard C libraries (aka libc ) are free software ; You can study and improve the source code of GNU glibc or musl-libc .

See also this answer on the relevant question.

+1
source

if your linker batch file contains a specific heap definition, including a label at the start address and a length label,

then you can write your own version of malloc, free, realloc, calloc, etc.

BTW: the code calls "free" () "How was the memory allocation allocated so that" free () "will be returned to the heap?

0
source

All Articles