Integrating malloc

This is a discussion on Integrating malloc within the C Programming forums, part of the General Programming Boards category; Hello all, I have read, and reviewed the book "The C Programming Language", and I saw an example of how ...

  1. #1
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477

    Integrating malloc

    Hello all,

    I have read, and reviewed the book "The C Programming Language", and I saw an example of how to use malloc, and free. I am trying to write a program that will issue a block of memory to it, and then use K&R's implementation just to handle that block wisely.

    I have run into a few problems though. Firstly, I believe I am missing an initialization of one or more of the pointers. I partially modified the code since I don't need morecore() or sbrk() since I already have a block of memory to work with:

    Code:
    #include <stdlib.h>
    
    typedef long Align;
    
    union header {
    	struct {
    		union header *ptr;
    		unsigned size;
    	} s;
    	Align x;
    };
    
    typedef union header Header;
    
    static Header base;
    static Header *freep = NULL;
    
    void* mymalloc(unsigned nbytes) {
    	Header *p, *prevp;
    	unsigned nunits;
    
    	nunits = (nbytes+sizeof(Header)-1)/sizeof(Header) + 1;
    	if ((prevp = freep) == NULL) {
    		base.s.ptr = freep = prevp = false;
    		base.s.size = 0;
    	}
    	for (p = prevp->s.ptr; ; prevp = p, p = p->s.ptr) {
    		if (p->s.size >= nunits) {
    			prevp->s.ptr = p->s.ptr;
    		}else {
    			p->s.size -= nunits;
    			p += p->s.size;
    			p->s.size = nunits;
    		}
    		freep = prevp;
    		return (void *)(p+1);
    	}
    	if (p == freep)
    		return NULL;
    	return (void *)0;
    }
    
    void myfree(void *ap) {
    	Header *bp, *p;
    
    	bp = (Header *)ap - 1;
    	for (p = freep; !(bp > p && bp < p->s.ptr); p = p->s.ptr)
    		if (p >= p->s.ptr && (bp > p || bp < p->s.ptr))
    			break;
    
    	if (bp + bp->s.size == p->s.ptr) {
    		bp->s.size += p->s.ptr->s.size;
    		bp->s.ptr = p->s.ptr->s.ptr;
    	}else
    		bp->s.ptr = p->s.ptr;
    	if (p + p->s.size == bp) {
    		p->s.size += bp->s.size;
    		p->s.ptr = bp->s.ptr;
    	}else
    		p->s.ptr = bp;
    	freep = p;
    }
    
    int main() {
    	char *test;
    
    	// allocating base (ex for block of memory)
    	base.s.ptr = (union header *)malloc(500);
    	base.s.size = 0;
    
    	// want to use my block wisely
    	test = (char *)mymalloc(25);
    
    	// free that part of my block
    	myfree(test);
    
    	free(base.s.ptr);
    
    	return 0;
    }
    Code 1.1: Implementing a version of malloc

    I believe it crashes during the first for loop of mymalloc(), but I'm not sure why. I've disected this code one line at a time, but still have no clue why it crashes. If its something simple, or I bypassed an important code or/else please let me know.


    Thank you for your time,
    - Stack Overflow
    Last edited by Stack Overflow; 08-02-2004 at 11:51 AM.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    The first problem I see is that you are compiling as C++. The actual problem that you are encountering is the dereference of a null pointer.
    Code:
    static Header *freep = NULL;
    freep becomes a null pointer.
    Code:
    void* mymalloc(unsigned nbytes) {
    	Header *p, *prevp;
    	unsigned nunits;
    
    	nunits = (nbytes+sizeof(Header)-1)/sizeof(Header) + 1;
    	if ((prevp = freep) == NULL) {
    prevp becomes a null pointer.
    Code:
    		base.s.ptr = freep = prevp = false;
    This tells me that you are compiling as C++. Only C99 supports false, and then you must include stdbool.h to use it. The effect of this line is to set base.s.ptr, freep and prevp to be 0, or a null pointer.
    Code:
    		base.s.size = 0;
    	}
    	for (p = prevp->s.ptr; ; prevp = p, p = p->s.ptr) {
    Here you attempt to dereference prevp which as you can see from above is presently a null pointer. That is what is causing your crash.

  3. #3
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Thanks for your analysis, I now understand. I'm trying to make this C as much as I can, so I'll be changing the code around alot.

    Do you think it would be a good idea if I removed the if ((prevp = freep) == NULL) line that sets my pointer to NULL?


    Thanks again,
    Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    Do you think it would be a good idea if I removed the if ((prevp = freep) == NULL) line that sets my pointer to NULL?
    It makes no difference as long as prevp points to memory in your address space before you dereference it. As is the function has flawed logic that needs to be resolved at the design level.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. the basics of malloc
    By nakedBallerina in forum C Programming
    Replies: 21
    Last Post: 05-20-2008, 02:32 AM
  3. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 11:26 AM
  4. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 08:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21