Iterators Vs. Indexers

In C++ STL containers specifically (i.e. vector), you’ve three methods for accessing data inside it, using iterators, indexers or at operator.So, the question is, when accessing data inside a container, which method to use?

I’ve read this article and this one also. It was very useful. Below are useful quotes from it.

Using an iterator results in incrementing a pointer (for incrementing) and for dereferencing into dereferencing a pointer.
With an index, incrementing should be equally fast, but looking up an element involves an addition (data pointer+index) and dereferencing that pointer, but the difference should be marginal.

Benchmark results for 500M iterations, vector size 10, with gcc 4.3.3 (-O3), linux x86_64:
at(): 9158
operator[]: 4269
iterator: 3914ms

I think the only answer could be a test on your platform. Generally the only thing which is standardized in the STL is the type of iterators a collection offers and the complexity of algorithms.

I would say that there is no (not much of a difference) between those two versions- the only difference I could think of would be tjat the code has to iterate through the whole collection when it has to compute the length of an array (I’m not sure if the length is stored in a variable inside the vector, then the overhead wouldn’t matter)

Accessing the elements with “at” should take a little bit longer than directly accessing it with [] because it checks if you are in the bounds of the vector and throws an exception if you are out of bounds (it seems [] is normally just using pointer arithmetic – so it should be faster)

The most you gain from using an iterator is that your code is ready for nearly all STL-containers. So if you decide your vector should have been a list, you can simply switch the declaration, and need not touch all the places where you iterate through the list (especially if you used a typedef).

Chapter #1 – Object Oriented Programming using C++

An item specified in terms of operations is called abstract data type.

A class is a piece of software that indicates data specification and functions operating on these data and possibly on the data belonging to class instances.

Pointers are variables leading to variables.

Dangling pointers (or wild pointers) in computer programming are pointers that do not point to a valid object of the appropriate type.

T o avoid the dangling pointers problem, always assign NULL to the pointer after deleting it (p = 0 or p = NULL). This is called NULL pointer (not p refers to NULL).

Always defines an explicit copy constructor when you’ve pointers in your class.

STL was developed by Alexander Stepanov and Meng Lee.

STL contains 3 types of generic entities: containers, iterators and algorithms.

A container is a data structure that holds some objects which are usually of the same type.

STL Containers: deque, list, map, multimap, set, multiset, stack, queue, priority_queue and vector.

An iterator is an object used to reference an element stored in a container (generalization of pointer).

No iterators are supported in stack, queue and priority_queue containers.

Check functional header to be used with STL algorithms.

reinterpret_cast is used to cast pointer objects where static_cast is used to cast usual objects. Check this article.

Introduction to Unreal Script

  • Ways to add additional content to Game Engine:
    • Write code in the same language (native code) that the game engine was written in.
      • Advantages
        • Speed of execution.
        • Familiar development environment.
        • Easier path of integration with code.
      • Disadvantages:
        • The entire game or portion of the game would have to be recompiled which can be an extremely lengthy process.
        • With longer compile times, testing game content and making tweaks to behavior becomes a very time consuming.
    • Use a separate language from our game engine that is executed by the game engine itself.
      • Advantages:
        • Solves the problem of inflexibility of native code.
      • Disadvantages:
        • Comes at a heavy performance cost.
          • When code has to be interpreted by the game engine at runtime, the computer is not going to be able to do as much processing as a sophisticated game might need.
    • Use a mixture of the first two methods.
  • Unreal Engine has Virtual Machine!
  • Regardless of how we get the scripts running, the compiled scripts are executed by the Unreal Virtual Machine, which is just a name given to several components which all are involved in the running of the compiled scripts. These components include the server, the client, the rendering engine, and the engine support code. Together these components translate the bytecode contained within the compiled scripts into the result the player sees on screen during play.

    When to use Unreal Script

  • So, when is it best to use UnrealScript and when is it better to use native code? As a general rule, scripts should be designed to be idle the majority of the time. In essence, the code in an UnrealScript should only be run after the firing of some specific event. Take a health pickup, for example. This object will constantly be polling to see if something touches it. Then, once it finds a touching object, some specialized functionality will be applied to that object. It simply would not make sense for the polling to be done in UnrealScript as that would bog down the system and thus slow down the game. Instead, a special function, or event as the case may be, would be written in UnrealScript that would only handle the functionality that is to be performed on touching objects as they are found. This function would be called from the native code which would be handling the polling. This is a great example of how native code and UnrealScript can work together to make your game efficient.
  • This is not to say that there are not situations where scripts will need to perform some functionality every tick or every few ticks. In fact, there are several cases where this is absolutely necessary, especially if you are unable to write your own native code for your class. In these situations, the code inside of the Tick() function will need to be optimized as much as possible and only perform essential operations. In some cases, the code in the Tick() function may only need to be run every so often. Adding up the elapsed time each tick and only executing the code when that time reaches a specified amount is a good way to keep the system from slowing down.

Chapter 12 & 13 Object-Oriented Programming Inheritance & Polymorphism

Polymorphism, Virtual Functions and Dynamic Binding “Under the Hood”