954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Creating Header files

//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

aplh_ucsc
Light Poster
40 posts since Nov 2010
Reputation Points: 11
Solved Threads: 0
 

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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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
aplh_ucsc
Light Poster
40 posts since Nov 2010
Reputation Points: 11
Solved Threads: 0
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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?

aplh_ucsc
Light Poster
40 posts since Nov 2010
Reputation Points: 11
Solved Threads: 0
 

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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

I got the object file using

gcc -c myMalloc.c

So how can i link it with the header?

aplh_ucsc
Light Poster
40 posts since Nov 2010
Reputation Points: 11
Solved Threads: 0
 
gcc myMalloc.o check.c
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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

aplh_ucsc
Light Poster
40 posts since Nov 2010
Reputation Points: 11
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You