1

I have function like this for delete my 2 dimensional struct, but it is not too fast, i guess there is a much faster way to do this(Like memset or something), any idea would be appreciated;)

 void freeAlllistNode(LISTNODEPTR *sPtr[][10])
{   LISTNODEPTR temp;
for (char i = 0; i<19; i++){
    for (char di = 0; di<10; di++){
        while (sPtr[i][di] != NULL){
            temp = *(sPtr[i] + di);
            *(sPtr[i] + di) = temp->next;
            free(temp);
        }
    }
  }
}

And this is my struct definition, in case if it's necessary

typedef struct listNode{
char* val ;
struct listNode *next;
}LISTNODE;
using LISTNODEPTR = LISTNODE*;
Shaheen Zahedi
  • 1,216
  • 3
  • 15
  • 40
  • 2
    No, there is no faster way to do it. You have so many dynamically allocated objects to delete. This is fundamental. If you have hundreds of objects to delete, you have hundreds of objects to delete. There are no shortcuts. If this is turning out to be a performance problem, then the answer is to redesign your data structures to avoid having to create hundreds of dynamic objects in the first place. – Sam Varshavchik Jul 19 '16 at 11:03
  • Not cause of your problem, but I see a free(): do you really allocate your objects with malloc() ? Should be new/delete. – Christophe Jul 19 '16 at 11:11
  • do i really need to iteraate through every object ;( , i mean is there something like memset, that i call with size, and delete – Shaheen Zahedi Jul 19 '16 at 11:23
  • You could just leak memory instead. – Jon Chesterfield Jul 19 '16 at 12:57
  • @Jon Chesterfield I just thinking of that ;) , for my project it is not worth the time – Shaheen Zahedi Jul 19 '16 at 14:21
  • @ShaheenZahedi if dev time is a concern, you could save loads of it by using std::list> instead of rolling your own – Jon Chesterfield Jul 19 '16 at 14:26
  • @Jon Chesterfield thank you, i will keep that in mind, i have so much code which i implemented with linked list, is there much of a difference? , i just trying to figure out, is it worth the time to re-implement all of the codes with std::list> ? – Shaheen Zahedi Jul 19 '16 at 14:31
  • @ShaheenZahedi I think you'll be surprised by how much of your code disappears when using the standard containers. Broadly recommended instead of rolling your own C-style objects, unless you really enjoy implementing containers. – Jon Chesterfield Jul 19 '16 at 14:35
  • @JonChesterfield oh really? ,that is niiiiiice, no i never enjoyed the headache with linked list and pointers and all of that, i just want it to be fast, some says that linked list is fast, but what you implying is std::list> is way faster than linked list, am i correct? – Shaheen Zahedi Jul 20 '16 at 08:43
  • std::list is a linked list. A linked list of linked lists is unlikely to be fast unless you've done really clever things with the implementation though - it's too cache-unfriendly. Perhaps it would be worth posting a new question along the lines of "I am doing this, with this code, but it takes far too long - should I change the core datastructure?" – Jon Chesterfield Jul 20 '16 at 10:23

1 Answers1

4

No. memset is never a replacement for malloc/free, in any circumstance. They do completely different things. A proposal that memset could somehow be a replacement for malloc/free is a fundamental misunderstanding of how dynamic memory, and dynamic scope allocation works.

The only thing that could even remotely implement something like that would be standard C++ library containers with a custom allocation class. That is, std::list, std::vector, and others, and none of this manual link list implementation.

Standard C++ library containers take an optional template parameter that specify a custom allocator class. A custom allocator class would have to be written that manages dynamic memory allocation for the container, something along the lines of allocating large chunks of memory, and then doling them out, piecewise, for each value placed into the container. Then, when the container gets destroyed, all allocated memory could be disposed of in a few, short, deletes.

It would, of course, be possible to also implement this approach with a manual link list implementation. But it's a lot of work anyway, and as long as all that work has to be done, why waste any more time with a manual link list, just use std::list.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148