dynamic memory allocation concerns
Posted: Sun Dec 26, 2004 12:19 pm
Hello!
I'm in the design phase for a project which will require support for any number of dynamically created objects (structures). Inside these structures will be strings. I'm trying to think of the best way to do this in PB.. as far as I see it my options are a) PB's linked lists commands, and b) manual memory allocation (using either PB's AllocateMemory, or the WinAPI's heap functions).
I sort of like the linked list idea, but with my recent discovery of the bug described here, I am hesitant to use them (are there any other bugs w/ linked lists that are known..?) I also may need to do nested linked lists (ie lists inside of lists), and AFAIK this is currently unsupported with native PB commands.
So this brings about some questions about rolling my own code to dynamically allocate an ever-changing amount of structures. I figure it can be done by doing something like:
*newStruct = AllocateMemory (SizeOf(myStruct))
However this brings some questions to mind. Namely, what will this do to structures that have strings in them? The size of a struct that has a string is constant (since regardless of the string's contents, the struct itself will contain a pointer for the string buffer), however the amount of memory actually being used by the string is prone to change (ie if the string is modified, etc)
What I am wondering is if FreeMemory (*newStruct) will therefore take this into account, and de-allocate the string's buffer as well? Or will it simply de-allocate the amount of memory equal to the size of the structure? (Leaving the string's contents somewhere in memory.. ie a memory leak)
And finally, is there any reason why PB's memory functions would not suffice for this? Would the WinAPI's heap functions offer any difference/advantage?
Thanks in advance for any help you PB gurus can offer
I'm in the design phase for a project which will require support for any number of dynamically created objects (structures). Inside these structures will be strings. I'm trying to think of the best way to do this in PB.. as far as I see it my options are a) PB's linked lists commands, and b) manual memory allocation (using either PB's AllocateMemory, or the WinAPI's heap functions).
I sort of like the linked list idea, but with my recent discovery of the bug described here, I am hesitant to use them (are there any other bugs w/ linked lists that are known..?) I also may need to do nested linked lists (ie lists inside of lists), and AFAIK this is currently unsupported with native PB commands.
So this brings about some questions about rolling my own code to dynamically allocate an ever-changing amount of structures. I figure it can be done by doing something like:
*newStruct = AllocateMemory (SizeOf(myStruct))
However this brings some questions to mind. Namely, what will this do to structures that have strings in them? The size of a struct that has a string is constant (since regardless of the string's contents, the struct itself will contain a pointer for the string buffer), however the amount of memory actually being used by the string is prone to change (ie if the string is modified, etc)
What I am wondering is if FreeMemory (*newStruct) will therefore take this into account, and de-allocate the string's buffer as well? Or will it simply de-allocate the amount of memory equal to the size of the structure? (Leaving the string's contents somewhere in memory.. ie a memory leak)
And finally, is there any reason why PB's memory functions would not suffice for this? Would the WinAPI's heap functions offer any difference/advantage?
Thanks in advance for any help you PB gurus can offer
