Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Programming A Malloc
kvarnerexpress
post 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;
}
Go to the top of the page
 
+Quote Post
kvkv
post 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".
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. A Question About C++ Programming Under Linux(7)
  2. C Most Popular Programming Language(3)
  3. D Programming Language(9)
  4. Game Programming(17)
  5. Good Books On C++ Game Programming(2)
  6. Where Can I Learn Linux Programming?(2)
  7. Programming Help(7)
  8. Life In Programming?(11)
  9. Vesa Programming Viewer In C(2)
  10. Vesa Programming Viewer In D O S(3)
  11. Never Ending Books For C++ Programming(3)
  12. Where To Start Learning C++ Programming Language(4)
  13. C Problem: "two Or More Data Types In Declaration"(6)
  14. Win32: Dialog Box And Accelerator(2)
  15. 256-color Vga Programming In C Site(0)
  1. Dos Game Programming In C For Beginners(1)
  2. Dos Programming, Undocumented Dos, And Dos Secrets(3)
  3. Detailed C Beginner Tut(2)
  4. C/c++ Programming Experince(3)
  5. Beginners Guide To C/c++ Programming?(3)
  6. C Programming: Arrays(2)
  7. How Many of You Use the C Programming language?(23)
  8. C Or C++ Easy Programming Generator(6)
  9. C Programming Video Tutorials(3)
  10. Discussion On Dynamic Programming(3)
  11. Alright, I'm Taking Computer Programming As A Class In School.(10)
  12. Why Must We Learn Object Oriented Programming(4)
  13. Finding The Rgb Color Of An Image(3)


 



- Lo-Fi Version Time is now: 13th October 2008 - 05:42 PM