static functions are only visible in the file they are declared/defined in. The two morecore declaration and definitions are not the same function.
Printable View
static functions are only visible in the file they are declared/defined in. The two morecore declaration and definitions are not the same function.
Well, I'm assuming he's not compiling his .h file separately (although perhaps I shouldn't). I had tried it (as mentioned above) with the prototype in a #included header, and that works just fine (as it should).
Yes I am running the static function in the same file.
And , am I not right in this. As I have included the . h in the beginnning of my program; wont the compiler run the .h file before running the program file.
Hence I have not compiled my .h file seperately. Even if I did, it should not hurt. As the compiler will anyway do the same.
Does your prototype
occur before the definition of Header? (I can't see that resulting in your error, but still . . . .)Code:static Header *morecore(unsigned long nunits);
Could you post your complete code, .c and .h files?
Header file:
C File:Code:
#ifndef MYMALLOC_H
#define MYMALLOC_H
void *mymalloc(unsigned long nbytes);
static Header *morecore(unsigned long nunits);
void myfree(void *p);
#endif
Code:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "mymalloc.h"
#define MEM_SIZE 1024 * 1024
#define NALLOC 1024
typedef long Align; /* for alignment to long boundary */
union header{ /* block header */
struct{
unsigned long size; /* size of this block */
int prev_check;
union header *ptr; /* next block if on free list */
} s;
Align x; /* force alignment of blocks */
};
typedef union header Header;
static Header base; /* empty list to get started */
static Header *freeptr = NULL; /* start of free list */
/* malloc: general-purpose storage allocator */
void *mymalloc(unsigned long nbytes)
{
Header *p, *prevptr;
unsigned long nunits;
nunits = (nbytes+sizeof(Header)-1)/sizeof(Header) + 1;
if ((prevptr = freeptr) == NULL)
{ /* no free list yet */
base.s.ptr = (Header *)calloc(MEM_SIZE,sizeof(Header));
base.s.size = 0;
/* allocase a memory from OS and then manage it */
base.s.ptr->s.size = MEM_SIZE;
base.s.ptr->s.ptr = &base;
freeptr = prevptr = &base;
}
for (p = prevptr->s.ptr; ; prevptr = p, p = p->s.ptr)
{
if (p->s.size >= nunits)
{ /* big enough */
if (p->s.size == nunits) /* exactly */
prevptr->s.ptr = p->s.ptr;
else
{ /* allocate tail end */
p->s.size -= nunits;
p += p->s.size;
p->s.size = nunits;
}
freeptr = prevptr;
base.s.prev_check =0;
return (void *)(p+1);
}
if (p == freeptr) /* wrapped around free list */
if ((p = morecore(nunits)) == NULL)
return NULL; /* none left */
}
}
static Header *morecore(unsigned long nunits)
{
char *cp, *sbrk(int);
Header *up;
if (nunits < NALLOC)
nunits = NALLOC;
cp = sbrk(nunits * sizeof(Header));
if (cp == (char *) -1)
return NULL;
up = (Header *) cp;
up->s.size = nunits;
free ((void *)(up+1));
return freeptr;
}
/* free: put block ap in free list */
void myfree(void *ap)
{
Header *bp, *p;
bp = (Header *)ap - 1; /* point to block header */
printf("bp->s.size %d\n",bp->s.size);
for (p = freeptr; !(bp > p && bp < p->s.ptr); p = p->s.ptr)
if (p >= p->s.ptr && (bp > p || bp < p->s.ptr))
{
printf("in the corner\n");
break; /* freed block at start or end of arena */
}
bp->s.prev_check =1;
if (bp + bp->s.size == p->s.ptr)
{
/* join to upper nbr */
bp->s.size += p->s.ptr->s.size;
bp->s.ptr = p->s.ptr->s.ptr;
}
else
bp->s.ptr = p->s.ptr;
if (p + p->s.size == bp)
{ /* join to lower nbr */
p->s.size += bp->s.size;
p->s.ptr = bp->s.ptr;
}
else
p->s.ptr = bp;
freeptr = p;
}
Yes, you see, my guess was right. You can't use Header in the header file because it hasn't been defined yet.
Move this
into the header file, probably before your function prototypes.Code:typedef long Align; /* for alignment to long boundary */
union header{ /* block header */
struct{
unsigned long size; /* size of this block */
int prev_check;
union header *ptr; /* next block if on free list */
} s;
Align x; /* force alignment of blocks */
};
typedef union header Header;
Ok I wasnt aware of that. That has fixed the problem.
Now something I cant figure out.
But how am I getting undefined symbol main when the main function is not in this file?Code:Undefined first referenced
symbol in file
main /usr/lib/crt1.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
It looks like you invoked the linker and tried to create an executable.
When you're dealing with multiple source files, compile them with -c, and then link the object files together together without -c.
Code:$ gcc -c source1.c
$ gcc -c source2.c
$ gcc source1.o source2.o -o executable
$ ./executable
You can't get an executable out of a file that doesn't have a main. If this is just supposed to be a library, do "compile only" or something similar -- or add a "real" program to your project so that it will have something to do.
Thats a great sigh of relief. I didnt know about the -c functionality. I thought there was something wrong with my program.