Thread: Dynamic memory Worst Fit

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    2

    Dynamic memory Worst Fit

    Hey fellow C programmers, I am working on an assignment for school. We have to make a dynamic memory map. Rules:
    • Only used <stdio.h>
    • One global char[] of a defined MEM_SIZE
    • Four functions we will use:
      • void mem_init(void);
      • void dump_mem_structs(void);
      • void *my_malloc(size_t size);
      • void my_free(void *ptr);


    Mem_init initialises the variables for *my_malloc(size_t size) and for my_free(void *ptr). Variables used for memory management are not allowed to be created in main OR globally.

    Dump_mem_structs shows the blocks that are in use.

    *my_malloc returns a block of the global char[] as a ptr to be used. Malloc should keep track of what memory block is used and what is not.

    my_free releases the memory that was claimed by *my_malloc.

    Given prototype:
    Code:
    void main(void)
    {
        void *p[10];
    
    
        mem_init();
        dump_mem_structs();
    
    
        p[1] = my_malloc(5);
        p[2] = my_malloc(3);
        p[3] = my_malloc(6);
        p[4] = my_malloc(4);
        p[5] = my_malloc(2);
        p[6] = my_malloc(6);
        p[7] = my_malloc(3);
        p[8] = my_malloc(3);
    
    
        dump_mem_structs();
    
    
        my_free(p[2]);
        my_free(p[5]);
        my_free(p[8]);
    
    
        dump_mem_structs();
    
    
        p[9] = my_malloc(4);
    
    
        dump_mem_structs();
    }
    What I added so far:
    Code:
    #include <stdio.h>
    
    
    #define MEM_SIZE 100
    
    
    void mem_init(void);
    void dump_mem_structs(void);
    void *my_malloc(size_t size);
    void my_free(void *ptr);
    
    
    typedef struct memory_block {
    	int used;
    	int size;
    	//void *data;
    } mem_block;
    
    
    char RAM[MEM_SIZE];
    
    ... main ...
    
    
    void mem_init(void) {
        mem_block *ptr = (mem_block *)RAM;
        
        ptr->size = MEM_SIZE;
        ptr->used = 0;
        
        return;
    }
    
    
    void dump_mem_structs(void) {
        char *status;
        
        mem_block *ptr = (mem_block *)RAM;
        
        if (ptr->used == 0)
            status = "Free";
        else
            status = "In use";
        printf("Block of %5d bytes: %s\n", ptr->size, status);
    
    
        return;
    }
    
    
    void *my_malloc(size_t size) {
        int i;
        
        mem_block *ptr = (mem_block *)RAM;
        ptr->size = (ptr->size - size);
        
        for (i = 0; i < size; i++) {
            ptr->size = size;
        }
        
        return (p);
        
    }
    
    
    void my_free(void *ptr) {
        return;
    }
    My problem is, I start out with only 1 free block of 100 bytes. Now in my current way I just rewrite that same block back to 5 bytes and the free 95 bytes just disappear. How can I make this work without declaring a global array of the struct?

    What I would have done was to #define AMOUNT_BLOCKS and build my code based on that. So void *p[10] would be void *p[AMOUNT_BLOCKS] and I'd define a global array mem_block blocks[AMOUNT_BLOCKS]. But that's not allowed..

    Can't get my head around this one!

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,036
    In mem_init allocate a block(s) to keep track of all the blocks the user will allocate.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    2
    Thanks! I made my code work a little better, but not fully functional yet.

    Code:
    #include <unistd.h>
    
    
    #define MEM_SIZE 100
    
    
    typedef struct memory_block {
    	int used;
    	int size;
    } mem_block;
    
    
    void my_malloc(size_t size);
    void dump_mem_structs(void);
    void mem_init(void);
    mem_block *next_struct(mem_block *p_curr);
    
    
    char RAM[MEM_SIZE];
    
    
    int main() {
    	
    	mem_init();
    	dump_mem_structs();
    	printf("\nAllocating new memory...\n\n");
    	
    	my_malloc(5);
    	my_malloc(7);
    	my_malloc(9);
    	my_malloc(12);
    	my_malloc(5);
    	
    	dump_mem_structs();
    
    
    	
    	return 0;
    }
    
    
    void mem_init(void) {
    	printf("\nStart mem address: %p\nEnd mem address  : %p\n\n", &RAM[0], &RAM[100]);
    	mem_block *ptr = (mem_block *)RAM;
    	
    	ptr->size = MEM_SIZE;
    	ptr->used = 0;
    	
    	return;
    }
    
    
    void my_malloc(size_t size) {
    	
    	mem_block *ptr = (mem_block *)RAM;
    	
    	while (ptr->used == 1) {
    		ptr = next_struct(ptr);
    	}
    	printf("Next pointer address: %p\n", ptr);
    	
    	ptr->used = 1;
    	ptr->size = size;
    	
    	return;
    }
    
    
    void dump_mem_structs(void) {
    	char *status;
    	mem_block *ptr = (mem_block *)RAM;
    	int curr_block_size = ptr->size + sizeof(mem_block);
    	
    	while (&RAM[0] + curr_block_size <= (&RAM[MEM_SIZE] + sizeof(mem_block))) {
    		if (ptr->used == 0)
    			status = "Free";
    		else
    			status = "In use";
    		
    		printf("Block of %5d bytes: %s\n", ptr->size + (int)sizeof(mem_block), status);
    		ptr = next_struct(ptr);
    		curr_block_size += (ptr->size + sizeof(mem_block));
    	}
    	
    	return;
    }
    
    
    mem_block *next_struct(mem_block *p_curr) {
    	
    	//printf("Address of argument: %p\nOne mem_block higher: %p\n", p_curr, p_curr + (int)(sizeof(mem_block)));
    	//printf("Size of mem_block: %d\n", (int)sizeof(mem_block));
    	
    	//mem_block *ptr = (mem_block *)RAM;
    	//int next = p_curr->size + 1;//sizeof(mem_block);
    	
    	mem_block *next = p_curr + sizeof(mem_block); //sizeof(mem_block);
    	return next;
    	
    	//return (p_curr + next);
    }
    Excuse my comments, lol! My output is:

    Next pointer address: 0x601080
    Next pointer address: 0x6010c0
    Next pointer address: 0x601100

    And some more things. But these matter right now. Why does my function *next_struct increase my address by 64 every time it is called? Sizeof(mem_block) is 8. So shouldn't it go from 0x601080 to 0x601088?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,323
    Quote Originally Posted by Athylus
    Why does my function *next_struct increase my address by 64 every time it is called? Sizeof(mem_block) is 8. So shouldn't it go from 0x601080 to 0x601088?
    You're dealing with pointers, not directly with numeric memory addresses. So, if you have a pointer to T named p at memory address 100, where sizeof(T) == 8, p + 1 gives you a pointer to the T object in the next position (or the next element in an array of T objects), hence the memory address of (p + 1) would be 100 + (1 * 8) = 108. By doing p + sizeof(T), would would get a pointer to the T object sizeof(T) == 8 positions away, hence the resulting memory address would be 100 + (8 * 8) = 164 instead of 108.

    As such, I would get rid of the next_struct function and instead just add 1 (or increment) the pointer that would have been passed as the argument.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using a dynamic memory
    By RyanC in forum C Programming
    Replies: 8
    Last Post: 06-04-2015, 09:49 AM
  2. Help in Dynamic Memory again!
    By Xian Yao in forum C Programming
    Replies: 3
    Last Post: 12-02-2013, 11:47 PM
  3. Dynamic memory in C
    By JamesD in forum C Programming
    Replies: 38
    Last Post: 03-15-2011, 12:35 AM
  4. Dynamic memory and realloc(), freeing memory
    By C_Sparky in forum C Programming
    Replies: 6
    Last Post: 10-06-2010, 07:55 PM
  5. static memory and dynamic memory
    By nextus in forum C++ Programming
    Replies: 1
    Last Post: 03-01-2003, 08:46 PM

Tags for this Thread