//myMalloc.c
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>

void *newMalloc(size_t vol);
void newFree(void *adPtr);
void save();
void search();
void *findSpace();
void freelist();
static int buf[12500];

void *newMalloc(size_t vol){
// some code
}

void save(){
// some code
}

void search(){
// some code
}

void newFree(void *adPtr){
// some code
}

void *findSpace(){
// some code
}

void freelist(){
// some code
}

How should I begin to write my "myMalloc.h" file

vedro-compota commented: ++++++++++ +1

Recommended Answers

All 8 Replies

Take all of the declarations and put them in a header file:

void *newMalloc(size_t vol);
void newFree(void *adPtr);
void save();
void search();
void *findSpace();
void freelist();

Add any includes that are required by the declarations:

#include <stddef.h> // For size_t 

void *newMalloc(size_t vol);
void newFree(void *adPtr);
void save();
void search();
void *findSpace();
void freelist();

Finally, add inclusion guards:

#ifndef MYMALLOC_H
#define MYMALLOC_H

#include <stddef.h> // For size_t 

void *newMalloc(size_t vol);
void newFree(void *adPtr);
void save();
void search();
void *findSpace();
void freelist();

#endif

Bam! Instant header. :) One thing to take note of is that I used stddef.h for the declaration of size_t rather than stdio.h or stdlib.h (it's in all three). The reason is because you should avoid unnecessary includes, and all of the junk in stdio.h and stdlib.h counts as unnecessary when stddef.h is quite a bit smaller.

i checked a simple programme with this header. it is,

//check.c
#include<stdio.h>
#include<myMalloc.h>


int main(){

int *x=newMalloc(10);
*x=*x+10;
printf("%d \n",*x);
newFree(x);

}

but it gives me an error says,

check.c:2:21: error: myMalloc.h: No such file or directory

Try #include "myMalloc.h" . Angle brackets should typically be reserved for headers proved by your compiler.

Wow thankx :)
but still gave me an error. it says

/tmp/ccUN4ZuK.o: In function `main':
check.c:(.text+0x11): undefined reference to `newMalloc'
check.c:(.text+0x47): undefined reference to `newFree'
collect2: ld returned 1 exit status

I think it is relevant to myMalloc.c file. Not with myMalloc.h file. is it?

Headers only provide declarations. You still need to compile myMalloc.c with the project using it, or link to a precompiled *.o object file to provide the stuff declared in your header.

I got the object file using

gcc -c myMalloc.c

So how can i link it with the header?

gcc myMalloc.o check.c

Wow.. Finally it gets work :) Thankx Naure for all ur helps throughout this assignment :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.