Conflicting types for “free”

☆樱花仙子☆ 提交于 2019-12-06 04:37:27

问题


I'm getting the error

Conflicting types for 'free'

on the call to 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;
}

Can't figure out why. Not sure if this matter, but what's above it is

#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 what's below it is a bunch of function implementations.


回答1:


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

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




回答2:


You could implement your malloc and free above some operating system memory address space primitives (modifying the virtual memory of your process), like (on Linux) mmap(2) and munmap. Details are operating system specific.

BTW, if your goal is to write a program using only <stdio.h> most implementations of it are internally using malloc, since the buffer inside every FILE is generally some dynamically allocated byte zone (so concretely, it is generally allocated thru malloc). In other words, the implementation of fopen very probably uses malloc; see also this. Hence if you accept to include <stdio.h> you should accept to include <stdlib.h>...

Notice that several standard C libraries (a.k.a. libc) are free software; you could study -and improve- the source code of GNU glibc or of musl-libc.

See also this answer to a related question.




回答3:


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

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

BTW: the code is calling 'free()' How was that memory allocation made that 'free()' will be returning to the heap?



来源:https://stackoverflow.com/questions/30004003/conflicting-types-for-free

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!