basically the program is manage storage place in memory. each chunk has header including all the details about the chunk including size, next chunk. I create struct header to store all these data and put the address of header into the array that simulate the memory which is myalloc.memory. after I initialize the memory, I tried to allocate some space, but the address of header I stored in the memory somehow changed.
this is the code in the intializer:
…
void initialize_allocator(int _size, enum allocation_algorithm _aalgorithm) {
assert(_size > 0);
myalloc.aalgorithm = _aalgorithm;
if (_size % 64 != 0) {
_size = ((_size / 64) + 1) * 64;
}
myalloc.size = _size;
myalloc.memory = malloc(myalloc.size);
char* ptr = myalloc.memory;
memset(myalloc.memory, 0, myalloc.size);
header* h = (header*)malloc(sizeof(header));
ht = h;
h->size = sizeof(header*);
h->free = true;
h->free_space = _size - sizeof(header*);
h->next = NULL;
memcpy(ptr,h,sizeof(header*));
memcpy(h,myalloc.memory,sizeof(header*));//testing address
printf("%d,%d, %p, %p",h->size, h->free_space, h, ht);
// Add some other initialization
}
and the part in allocate is:
void* allocate(int _size) {
printf("!");
char* current_ptr = myalloc.memory;
char* next_ptr = myalloc.memory;
char* end_ptr = myalloc.memory + myalloc.size;
header* curr = (char*)malloc(sizeof(header*));
memcpy(curr,current_ptr,sizeof(header*));
printf("%d,%d,%pn",curr->size, curr->free_space,curr);//test adress again
if (myalloc.aalgorithm == FIRST_FIT) {
// First Fit Allocation
while (current_ptr < end_ptr && (end_ptr - current_ptr) >= (_size + sizeof(header*))) {
//current pointer and its free space have enough space
printf("#");
memcpy(curr,current_ptr,sizeof(header*));
printf("%d,%d,%p, %p",curr->size, curr->free_space,curr, ht);
if( curr->free_space - curr->size >= _size && curr->next == NULL){
printf("1");
curr->size = _size + sizeof(header*);
curr->free = false;
header* temp = (header*)malloc(sizeof(header));//create last header
next_ptr = current_ptr + _size + sizeof(header*);
memcpy(next_ptr,&temp,sizeof(header*));//initialize
temp->free = true;
temp->size = 0;
temp->next = NULL;
temp->free_space = end_ptr - current_ptr;
curr->next = next_ptr;
return current_ptr + sizeof(header*);
}else if{
…code and output
and these are the structs:
typedef struct block {
int size;
bool free;
int free_space;
char *next;
} header;
struct Myalloc {
enum allocation_algorithm aalgorithm;
int size;
void* memory;
// Some other data members you want,
// such as lists to record allocated/free memory
};
I run the initialize function first and then use allocate function, but the address changed so I can’t get the header info. and this is part of the main:
initialize_allocator(100, FIRST_FIT);
printf("Using first fit algorithm on memory size 100n");
printf("9");
int* p[50] = {NULL};
for(int i=0; i<10; ++i) {
p[i] = allocate(sizeof(int));
if(p[i] == NULL) {...
I want to know how to solve the address change issue. since I can’t push forward, there are memory leak happens.valgrind
qiu shi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.