I am creating a replacement memory manager for a school project and I am having trouble with comparing the memory address of a variable in my main.c file to my Node -> starting address, found in my mem.c file. The last 8 digits of the memory address appear to be the same, but the Node -> starting address usually has an extra 4 digits prepended to the memory address.
TLDR I need to test if node -> starting address == memory address of x, and its not working
printf ("var x: %p \n", x ); out = 0x3ee30671
printf("new node start address: %p \n", new_node -> start_address ); out = 0x56223ee30671
main.c
#include <stdio.h>
#define USE_REPLACEMENT_MANAGER 1
#if USE_REPLACEMENT_MANAGER
#undef malloc
#define malloc myMalloc
#undef free
#define free myFree
#include "replacement.h"
#else
#include <stdlib.h>
void* (*f) (size_t s) = malloc;
#endif
int main( int argc, const char * argv[] ){
char * x = (char *) malloc(16);
printf ("var x: %p \n", x );
free(x);
}
replacement.c
#include <stdio.h>
#include "mem.h"
#include "replacement.h"
void* myMalloc( size_t size){
return getAddress ( size);
}
void myFree( void * ptr ){
printf("free address: %p \n", ptr);
mmFree( ptr );
}
replacement.h
#ifdef REPLACEMENT_MANAGER_INCLUDED
#define REPLACEMENT_MANAGER_INCLUDED
void* myMalloc( size_t size);
void myFree( void * ptr );
void printMyMap ( void );
#endif
mem.c
#include <stdio.h>
#include <stdlib.h>
#include "mem.h"
typedef struct Node{
int type;
size_t size;
char* start_address;
struct Node* next;
struct Node* prev;
}Node;
/*
* PRIVATE FUNCTIONS
*/
void init_heap( void );
/*
* PRIVATE FILE LEVEL GLOBAL VARIABLES
*/
#define HEAP_SIZE 1000
char * heap = NULL;
Node* head = NULL;
// FUNCTION THAT ALLOCATES SPACE ON REPLACEMENT HEAP AND RETURNS A POINTER TO THE START ADDRESS
void* getAddress ( size_t size ){
if ( heap == NULL ){
init_heap();
}
Node * curr = head;
while ( curr -> next != NULL ){
if ( ( curr -> size < size ) && ( curr -> type == 0 ) ){
return curr -> start_address;
} else curr = curr -> next;
}
Node* new_node = (Node *) malloc( sizeof(Node) );
new_node -> type = 1;
new_node -> size = size;
new_node -> start_address = ( curr -> start_address ) + ( curr -> size ) + 1;
new_node -> next = NULL;
new_node -> prev = curr;
curr -> next = new_node;
printf("new node start address: %p \n", new_node -> start_address );
return new_node -> start_address;
}
// FUNCTION THAT INITIALIZES REPLACEMENT HEAP AND THE HEAD OF THE LINKED LIST
void init_heap( void ){
heap = malloc( HEAP_SIZE );
printf("heap : %p \n",heap);
head = (Node*) malloc( sizeof(Node) );
head -> type = 1;
head -> size = 0;
head -> start_address = heap;
head -> next = NULL;
head -> prev = NULL;
}
void mmFree( void * ptr ){
Node * curr = head;
printf( "%p \n", (char*) curr -> start_address );
while ( curr -> next != NULL ){
if ( curr -> start_address == ptr ){
printf( "I NEED THIS TO PRINT" );
curr -> type = 0;
break;
} else curr = curr -> next;
}
Node * p = curr -> prev;
Node * n = curr -> next;
if ( ( p != NULL ) && ( p -> type == 0 ) ){
curr -> start_address = p -> start_address;
curr -> size = ( curr -> size ) + ( p -> size );
p -> prev -> next = curr;
curr -> prev = p -> prev;
free ( p );
}
if ( ( n != NULL ) && (n -> type == 0) ){
curr -> size = ( curr -> size ) + ( n -> size );
if ( n -> next != NULL ){
n -> next -> prev = curr;
curr -> next = n -> next;
}else curr -> next = NULL;
free ( n );
}
}
mem.h
#ifdef MEM_INCLUDED
#define MEM_INCLUDED
void mmFree( void * ptr);
void* getAddress ( size_t size );
void printHeapMap( void );
#endif
OS: Ubuntu on vitual box VM – Andrew Zelano Feb 22 '19 at 23:33