Thread: Malloc help

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    19

    Malloc help

    Hi, I have an assignment to make a memory allocator. I used my C book as a template of how to make it and I am running into 2 problems. One being whenever I try to debug it with gdb it instantly quits with this message: Program exited with code 0304.
    When I try to implement my malloc function in another program I made earlier I get this thing:
    *** glibc detected *** ./a.out: free(): invalid pointer: 0x095812d9 ***
    ======= Backtrace: =========
    /lib/tls/i686/cmov/libc.so.6[0x972ff1]
    /lib/tls/i686/cmov/libc.so.6[0x9746f2]
    /lib/tls/i686/cmov/libc.so.6(cfree+0x6d)[0x9777cd]
    ./a.out[0x80487c8]
    ./a.out[0x804862a]
    ./a.out[0x8048830]
    /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0x91eb56]
    ./a.out[0x8048581]
    ======= Memory map: ========
    003d8000-003d9000 r-xp 00000000 00:00 0 [vdso]
    00655000-00670000 r-xp 00000000 07:00 556017 /lib/ld-2.10.1.so
    00670000-00671000 r--p 0001a000 07:00 556017 /lib/ld-2.10.1.so
    00671000-00672000 rw-p 0001b000 07:00 556017 /lib/ld-2.10.1.so
    00908000-00a46000 r-xp 00000000 07:00 580508 /lib/tls/i686/cmov/libc-2.10.1.so
    00a46000-00a47000 ---p 0013e000 07:00 580508 /lib/tls/i686/cmov/libc-2.10.1.so
    00a47000-00a49000 r--p 0013e000 07:00 580508 /lib/tls/i686/cmov/libc-2.10.1.so
    00a49000-00a4a000 rw-p 00140000 07:00 580508 /lib/tls/i686/cmov/libc-2.10.1.so
    00a4a000-00a4d000 rw-p 00000000 00:00 0
    00bd7000-00bf3000 r-xp 00000000 07:00 555973 /lib/libgcc_s.so.1
    00bf3000-00bf4000 r--p 0001b000 07:00 555973 /lib/libgcc_s.so.1
    00bf4000-00bf5000 rw-p 0001c000 07:00 555973 /lib/libgcc_s.so.1
    08048000-08049000 r-xp 00000000 07:00 294736 /home/gene/NetBeansProjects/Malloc/a.out
    08049000-0804a000 r--p 00000000 07:00 294736 /home/gene/NetBeansProjects/Malloc/a.out
    0804a000-0804b000 rw-p 00001000 07:00 294736 /home/gene/NetBeansProjects/Malloc/a.out
    09581000-095a2000 rw-p 00000000 00:00 0 [heap]
    b7600000-b7621000 rw-p 00000000 00:00 0
    b7621000-b7700000 ---p 00000000 00:00 0
    b773b000-b773c000 rw-p 00000000 00:00 0
    b774d000-b774f000 rw-p 00000000 00:00 0
    bfc5b000-bfc70000 rw-p 00000000 00:00 0 [stack]
    Aborted

    This is my header file
    Code:
    #ifndef _FUNCTIONS_H
    #define	_FUNCTIONS_H
    
    struct hdr {
        unsigned int len :29;
        unsigned int excess :2;
        unsigned int allocated :1;
        struct hdr *next;
    };
    
    typedef struct hdr header;
    
    void *myMalloc(size_t size);
    void myFree(void *p);
    static header *moreMem(unsigned int);
    
    #endif
    My functions:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "Functions.h"
    
    static int msize = 1024;
    static header head;
    static header *fblock = NULL;
    
    void *myMalloc(size_t size) {
        header *prev, *p;
        moreMem(msize*sizeof(size_t));
    
        if ((prev = fblock) == NULL) {
            head.next = fblock = prev = &head;
            head.len = 0;
        }
            
        for (p = prev->next; ; prev = p, p = p->next) {
            if (size != 0) {
                if (p->len >= size) {
                    if (p->len == size) {
                        prev->next = p->next;
                    }
    
                    else {
                        p->len -= size;
                        p += p->len;
                        p->len = size;
                    }
                    
                    fblock = prev;
                    p->allocated = 1;
                    return (void*) (p+1);
                }
            
                if (p == fblock) {
                    if (p == NULL) {
                        return NULL;
                    }
                }
            }
            
            else if (size <= 0) {
                return NULL;
            }
        }
    }
    
    static header *moreMem(unsigned int size) {
        char *ptr;
        header *mem;
        if (size < msize) {
            size = msize;
        }
    
        ptr = malloc(size * sizeof(header));
    
        if (ptr == (char*)-1) {
            return NULL;
        }
    
        mem = (header*)ptr;
        mem->len = size;
        free ((void*)mem+1);
        return fblock;
    }
    
    void myFree(void *p) {
    
    }
    Any feedback is appreciated.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Why are you casting <free ((void*)mem+1);> to void* here? As far as I know you can use free ony any type of previously malloc-ed pointer.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Pretty sure that casts have a much higher precedence than addition so you are trying to do arithmetic on a void pointer. (I amnot sure if this is what claudiu was getting at or not)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > return (void*) (p+1);
    > free ((void*)mem+1);
    You need to watch your pointer arithmetic.

    Strictly speaking, arithmetic on a void* pointer is undefined (what size is void?)

    My guess is your round-trip arithmetic fudging isn't working.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by DeadPlanet View Post
    Pretty sure that casts have a much higher precedence than addition so you are trying to do arithmetic on a void pointer. (I amnot sure if this is what claudiu was getting at or not)
    Yep. I was just surprised he said that the program works fine on another machine.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    19
    If I take off the +1 it gets rid of the error, but then it freezes on some function, and I have no way of finding out where it's messed up because gdb wont work for some reason.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Questions about pointers and malloc
    By H`eya in forum C Programming
    Replies: 4
    Last Post: 04-19-2010, 07:50 PM
  2. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  3. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  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, 09:08 AM