I am trying to create a free list to implement a heap allocator. Here is how I defined the struct so far:
typedef struct headerT headerT;
struct headerT{
size_t payloadSize;
struct headerT* next; // unused field to make struct 8 bytes, simplifies alignment
};
headerT* freeList;
and here is how I initialize my heap:
int myinit()
{
freeList = InitHeapSegment(0); // InitHeapSegment resets heap segment to empty, no pages
//allocated
freeList->payloadSize = 0;
freeList->next=NULL;
return 0;
}
I get a segfault immediately when the function tries to change the payload size to zero. What am I missing here?
The problem is here:
You say that
InitHeapSegment“resets” heap segment to empty, but no pages are allocated. Probably its notmalloc()ing. HencefreeListcould point to a junk value which you do not have the permission to access, giving you a SIGSEGV or SEGMENTATION VIOLATION error. In Windows, this error is of type Cx00000005