|
|
|
|
![]() ![]() |
Dec 9 2005, 11:03 AM
Post
#1
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 407 Joined: 13-December 04 Member No.: 2,696 |
A little background about what I am doing is this:
I have an array which holds a linked list containing the memory that is currently taken from the system. Each index I have like 16 size memory or less can be stored only in index 0, index 1 is for memory inbetween 16-32, index 2 is for memory inbetween 32-64, etc all the way up to 2048 which is the max allocatable memory. Now the way my malloc is to work is that it first will run through my list to see if any memory is available that is greater than or equal to the size I need, if there is any at all it will return it. Then if the memory is too big it will split it in half and stick it into the freelist and repeat till the memory is the right size. Almost everything is working great except when I split my memory. My split memory function works fine if you split only once in a call to the malloc, if you split more than once it crashes. Im confused as to why though. Here is the function: Code: CODE metadata_t* split_memory(metadata_t* memory)
{ printf("Splitting the memory has been called \n"); printf("Old Memory Size is: %d\n",memory->size); printf("%p\n",memory); metadata_t* second = memory + (memory->size / 2); printf("%p\n",second-memory - memory->size/2); size_t temp = memory->size / 2; memory->size = temp; second->size = temp; printf("Unaccounted for space: %d\n",memory->size + second->size - temp * 2); printf("New Memory Size is: %d\n",second->size); memory->next = second; second->prev = memory; return memory; } |
|
|
|
Feb 1 2006, 05:15 AM
Post
#2
|
|
|
Newbie [Level 3] ![]() ![]() ![]() Group: Members Posts: 40 Joined: 29-January 06 Member No.: 17,841 |
I think in your code second->next is unassigned and is hanging. When you try to use it, it crashes. So,
before your CODE memory->next = second; you need to add CODE second->next = memory->next; making second->next point to next memory originally pointed by "memory". |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 13th October 2008 - 05:42 PM |