Code Monkey home page Code Monkey logo

cdds's Introduction

CDDS

cdds's People

Contributors

ryantheinventor avatar

Watchers

 avatar

cdds's Issues

Copy assignment operator should clean up/overwrite existing data

CDDS/CDDS/tDoubleLinkList.h

Lines 129 to 138 in 4294dc4

tDoubleLinkList<T> &tDoubleLinkList<T>::operator=(const tDoubleLinkList &rhs)
{
Node *cur = rhs.head;
while (cur != NULL)
{
push_back(cur->data);
cur = cur->next;
}
return *this;
}

Since a list being assigned to may already have existing nodes, you should take care to them up first before overwriting the head and tail variable on the list with new nodes duplicated from the other list.

Potential deference of null pointer in binary tree dtor and clear

template<typename T>
tBinaryTree<T>::~tBinaryTree()
{
//remove all nodes
rVertexDel(*root);
delete root;
}

root is initialized to nullptr on construction, which is proper and valid behavior. However, the destructor always deferences root, which can be null if no nodes were added/all nodes were removed.

Deferencing a null pointer is UB; it crashes with MSVC14.

Repro w/ the following code:

#include "tBinaryTree.h"

int main() 
{
	// construct val in a new "scope"
	{
		tBinaryTree<int> val;
	}
	// val will be destroyed before moving past this point

	return 0;
}

Potentially undefined use of NULL

Node *next = NULL;// pointer to node following this node
Node *prev = NULL;

Use of NULL is serviceable, but nullptr is preferred for coursework. Additionally, this header alone does not define NULL so use of this may result in compilation errors since NULL may go undefined as a macro or identifier. While nullptr is again preferred, other options include...

  • defining the NULL macro yourself at the top #define NULL 0
  • including #include <cstddef> at the top which defines NULL (see cppref)
  • using nullptr, a keyword providing a pointer literal for a null pointer ๐Ÿ˜ƒ (see cppref)

The use of nullptr is preferred in contexts like UE4 and also avoids any problems that may stem from accidentally providing a pointer where a numerical value is expected.

Pre-operators should return references to the iterators being incremented

iterator operator++() // pre-increment (returns a reference to this iterator after it is incremented)
{
cur = cur->next;
return *this;
}

iterator operator--() // pre-increment (returns a reference to this iterator after it is incremented)
{
cur = cur->prev;
return *this;
}

The implementations for your pre-inc/pre-dec operators should return references. As they are now, they are only returning copies of themselves.

To make this change, alter the return type of those operators such that they return iterator & where the & denotes that the return type will be a reference rather than a plain value type.

A more idiomatic way of writing !=

bool operator!=(const iterator &rhs) const// returns false if the iterator does not point to the same node
{
return !operator==(rhs);//No clue if this works
}

This does work! You may want to rewrite instead as...

return !(*this == rhs);

.. which may be more idiomatic.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.