0

I am working on a program where a block of memory is allocated using malloc and pointers are used to add information to the block of memory. I am using an array of pointers since the number of pointers is dependent on the size of the block but I am running into some issues whenever I'm adding the information to the block. I had to shorten my code but basically this is what it looks like

struct Header{
    int free;
    int size;
};

void* memory_allocator(int length){

    void* memoryBlock = malloc(length);

    //assuming that length is a multiple of 2
    int index = log2(length);

    Header** freeList = (Header**)malloc(sizeof(Header)*(index+1));
    freeList[index] = (Header*) memoryBlock;
    freeList[index]->size = length;
    freeList[index]->free = 1;

//Divide the memory block into chunks... This is where the problem happens
    for(int j=1; j <= index; j++){

        length = length/2;

        freeList[index-j] = (Header*)(((char*)freeList[index])+length);
        freeList[index-j]->size = length;
        freeList[index-j]->free = 1;
    }
}

My problem starts to happen at the for loop; it works fine at the first iteration but whenever it gets to the second it shoots a segmentation fault. The number I have been using to test this is 512 if it helps. Anybody can point out what I'm doing wrong please?

mpromonet
  • 11,326
  • 43
  • 62
  • 91
msalem
  • 1
  • 1
  • 1
    Standard Warning : Please [do not cast](http://stackoverflow.com/q/605845/2173917) the return value of `malloc()` and family in `C`. – Sourav Ghosh Mar 20 '15 at 07:07
  • 2
    `sizeof(Header)` --> `sizeof( struct Header)` – Sourav Ghosh Mar 20 '15 at 07:08
  • 1
    no `return` while function definition says `void*`. Please create a __meaningful__ ___[MCVE](http://stackoverflow.com/help/mcve)___. – Sourav Ghosh Mar 20 '15 at 07:14
  • Are you sure you would like to have 'length = length/2;' inside the loop... length is quickly 0 – mpromonet Mar 20 '15 at 07:18
  • @Sourav Ghosh Right, forgot to mention this was compiled with g++; forgot the return, sorry; if anything, i never got why some structs are written with the keyword before the type (i.e struct Header as opposed to Header). Whats the difference? – msalem Mar 20 '15 at 18:21
  • @mpromonet Yeah, while trying to simplify my code I suppose I created some bugs haha, let's just assume length can't go to zero – msalem Mar 20 '15 at 18:23

1 Answers1

1

The block you malloc is not initialized with NULL. You probably try to dereference an unititialized pointer.

Use calloc instead. The block will be initialized to NULL.

Header** freeList = calloc(index+1, sizeof(Header*));

You are also realocating the freelist for every block insertion. Is this really what you want ?

chmike
  • 20,922
  • 21
  • 83
  • 106
  • I though initializing the array without NULL was the problem so I did that with calloc and it still gives me that segmentation fault. The funny thing is that, while debugging, I saw that the last two index held the right information, address of block, size and free value, but when it tries to set the size information of the third that's when it shoots it. – msalem Mar 20 '15 at 18:17
  • And well i haven't worked too much with double pointers; What i am trying to do is just have the pointers in the array keep track of different spaces in the block, like the buddy memory allocation system, so I can use those later. – msalem Mar 20 '15 at 18:18
  • I would suggest you define freeList as an array of Header so that the header is not in the memory block. If you give the memory block to a user, he might overwrite the Header information. Regarding your segmentation fault, you could try to put parenthesis around freeList[index]: (char*)(freeList[index]). The cast could be applied to freeList and not freeList[index]. – chmike Mar 21 '15 at 07:40