Skip to main content
gamedev.net
🔒 Locked

Split Dynamic Memory

Started by OleKaiwalker Mar 4, 2004 at 8:47 AM 3 replies 400+ views
Original Post
OleKaiwalker
OleKaiwalker
Hey, If I have allocated some dynamic memory... lets say int *pInt = (int*)malloc(32*sizeof(int)); And want to split it into smaller parts (yup Memory Management ) How can I come across that problem? Should I just free the allocated memory and then allocated it again in two pieces, where the sum of their size matches the size of the first block?? That wouldn''t create any fragmentation? If I try to visualize it I don''t see the fragmentation... Heap when the first block is allocated (A is first block and B something completly else) AAAABBB... Heap when the block is freed ....BBB... Heap when I reallocate in smaller chunks A1A1A2A2BBB... Any fragmentation?
pink panther
pink panther
I think there is fragmentation since memory management needs control structures that are put in front of the allocated memory. If that''s true for the kind of memory management you use, 2 seperatly allocated pieces eat up more space than one and don''t fit into the gap any more.
commander keen
commander keen
if you have "ptr" and you want to break it down to "ptr1" and "ptr2"

ptr = new int[100] ;
ptr1 = ptr ;
ptr2 = ptr + 63 ;

that would give you one pointer from element 1 and up and ptr2 would start from 63 being the first element.
Commander Keen
OleKaiwalker
OleKaiwalker
Is that the way to do it, when working with Memory Management?
RenderTarget
RenderTarget
It is if you want it to be. Memory management isn''t set in stone. If it makes sense, try it.

Most memory managers for games allocate a big heap once, and do their own management within it, generally by just offsetting the base pointer and tracking things.

I like pie.
[sub]My spoon is too big.[/sub]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.