Wrong pointer returned when creating a version of malloc in c
What is the reason for this error? I am creating a personal version of malloc and free for my C class and but encountering the following issue: I split a free spot into 2 nodes using the best fit method to insert a value into my linked list, however it's returning the incorrect value to the testing program. The value of currentBlock + sizeof(struct Block) is 6299672 but when I print it out from the testing program it prints 6300251. This leads to a seg fault due to the pointer being incorrect when I free it later in the program. Is this an issue from incorrect casting or am I missing something else?
Thank you! The relevant sections of my code are below:
Below is my my_malloc and my_free methods:
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <assert.h>
struct Block {
int occ; // whether block is occupied
int size; // size of block (including header)
struct Block *prev; // pointer to previous block
struct Block *next; // pointer to next block
};
static struct Block *head = NULL; // head of list
void *my_malloc(int size)
{
struct Block *currentBlock = head;
struct Block *bestFitBlock = NULL;
int bestFit = 0;
void *offset = sbrk(0);
void *ending = offset + size + sizeof(struct Block);
int success = brk(ending);
struct Block *newBlock = offset;
newBlock -> occ = 1;
newBlock -> size = size + sizeof(struct Block);
while(head != NULL && currentBlock != NULL){
//If the block you're currently looking at is free and either the same or larger size than the incoming block,
//then split the block and take up the block and have a free block
if(currentBlock -> occ == 0){
if(currentBlock -> size >= newBlock -> size){
//If the incoming block and the free block are the same size, just fill it in
if(currentBlock -> size == newBlock -> size){
currentBlock -> occ = 1;
int removeExtra = 0 - newBlock -> size;
sbrk(removeExtra);
return currentBlock + sizeof(struct Block);
}
//If they're bigger sizes, split it into 2 blocks and fill 1 in. Must also leave room for another block to the right
else if(currentBlock -> size >= newBlock -> size + sizeof(struct Block)){
//Set the integer to 1 to denote that there is a place for it to go if no blocks match size
bestFit = 1;
//Save the block in which it would fit best if no blocks match size
if(bestFitBlock == NULL){
bestFitBlock = currentBlock;
}
else if(currentBlock -> size > bestFitBlock -> size){
bestFitBlock = currentBlock;
}
}
}
}
//Go to the next block
currentBlock = currentBlock -> next;
}
//Split the block into a taken and free slot
if(bestFit == 1){
currentBlock = bestFitBlock;
int freeSize = currentBlock -> size - newBlock -> size;
currentBlock -> occ = 1;
currentBlock -> size = newBlock -> size;
void *tempPointer = (int)currentBlock + (int)currentBlock -> size;
struct Block *newFreeBlock = tempPointer;
newFreeBlock -> occ = 0;
newFreeBlock -> size = freeSize;
newFreeBlock -> next = currentBlock -> next;
newFreeBlock -> prev = currentBlock;
newFreeBlock -> next -> prev = newFreeBlock;
currentBlock -> next = newFreeBlock;
//TROUBLESHOOTING: PRINT WHAT IS SUPPOSED TO BE RETURNED
printf("%d ", (int)currentBlock + (int)sizeof(struct Block));
int removeExtra = 0 - newBlock -> size;
sbrk(removeExtra);
return (int)currentBlock + (int)sizeof(struct Block);
}
if(head == NULL){
head = newBlock;
}
else{
currentBlock = head;
while(currentBlock -> next != NULL){
currentBlock = currentBlock -> next;
}
currentBlock -> next = newBlock;
newBlock -> prev = currentBlock;
}
if(success == 0){
return offset + sizeof(struct Block);
}
else{
printf("Error - Malloc not completedn");
return (int *)-1;
}
}
void my_free(void *data)
{
struct Block *currentBlock = head;
void *lookingFor = data - sizeof(struct Block);
while(lookingFor != currentBlock){
currentBlock = currentBlock -> next;
}
currentBlock -> occ = 0;
//If the block after the one being freed is empty, coalesce these two together
if(currentBlock -> next != NULL && currentBlock -> next -> occ == 0){
currentBlock -> size = currentBlock -> size + currentBlock -> next -> size;
currentBlock -> next = currentBlock -> next -> next;
if(currentBlock -> next != NULL){
struct Block *tempBlock = currentBlock -> next;
tempBlock -> prev = currentBlock;
}
}
//If the block before the one being freed is empty, change the current block to the previous one
//and do the same procedure as above to coalesce the two together
if(currentBlock != head && currentBlock -> prev -> occ == 0){
currentBlock = currentBlock -> prev;
currentBlock -> size = currentBlock -> size + currentBlock -> next -> size;
currentBlock -> next = currentBlock -> next -> next;
if(currentBlock -> next != NULL){
currentBlock -> next -> prev = currentBlock;
}
}
//If the free block is next to the end of the heap, decrease the size of the heap
//by disbanding the last block
if(currentBlock -> next == NULL){
int sizeDecrease = 0 - currentBlock -> size;
sbrk(sizeDecrease);
if(currentBlock == head){
brk(head);
head = NULL;
}
else{
currentBlock -> prev -> next == NULL;
}
}
}
and my testing program:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
void *my_malloc(int size);
void my_free(void *data);
//include your code
//replace malloc here with the appropriate version of my_malloc
#define MALLOC my_malloc
//replace free here with the appropriate version of my_free
#define FREE my_free
//define DUMP_HEAP() to be dump_heap() when you are done
#define DUMP_HEAP()
int main()
{
char *dummy = MALLOC(23 + 24 * 5);
FREE(dummy);
DUMP_HEAP();
char *this = MALLOC(5);
char *is = MALLOC(3);
char *a = MALLOC(2);
char *test = MALLOC(5);
char *program = MALLOC(8);
DUMP_HEAP();
strcpy(this, "this");
strcpy(is, "is");
strcpy(a, "a");
strcpy(test, "test");
strcpy(program, "program");
printf("%s %s %s %s %sn", this, is, a, test, program);
DUMP_HEAP();
FREE(is);
FREE(test);
printf("%s %s %s %s %sn", this, "*", a, "*", program);
DUMP_HEAP();
FREE(this);
printf("%s %s %s %s %sn", "*", "*", a, "*", program);
DUMP_HEAP();
this = MALLOC(5);
//TROUBLESHOOTING: PRINT WHAT'S ACTUALLY BEING RETURNED
printf("%d ", (char *)this);
strcpy(this, "this");
printf("%s %s %s %s %sn", this, "*", a, "*", program);
DUMP_HEAP();
is = MALLOC(3);
test = MALLOC(5);
//MORE TROUBLESHOOTING
printf("%d ", (char *)test);
strcpy(is, "is");
strcpy(test, "test");
printf("%s %s %s %s %sn", this, is, a, test, program);
DUMP_HEAP();
FREE(test);
FREE(is);
FREE(a);
FREE(this);
FREE(program);
printf("%s %s %s %s %sn", "*", "*", "*", "*", "*");
DUMP_HEAP();
return 0;
}
The issue I'm having here is a seg fault when the line FREE(this) runs at the end of the program. I assume this is because of the wrong pointer being returned from my my_malloc function. I apologize for the code not being super concise, thanks in advance for any help though!
c pointers segmentation-fault return
add a comment |
What is the reason for this error? I am creating a personal version of malloc and free for my C class and but encountering the following issue: I split a free spot into 2 nodes using the best fit method to insert a value into my linked list, however it's returning the incorrect value to the testing program. The value of currentBlock + sizeof(struct Block) is 6299672 but when I print it out from the testing program it prints 6300251. This leads to a seg fault due to the pointer being incorrect when I free it later in the program. Is this an issue from incorrect casting or am I missing something else?
Thank you! The relevant sections of my code are below:
Below is my my_malloc and my_free methods:
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <assert.h>
struct Block {
int occ; // whether block is occupied
int size; // size of block (including header)
struct Block *prev; // pointer to previous block
struct Block *next; // pointer to next block
};
static struct Block *head = NULL; // head of list
void *my_malloc(int size)
{
struct Block *currentBlock = head;
struct Block *bestFitBlock = NULL;
int bestFit = 0;
void *offset = sbrk(0);
void *ending = offset + size + sizeof(struct Block);
int success = brk(ending);
struct Block *newBlock = offset;
newBlock -> occ = 1;
newBlock -> size = size + sizeof(struct Block);
while(head != NULL && currentBlock != NULL){
//If the block you're currently looking at is free and either the same or larger size than the incoming block,
//then split the block and take up the block and have a free block
if(currentBlock -> occ == 0){
if(currentBlock -> size >= newBlock -> size){
//If the incoming block and the free block are the same size, just fill it in
if(currentBlock -> size == newBlock -> size){
currentBlock -> occ = 1;
int removeExtra = 0 - newBlock -> size;
sbrk(removeExtra);
return currentBlock + sizeof(struct Block);
}
//If they're bigger sizes, split it into 2 blocks and fill 1 in. Must also leave room for another block to the right
else if(currentBlock -> size >= newBlock -> size + sizeof(struct Block)){
//Set the integer to 1 to denote that there is a place for it to go if no blocks match size
bestFit = 1;
//Save the block in which it would fit best if no blocks match size
if(bestFitBlock == NULL){
bestFitBlock = currentBlock;
}
else if(currentBlock -> size > bestFitBlock -> size){
bestFitBlock = currentBlock;
}
}
}
}
//Go to the next block
currentBlock = currentBlock -> next;
}
//Split the block into a taken and free slot
if(bestFit == 1){
currentBlock = bestFitBlock;
int freeSize = currentBlock -> size - newBlock -> size;
currentBlock -> occ = 1;
currentBlock -> size = newBlock -> size;
void *tempPointer = (int)currentBlock + (int)currentBlock -> size;
struct Block *newFreeBlock = tempPointer;
newFreeBlock -> occ = 0;
newFreeBlock -> size = freeSize;
newFreeBlock -> next = currentBlock -> next;
newFreeBlock -> prev = currentBlock;
newFreeBlock -> next -> prev = newFreeBlock;
currentBlock -> next = newFreeBlock;
//TROUBLESHOOTING: PRINT WHAT IS SUPPOSED TO BE RETURNED
printf("%d ", (int)currentBlock + (int)sizeof(struct Block));
int removeExtra = 0 - newBlock -> size;
sbrk(removeExtra);
return (int)currentBlock + (int)sizeof(struct Block);
}
if(head == NULL){
head = newBlock;
}
else{
currentBlock = head;
while(currentBlock -> next != NULL){
currentBlock = currentBlock -> next;
}
currentBlock -> next = newBlock;
newBlock -> prev = currentBlock;
}
if(success == 0){
return offset + sizeof(struct Block);
}
else{
printf("Error - Malloc not completedn");
return (int *)-1;
}
}
void my_free(void *data)
{
struct Block *currentBlock = head;
void *lookingFor = data - sizeof(struct Block);
while(lookingFor != currentBlock){
currentBlock = currentBlock -> next;
}
currentBlock -> occ = 0;
//If the block after the one being freed is empty, coalesce these two together
if(currentBlock -> next != NULL && currentBlock -> next -> occ == 0){
currentBlock -> size = currentBlock -> size + currentBlock -> next -> size;
currentBlock -> next = currentBlock -> next -> next;
if(currentBlock -> next != NULL){
struct Block *tempBlock = currentBlock -> next;
tempBlock -> prev = currentBlock;
}
}
//If the block before the one being freed is empty, change the current block to the previous one
//and do the same procedure as above to coalesce the two together
if(currentBlock != head && currentBlock -> prev -> occ == 0){
currentBlock = currentBlock -> prev;
currentBlock -> size = currentBlock -> size + currentBlock -> next -> size;
currentBlock -> next = currentBlock -> next -> next;
if(currentBlock -> next != NULL){
currentBlock -> next -> prev = currentBlock;
}
}
//If the free block is next to the end of the heap, decrease the size of the heap
//by disbanding the last block
if(currentBlock -> next == NULL){
int sizeDecrease = 0 - currentBlock -> size;
sbrk(sizeDecrease);
if(currentBlock == head){
brk(head);
head = NULL;
}
else{
currentBlock -> prev -> next == NULL;
}
}
}
and my testing program:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
void *my_malloc(int size);
void my_free(void *data);
//include your code
//replace malloc here with the appropriate version of my_malloc
#define MALLOC my_malloc
//replace free here with the appropriate version of my_free
#define FREE my_free
//define DUMP_HEAP() to be dump_heap() when you are done
#define DUMP_HEAP()
int main()
{
char *dummy = MALLOC(23 + 24 * 5);
FREE(dummy);
DUMP_HEAP();
char *this = MALLOC(5);
char *is = MALLOC(3);
char *a = MALLOC(2);
char *test = MALLOC(5);
char *program = MALLOC(8);
DUMP_HEAP();
strcpy(this, "this");
strcpy(is, "is");
strcpy(a, "a");
strcpy(test, "test");
strcpy(program, "program");
printf("%s %s %s %s %sn", this, is, a, test, program);
DUMP_HEAP();
FREE(is);
FREE(test);
printf("%s %s %s %s %sn", this, "*", a, "*", program);
DUMP_HEAP();
FREE(this);
printf("%s %s %s %s %sn", "*", "*", a, "*", program);
DUMP_HEAP();
this = MALLOC(5);
//TROUBLESHOOTING: PRINT WHAT'S ACTUALLY BEING RETURNED
printf("%d ", (char *)this);
strcpy(this, "this");
printf("%s %s %s %s %sn", this, "*", a, "*", program);
DUMP_HEAP();
is = MALLOC(3);
test = MALLOC(5);
//MORE TROUBLESHOOTING
printf("%d ", (char *)test);
strcpy(is, "is");
strcpy(test, "test");
printf("%s %s %s %s %sn", this, is, a, test, program);
DUMP_HEAP();
FREE(test);
FREE(is);
FREE(a);
FREE(this);
FREE(program);
printf("%s %s %s %s %sn", "*", "*", "*", "*", "*");
DUMP_HEAP();
return 0;
}
The issue I'm having here is a seg fault when the line FREE(this) runs at the end of the program. I assume this is because of the wrong pointer being returned from my my_malloc function. I apologize for the code not being super concise, thanks in advance for any help though!
c pointers segmentation-fault return
You have twoFREE(this);
in your program. I bet this comes from the value ofthis
being incorrectly reinitialized.
– Jean-Marc Zimmer
Nov 14 '18 at 10:42
add a comment |
What is the reason for this error? I am creating a personal version of malloc and free for my C class and but encountering the following issue: I split a free spot into 2 nodes using the best fit method to insert a value into my linked list, however it's returning the incorrect value to the testing program. The value of currentBlock + sizeof(struct Block) is 6299672 but when I print it out from the testing program it prints 6300251. This leads to a seg fault due to the pointer being incorrect when I free it later in the program. Is this an issue from incorrect casting or am I missing something else?
Thank you! The relevant sections of my code are below:
Below is my my_malloc and my_free methods:
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <assert.h>
struct Block {
int occ; // whether block is occupied
int size; // size of block (including header)
struct Block *prev; // pointer to previous block
struct Block *next; // pointer to next block
};
static struct Block *head = NULL; // head of list
void *my_malloc(int size)
{
struct Block *currentBlock = head;
struct Block *bestFitBlock = NULL;
int bestFit = 0;
void *offset = sbrk(0);
void *ending = offset + size + sizeof(struct Block);
int success = brk(ending);
struct Block *newBlock = offset;
newBlock -> occ = 1;
newBlock -> size = size + sizeof(struct Block);
while(head != NULL && currentBlock != NULL){
//If the block you're currently looking at is free and either the same or larger size than the incoming block,
//then split the block and take up the block and have a free block
if(currentBlock -> occ == 0){
if(currentBlock -> size >= newBlock -> size){
//If the incoming block and the free block are the same size, just fill it in
if(currentBlock -> size == newBlock -> size){
currentBlock -> occ = 1;
int removeExtra = 0 - newBlock -> size;
sbrk(removeExtra);
return currentBlock + sizeof(struct Block);
}
//If they're bigger sizes, split it into 2 blocks and fill 1 in. Must also leave room for another block to the right
else if(currentBlock -> size >= newBlock -> size + sizeof(struct Block)){
//Set the integer to 1 to denote that there is a place for it to go if no blocks match size
bestFit = 1;
//Save the block in which it would fit best if no blocks match size
if(bestFitBlock == NULL){
bestFitBlock = currentBlock;
}
else if(currentBlock -> size > bestFitBlock -> size){
bestFitBlock = currentBlock;
}
}
}
}
//Go to the next block
currentBlock = currentBlock -> next;
}
//Split the block into a taken and free slot
if(bestFit == 1){
currentBlock = bestFitBlock;
int freeSize = currentBlock -> size - newBlock -> size;
currentBlock -> occ = 1;
currentBlock -> size = newBlock -> size;
void *tempPointer = (int)currentBlock + (int)currentBlock -> size;
struct Block *newFreeBlock = tempPointer;
newFreeBlock -> occ = 0;
newFreeBlock -> size = freeSize;
newFreeBlock -> next = currentBlock -> next;
newFreeBlock -> prev = currentBlock;
newFreeBlock -> next -> prev = newFreeBlock;
currentBlock -> next = newFreeBlock;
//TROUBLESHOOTING: PRINT WHAT IS SUPPOSED TO BE RETURNED
printf("%d ", (int)currentBlock + (int)sizeof(struct Block));
int removeExtra = 0 - newBlock -> size;
sbrk(removeExtra);
return (int)currentBlock + (int)sizeof(struct Block);
}
if(head == NULL){
head = newBlock;
}
else{
currentBlock = head;
while(currentBlock -> next != NULL){
currentBlock = currentBlock -> next;
}
currentBlock -> next = newBlock;
newBlock -> prev = currentBlock;
}
if(success == 0){
return offset + sizeof(struct Block);
}
else{
printf("Error - Malloc not completedn");
return (int *)-1;
}
}
void my_free(void *data)
{
struct Block *currentBlock = head;
void *lookingFor = data - sizeof(struct Block);
while(lookingFor != currentBlock){
currentBlock = currentBlock -> next;
}
currentBlock -> occ = 0;
//If the block after the one being freed is empty, coalesce these two together
if(currentBlock -> next != NULL && currentBlock -> next -> occ == 0){
currentBlock -> size = currentBlock -> size + currentBlock -> next -> size;
currentBlock -> next = currentBlock -> next -> next;
if(currentBlock -> next != NULL){
struct Block *tempBlock = currentBlock -> next;
tempBlock -> prev = currentBlock;
}
}
//If the block before the one being freed is empty, change the current block to the previous one
//and do the same procedure as above to coalesce the two together
if(currentBlock != head && currentBlock -> prev -> occ == 0){
currentBlock = currentBlock -> prev;
currentBlock -> size = currentBlock -> size + currentBlock -> next -> size;
currentBlock -> next = currentBlock -> next -> next;
if(currentBlock -> next != NULL){
currentBlock -> next -> prev = currentBlock;
}
}
//If the free block is next to the end of the heap, decrease the size of the heap
//by disbanding the last block
if(currentBlock -> next == NULL){
int sizeDecrease = 0 - currentBlock -> size;
sbrk(sizeDecrease);
if(currentBlock == head){
brk(head);
head = NULL;
}
else{
currentBlock -> prev -> next == NULL;
}
}
}
and my testing program:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
void *my_malloc(int size);
void my_free(void *data);
//include your code
//replace malloc here with the appropriate version of my_malloc
#define MALLOC my_malloc
//replace free here with the appropriate version of my_free
#define FREE my_free
//define DUMP_HEAP() to be dump_heap() when you are done
#define DUMP_HEAP()
int main()
{
char *dummy = MALLOC(23 + 24 * 5);
FREE(dummy);
DUMP_HEAP();
char *this = MALLOC(5);
char *is = MALLOC(3);
char *a = MALLOC(2);
char *test = MALLOC(5);
char *program = MALLOC(8);
DUMP_HEAP();
strcpy(this, "this");
strcpy(is, "is");
strcpy(a, "a");
strcpy(test, "test");
strcpy(program, "program");
printf("%s %s %s %s %sn", this, is, a, test, program);
DUMP_HEAP();
FREE(is);
FREE(test);
printf("%s %s %s %s %sn", this, "*", a, "*", program);
DUMP_HEAP();
FREE(this);
printf("%s %s %s %s %sn", "*", "*", a, "*", program);
DUMP_HEAP();
this = MALLOC(5);
//TROUBLESHOOTING: PRINT WHAT'S ACTUALLY BEING RETURNED
printf("%d ", (char *)this);
strcpy(this, "this");
printf("%s %s %s %s %sn", this, "*", a, "*", program);
DUMP_HEAP();
is = MALLOC(3);
test = MALLOC(5);
//MORE TROUBLESHOOTING
printf("%d ", (char *)test);
strcpy(is, "is");
strcpy(test, "test");
printf("%s %s %s %s %sn", this, is, a, test, program);
DUMP_HEAP();
FREE(test);
FREE(is);
FREE(a);
FREE(this);
FREE(program);
printf("%s %s %s %s %sn", "*", "*", "*", "*", "*");
DUMP_HEAP();
return 0;
}
The issue I'm having here is a seg fault when the line FREE(this) runs at the end of the program. I assume this is because of the wrong pointer being returned from my my_malloc function. I apologize for the code not being super concise, thanks in advance for any help though!
c pointers segmentation-fault return
What is the reason for this error? I am creating a personal version of malloc and free for my C class and but encountering the following issue: I split a free spot into 2 nodes using the best fit method to insert a value into my linked list, however it's returning the incorrect value to the testing program. The value of currentBlock + sizeof(struct Block) is 6299672 but when I print it out from the testing program it prints 6300251. This leads to a seg fault due to the pointer being incorrect when I free it later in the program. Is this an issue from incorrect casting or am I missing something else?
Thank you! The relevant sections of my code are below:
Below is my my_malloc and my_free methods:
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <assert.h>
struct Block {
int occ; // whether block is occupied
int size; // size of block (including header)
struct Block *prev; // pointer to previous block
struct Block *next; // pointer to next block
};
static struct Block *head = NULL; // head of list
void *my_malloc(int size)
{
struct Block *currentBlock = head;
struct Block *bestFitBlock = NULL;
int bestFit = 0;
void *offset = sbrk(0);
void *ending = offset + size + sizeof(struct Block);
int success = brk(ending);
struct Block *newBlock = offset;
newBlock -> occ = 1;
newBlock -> size = size + sizeof(struct Block);
while(head != NULL && currentBlock != NULL){
//If the block you're currently looking at is free and either the same or larger size than the incoming block,
//then split the block and take up the block and have a free block
if(currentBlock -> occ == 0){
if(currentBlock -> size >= newBlock -> size){
//If the incoming block and the free block are the same size, just fill it in
if(currentBlock -> size == newBlock -> size){
currentBlock -> occ = 1;
int removeExtra = 0 - newBlock -> size;
sbrk(removeExtra);
return currentBlock + sizeof(struct Block);
}
//If they're bigger sizes, split it into 2 blocks and fill 1 in. Must also leave room for another block to the right
else if(currentBlock -> size >= newBlock -> size + sizeof(struct Block)){
//Set the integer to 1 to denote that there is a place for it to go if no blocks match size
bestFit = 1;
//Save the block in which it would fit best if no blocks match size
if(bestFitBlock == NULL){
bestFitBlock = currentBlock;
}
else if(currentBlock -> size > bestFitBlock -> size){
bestFitBlock = currentBlock;
}
}
}
}
//Go to the next block
currentBlock = currentBlock -> next;
}
//Split the block into a taken and free slot
if(bestFit == 1){
currentBlock = bestFitBlock;
int freeSize = currentBlock -> size - newBlock -> size;
currentBlock -> occ = 1;
currentBlock -> size = newBlock -> size;
void *tempPointer = (int)currentBlock + (int)currentBlock -> size;
struct Block *newFreeBlock = tempPointer;
newFreeBlock -> occ = 0;
newFreeBlock -> size = freeSize;
newFreeBlock -> next = currentBlock -> next;
newFreeBlock -> prev = currentBlock;
newFreeBlock -> next -> prev = newFreeBlock;
currentBlock -> next = newFreeBlock;
//TROUBLESHOOTING: PRINT WHAT IS SUPPOSED TO BE RETURNED
printf("%d ", (int)currentBlock + (int)sizeof(struct Block));
int removeExtra = 0 - newBlock -> size;
sbrk(removeExtra);
return (int)currentBlock + (int)sizeof(struct Block);
}
if(head == NULL){
head = newBlock;
}
else{
currentBlock = head;
while(currentBlock -> next != NULL){
currentBlock = currentBlock -> next;
}
currentBlock -> next = newBlock;
newBlock -> prev = currentBlock;
}
if(success == 0){
return offset + sizeof(struct Block);
}
else{
printf("Error - Malloc not completedn");
return (int *)-1;
}
}
void my_free(void *data)
{
struct Block *currentBlock = head;
void *lookingFor = data - sizeof(struct Block);
while(lookingFor != currentBlock){
currentBlock = currentBlock -> next;
}
currentBlock -> occ = 0;
//If the block after the one being freed is empty, coalesce these two together
if(currentBlock -> next != NULL && currentBlock -> next -> occ == 0){
currentBlock -> size = currentBlock -> size + currentBlock -> next -> size;
currentBlock -> next = currentBlock -> next -> next;
if(currentBlock -> next != NULL){
struct Block *tempBlock = currentBlock -> next;
tempBlock -> prev = currentBlock;
}
}
//If the block before the one being freed is empty, change the current block to the previous one
//and do the same procedure as above to coalesce the two together
if(currentBlock != head && currentBlock -> prev -> occ == 0){
currentBlock = currentBlock -> prev;
currentBlock -> size = currentBlock -> size + currentBlock -> next -> size;
currentBlock -> next = currentBlock -> next -> next;
if(currentBlock -> next != NULL){
currentBlock -> next -> prev = currentBlock;
}
}
//If the free block is next to the end of the heap, decrease the size of the heap
//by disbanding the last block
if(currentBlock -> next == NULL){
int sizeDecrease = 0 - currentBlock -> size;
sbrk(sizeDecrease);
if(currentBlock == head){
brk(head);
head = NULL;
}
else{
currentBlock -> prev -> next == NULL;
}
}
}
and my testing program:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
void *my_malloc(int size);
void my_free(void *data);
//include your code
//replace malloc here with the appropriate version of my_malloc
#define MALLOC my_malloc
//replace free here with the appropriate version of my_free
#define FREE my_free
//define DUMP_HEAP() to be dump_heap() when you are done
#define DUMP_HEAP()
int main()
{
char *dummy = MALLOC(23 + 24 * 5);
FREE(dummy);
DUMP_HEAP();
char *this = MALLOC(5);
char *is = MALLOC(3);
char *a = MALLOC(2);
char *test = MALLOC(5);
char *program = MALLOC(8);
DUMP_HEAP();
strcpy(this, "this");
strcpy(is, "is");
strcpy(a, "a");
strcpy(test, "test");
strcpy(program, "program");
printf("%s %s %s %s %sn", this, is, a, test, program);
DUMP_HEAP();
FREE(is);
FREE(test);
printf("%s %s %s %s %sn", this, "*", a, "*", program);
DUMP_HEAP();
FREE(this);
printf("%s %s %s %s %sn", "*", "*", a, "*", program);
DUMP_HEAP();
this = MALLOC(5);
//TROUBLESHOOTING: PRINT WHAT'S ACTUALLY BEING RETURNED
printf("%d ", (char *)this);
strcpy(this, "this");
printf("%s %s %s %s %sn", this, "*", a, "*", program);
DUMP_HEAP();
is = MALLOC(3);
test = MALLOC(5);
//MORE TROUBLESHOOTING
printf("%d ", (char *)test);
strcpy(is, "is");
strcpy(test, "test");
printf("%s %s %s %s %sn", this, is, a, test, program);
DUMP_HEAP();
FREE(test);
FREE(is);
FREE(a);
FREE(this);
FREE(program);
printf("%s %s %s %s %sn", "*", "*", "*", "*", "*");
DUMP_HEAP();
return 0;
}
The issue I'm having here is a seg fault when the line FREE(this) runs at the end of the program. I assume this is because of the wrong pointer being returned from my my_malloc function. I apologize for the code not being super concise, thanks in advance for any help though!
c pointers segmentation-fault return
c pointers segmentation-fault return
edited Nov 13 '18 at 20:40
dchi
212
212
asked Nov 13 '18 at 20:23
Joe DuckoJoe Ducko
113
113
You have twoFREE(this);
in your program. I bet this comes from the value ofthis
being incorrectly reinitialized.
– Jean-Marc Zimmer
Nov 14 '18 at 10:42
add a comment |
You have twoFREE(this);
in your program. I bet this comes from the value ofthis
being incorrectly reinitialized.
– Jean-Marc Zimmer
Nov 14 '18 at 10:42
You have two
FREE(this);
in your program. I bet this comes from the value of this
being incorrectly reinitialized.– Jean-Marc Zimmer
Nov 14 '18 at 10:42
You have two
FREE(this);
in your program. I bet this comes from the value of this
being incorrectly reinitialized.– Jean-Marc Zimmer
Nov 14 '18 at 10:42
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288938%2fwrong-pointer-returned-when-creating-a-version-of-malloc-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288938%2fwrong-pointer-returned-when-creating-a-version-of-malloc-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You have two
FREE(this);
in your program. I bet this comes from the value ofthis
being incorrectly reinitialized.– Jean-Marc Zimmer
Nov 14 '18 at 10:42