Fired from Microsoft
Fired from Microsoft

Reputation: 89

Conflicting types for "free"

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.

Upvotes: 3

Views: 5613

Answers (3)

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.

Upvotes: 1

user3629249
user3629249

Reputation: 16540

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?

Upvotes: 0

Sourav Ghosh
Sourav Ghosh

Reputation: 134336

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

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

Upvotes: 10

Related Questions