Code: Select all
node **ptr = &list;
while(*ptr != NULL) {
if(*ptr == item3) {
*ptr = *ptr->next;
break;
}
ptr = &ptr->next;
}
Code: Select all
node **ptr = &list;
while(*ptr != NULL) {
if(*ptr == item3) {
*ptr = *ptr->next;
break;
}
ptr = &ptr->next;
}
@proxy: I'm not sure how this is doing it without traversing the list....And I dont see how I can do this Without traversing the list(or having a previous pointer)?
Just a consideration: your processor has a cache, which works by *lines* (32 bytes). When you request a dword, you usually get 8 for the price of 1 from the main memory ... Keeping things that are accessed together close from each others helps things keeping fasts.beyond infinity wrote: So I have a question:
What, if I only store a prev?ous/next pointer in the bookkeeping data, and the rest of information, esp.: Size of allocated chunk - at the end of the chunk?
If you only store a prev/next pointer in the bookkeeping data, you've already stored all the information you'll ever need for an allocated chunk. Why are your trying to use more bytes to store the size? The size is equal to ptr.next - ptr. Don't store anything at all at the end of the chunk. That's rather inconvenient, in addition to being completely unnecessary.beyond infinity wrote: @dreamsmith:
Hmmm ... your ideas/concepts are intrigueing (is that written correctly?)
So I have a question:
What, if I only store a prev?ous/next pointer in the bookkeeping data, and the rest of information, esp.: Size of allocated chunk - at the end of the chunk?
Yes, mostly. Calls to free() under this scheme are always O(1), and calls to malloc() are optionally O(1), if you start your "search" on the next higher freelist than you otherwise might. And even when you don't need O(1) allocations, it's still pretty damn fast (worst case performance would be O(n), but average case is going to be pretty short -- in fact, assuming random sized allocations and frees, on average you will examine 1.5 entries in the freelist before satisfying the request. If you frequently allocate and free blocks of a particular size, the average drops closer to 1. Still, when you NEED O(1), just start looking in the next higher list to guarentee your first hit is a good one. I wrote my malloc specifically to work in an environment where I had to be guarenteed a result in a fixed amount of time, then added the functionality to take a little longer [and fragment memory a bit less] when I could afford to).beyond infinity wrote:The previous/next pointers would render alloc and free operations O(1), if I understand this correctly - and if the free chunks are ordered in size ranges.