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?

6
  • 1
    Standard Warning : Please do not cast the return value of malloc() and family in C. Commented Mar 20, 2015 at 7:07
  • 2
    sizeof(Header) --> sizeof( struct Header) Commented Mar 20, 2015 at 7:08
  • 1
    no return while function definition says void*. Please create a meaningful MCVE. Commented Mar 20, 2015 at 7:14
  • Are you sure you would like to have 'length = length/2;' inside the loop... length is quickly 0 Commented Mar 20, 2015 at 7: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? Commented Mar 20, 2015 at 18:21

1 Answer 1

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 ?

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
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].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.