Catch up on stories from the past week (and beyond) at the Slashdot story archive

 



Forgot your password?
typodupeerror

Comment Memory leaks... (Score 4, Interesting) 326

Inside bitfast.h:
long *Tail = new long[65535];
long *Head = new long[65535];

for(n = 0; n < 65535; n++){ Head[n] = 0; Tail[n] = 0; }

Memory leak of 128KB for each time the function "Node* BitFast(Node* HeadNode,long run)" is called.

MISSING: delete[] Tail; delete[] Head;

Furthermore the following is faster or use memset:
long Tail[65535] = {0};
long Head[65535] = {0};

Unless you are running this in DOS16, DOS32 with Pharlap or smaller 8051, 68000 processor, you do not need to use new[] or malloc() for this.

In InsertToList, Node *tmpP = (Node*)malloc(sizeof(Node));
free(tmpP) missing inside FreeList()... no such function

In main.c:
Node *L1 = malloc(sizeof(Node));
Node *L2 = malloc(sizeof(Node));
MISSING: free(L1); free(L2);

In main.cpp:
Node *L1 = new Node;
Node *L2 = new Node;

Instead use (no need to use heap, use stack memory):
Node L1 = {0}, L2 = {0};

Mixing the usage of std::cout and printf() is not recommanded.
Use all of the former or the later, else you will have weird display on some run, unless you flush the output buffer. I suggest you use printf() all the way.
cout << "Ok!!!!" << "\n\n" << "Merge Sort : " << tim1 << " ms\n" << "BitFast : " << tim2 << " ms\n\n";
becomes:
printf("Ok!!!!\n\nMerge Sort : %ld ms\nBitFast : %ld ms\n\n", tim1, tim2);

Furthermore, your implementation of link list, and calling Length(Node*) for every equal is highly inefficient, requires O(n). Store the link list length somewhere when inserting. EqualSplit takes O(1.5n) instead of O(0.5n) because of that.

Something like:

typdef struct LinkList {
  Node  head;
  int   length;
} LinkList;

As a result, MergeSort recursively call itself, which calls EqualSplit every turn.

You should make your header file C friendly, and avoid mixing // with /* */, malloc and new[]

No clue why this header is needed:
#include <iso646.h>  instead of not/and use use ! and &&
before:  if(not p) return NULL;
becomes: if(!p) return NULL;

Finally, BitFast is not recursive; therefore, you should try to find an implementation of merge sort which is not recursive also, so you save on function calls, if possible.

"However, iterative, non-recursive, implementations of merge sort, avoiding method call overhead, are not difficult to code." -- Wikipedia

http://en.wikipedia.org/wiki/Merge_so rt

Your function should be in a file.c or be inline.

Slashdot Top Deals

"Only the hypocrite is really rotten to the core." -- Hannah Arendt.

Working...