• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Phil123

Industrial Strength Hash Table

16 posts in this topic

I've been enjoying the coding horrors forum for a while, so I decided to post a snippet of code that I found in a large c++ project that I'm (unfortunately) a part of.  Normally I wouldn't poke fun at code like this, but I couldn't resist this one.

 

Enjoy.

#pragma once

#include "Includes.h"



template<class T>

class Dictionary

{

private:

    std::vector<std::string> keys;

    std::vector<T> values;

public:

    Dictionary()

    {

        keys = new std::vector<std::string>{};

        values = new std::vector<T>{};

    }



    ~Dictionary()

    {

        delete[] keys;

        delete[] values;

    };



    std::vector* Values() { return &values; };

    std::vector* Keys() { return &keys; };



    size_t size() { return keys.size(); }



    void Add(std::string _key, T value)

    {

        keys.push_back(_key);

        values.push_back(value);

    };

    void Remove(std::string _key)

    {

        for (size_t i = 0; i < keys.size(); i++)

        {

            if (keys.at(i) == _key)

            {

                keys.erase(keys.begin() + i);

                values.erase(values.begin() + i);

                break;

            }

        }

    };

    T Get(std::string _key)

    {

        for (size_t i = 0; i < keys.size(); i++)

        {

            if (keys.at(i) == _key)

            {

                return values.at(i);

            }

        }

        return nullptr;

    };

};

0

Share this post


Link to post
Share on other sites

Does that code even compile? In what compiler?

 

In every compiler.

 

 

 

 

As long as you don't use that template.

1

Share this post


Link to post
Share on other sites


So let's see what the WTFs basically are...

 

I would add:

 

- Creating the std::vectors with new, where they would better be created on the stack. Its not like one of the vectors benefits is that it takes away the memory management for dynamic arrays.

 

- Returning both vectors as non-const pointer, basically breaking every concept of encapsulation of that class. Especially hard since both vectors are meant to store dependant values for one entry, imagine someone deleting values out of only the Value-vector.

 

- Also, returning the vectors by pointer instead of reference when they can never be nullptr.

1

Share this post


Link to post
Share on other sites

Method names begin with a capital letter

 

Hey! What's wrong with this? rolleyes.gif

 

I would like to add one.  How about every line being spaced with the beginning of each function being double spaced.  That might be "personal" preference, but it is a hell of an annoying one.

 

 

Does that code even compile? In what compiler?

 

In every compiler.

 

 

 

 

As long as you don't use that template.

 

 

Hehehehe.

EDIT: Or the fact that every function is terminated with a semicolon.  Again, personal preference; hell of an annoying one.

Edited by ByteTroll
0

Share this post


Link to post
Share on other sites

 

Does that code even compile? In what compiler?

 

In every compiler.

 

As long as you don't use that template.

 

Not instantiating a template doesn't prevent the first compilation pass, which will catch syntactical errors such as:

std::vector* Values() { return &values; }; // invalid use of template-name "std::vector" without an argument list
Edited by TheComet
0

Share this post


Link to post
Share on other sites


EDIT: Or the fact that every function is terminated with a semicolon.  Again, personal preference; hell of an annoying one.

WTF I didn't notice that at first.

0

Share this post


Link to post
Share on other sites
Using delete[] on objects created with new and not new[]

Relax, it is much worse than you think.laugh.png

 

delete[] is being called on objects which are not explicitly allocated via new at all. The vectors that are allocated on the heap are copied (if the compiler accepts this code at all!) and then leaked. I wonder how you can assign a pointer-to-vector to a vector though, since to my knowledge it only accepts const T& and T&& and std::initializer_list<T>.

 

Both keys and values are objects, not pointers, and part of the Dictionary object. Which means that you call delete (with or without brackets, who cares) on an object that is either allocated on the stack or inside a larger heap-allocated object. In either case, this is very bad mojo.

 

The Dictionary class has no virtual functions and keys is the first member, so likely (void*) keys == (void*) dictionary_object

Which means if you somehow get the compiler to accept this code (I would be surprised, though), it would double-delete the Dictionary object, once explicitly and once from its own destructor via a type-punned std::vector pointer.

Edited by samoth
1

Share this post


Link to post
Share on other sites


EDIT: Or the fact that every function is terminated with a semicolon. Again, personal preference; hell of an annoying one.

 

Haha, but get this, only some functions are terminated with a semicolon.  Others aren't.

0

Share this post


Link to post
Share on other sites

This looks like Java code that someone tried to port without an actual understanding of C++.

That reminds me of a joke which has become decidedly unfunny on my current project:

 

What's worse than C++ written by a Java developer?

 

[spoiler]Java written by a C++ developer.[/spoiler]

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0