Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 02 Oct 2013
Offline Last Active Jul 21 2016 01:18 PM

#5248981 Outputting to a file

Posted by TheComet on 26 August 2015 - 07:40 AM

Reminds me of the time my antivirus decided to delete all DLL files in my python site-packages while I was debugging. "Unable to call function: Function was deleted from your computer"

#5245705 Worst Hotfixes

Posted by TheComet on 11 August 2015 - 05:26 AM

In this thread we share stories of some of the worst hotfixes we've seen and/or applied.


In a game I had to present I was experiencing an extremely obscure bug where after some time pointers would randomly point to garbage values, crashing the game. I had an hour to get it working before the presentation.


I spent 45 minutes trying to reproduce it with no success. It happened at seemingly random times, but for some reason it was always the same two pointers that were modified.


Seeing as I was running out of time, I ended up inserting checks which would replace the garbage value (when it occurred) with the correct value again - the correct value I knew because I saw it in the debugger and it seemed to remain consistent.

if(game->settings_doc != 0x63e1b0)
    game->settings_doc = 0x63e1b0; /* from debugger */

After the presentation I sat down with valgrind and found the problem. A buffer overrun was writing into memory it wasn't supposed to.

#5245674 Can someome please tell me why this crashes?

Posted by TheComet on 11 August 2015 - 02:01 AM

If you happen to have a cygwin shell installed (e.g. if you installed git) you could also just use:

find /c/Users/admin/Documents -name "*.mp4" -exec basename {} \; > video_filenames.txt

Maybe someone can translate that to the Windows cmd equivalent.

#5245518 Breaking out of a nested loop

Posted by TheComet on 10 August 2015 - 11:53 AM

Why is my thread now in coding horrors biggrin.png What have you people done to my beautiful thread?

#5244395 Constructor Initializer List and Heap Allocation

Posted by TheComet on 03 August 2015 - 04:30 PM

Also note that if the allocation fails and you have more than one heap allocation in the initializer list (and said allocation throws std::bad_alloc or you throw an exception in the constructor body), you've got yourself a nice memory leak, because the destructor will never get called.

class Test
    Model* model1;
    Model* model2;
    Test() :
        model1(new Model),
        model2(new Model)  // this allocation fails

    ~Test()  // this will never get called
        if(model1) delete model1;
        if(model2) delete model2;

You're on the safe side if you use smart pointers.

class Test
    std::unique_ptr<Model> model1;
    std::unique_ptr<Model> model2;
    Test() :
        model1(new Model),
        model2(new Model)


#5241733 How is cross platform done

Posted by TheComet on 21 July 2015 - 11:20 AM

That's about as hard to answer as "how do people cook."

Most of it boils down to [...]

I see what you did there

#5238807 Multiple heritage on python

Posted by TheComet on 07 July 2015 - 10:21 AM

Still, it does not answer my question if super() is correct and how it works.


super() finds the next method in the MRO at each step. The proper way to call the base class __init__ methods is for every class to call super(), like this:

class A(object):
 def __init__(self):
  super(A, self).__init__()
  print('A init')

class B(object):
 def __init__(self):
  super(B, self).__init__()
  print('B init')

class C(A, B):
 def __init__(self):
  super(C, self).__init__()
  print('C init')

if __name__ == '__main__':
 c = C()

Perhaps this piece of code can shed some light:

class A(object):
 def foo(self):
  print('foo from A')

class B(object):
 def foo(self):
  print('foo from B')

class C(A, B):
 def foo(self):
  print('foo from C')

If we print out the MRO (method resolution order) of class C, we see the following order:

>>> C.__mro__
(<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>)

That's [C, A, B, object].


If we instantiate class C and call foo(), we get the expected output "foo from C":

>>> instance = C()
>>> instance.foo()
foo from C

super() can be used to call foo() from a base class. By specifying C as the class type, super() will return the next object in line in instance's MRO.

>>> instance.__class__.__mro__
(<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>)
>>> super(C, instance).foo()
foo from A

Because instance is of type C and C was specified in super(), A is the next in line in [C, A, B, object].
By specifying A instead of C in super(), the next object after A in line in instance's MRO will be returned:

>>> super(A, instance).foo()
foo from B

Because instance is of type C and A was specified in super(), B is the next in line in [C, A, B, object].


By specifying B to super(), the next in line is object, which of course doesn't have the method foo():

>>> super(B, instance).foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'super' object has no attribute 'foo'

#5238677 Multiple heritage on python

Posted by TheComet on 06 July 2015 - 04:23 PM

This answers your question: http://stackoverflow.com/questions/3277367/how-does-pythons-super-work-with-multiple-inheritance

#5237707 Unit setup

Posted by TheComet on 30 June 2015 - 11:55 AM

It's arbitrary. 1 meter can be 1 unit, but 1 meter could also be 45.7 units. It depends on how everything is scaled relative to everything else.

#5237335 Memory management patterns in C++

Posted by TheComet on 28 June 2015 - 03:50 PM

Have you tried to make a garbage collector or used reference counting?


Have you tried it?


"There will be a lot of bugs": Well yeah, but you can say that about anything. The arguments you're using have no sustenance. They are just opinions based on nothing.

#5232923 Copy file/folder cross-platform

Posted by TheComet on 05 June 2015 - 04:37 AM

You could do something as simple as:


#include <iostream>
#include <string>
#include <stdlib.h>

int copy_directory(std::string a, std::string b)
#if defined(_WIN32)
    return system(std::string("copy " + a + " " + b).c_str());
#elif defined(__APPLE__) || defined(__linux__)
    return system(std::string("cp -r " + a + " " + b).c_str());
#   error copy_directory does not support this OS!

int main()
    if(copy_directory("a", "b"))
        std::cout << "something went wrong" << std::endl;
    return 0;


If you want more control and don't want to use any external libs then you'll have to write multiple implementations using the filesystem API for each platform you want to support. In my project, I placed each implementation in a separate source file and had one common header file. Depending on the platform, the correct source file is compiled.

#5219814 Why didn't somebody tell me?

Posted by TheComet on 28 March 2015 - 08:28 AM

It's called the alphabet because the first two greek letters are "alpha" and "beta" --> "alpha" + "bet"

#5218686 c++ count lines in txt file and then read these lines without reopening a file

Posted by TheComet on 23 March 2015 - 11:21 PM

@WiredCat - This might be interesting to you: http://insanecoding.blogspot.ch/2011/11/how-to-read-in-file-in-c.html

Speed test of various file reading methods in C++.




and to people that said use vector no i wont because i dont want to recreate the array almost every line.

Vectors don't reallocate every line... They (usually) reallocate log(n) because they expand exponentially. If you read 5000 lines, the vector will reallocate ~12 times. Look, here's an example:

int main()
    std::vector<int> v;
    for(int i = 0; i != 5000; ++i)
    return 0;


$ valgrind ./test                       
==5628== HEAP SUMMARY:
==5628==   total heap usage: 14 allocs, 14 frees, 65,532 bytes allocated

See? 14 allocations, even though I pushed 5000 elements into it. That's not "every line".


Here's my fancy solution (C++11):

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
#include <iterator>

class Line
    std::string data;
    friend std::istream& operator>>(std::istream& is, Line& l)
        std::getline(is, l.data);
        return is;
    std::string operator() const { return data; }

std::vector<std::string> get_file_lines(std::string file_name)
    std::ifstream in(file_name.c_str(), std::ios::in);
    return std::vector<std::string> {std::istream_iterator<Line>{in},

int main()
    auto lines1 = get_file_lines("file.txt");
    return 0;



#5217690 Doge Indirection

Posted by TheComet on 19 March 2015 - 12:42 PM

Hackers have been around for too long, usually associated with dark themes and “the evil side” they represent the minority of IT Gurus that just have too much time on their hands, and a touch of evilness. Usually represented with a skull or “horrifically” they are iconic in a never ending battle against internet crime.
For this reason, I encourage all of us to start utilising the technique of indirection for storing all primitive datatypes in our programs. Not only will this make it impossible for hackers to track our variables, it will also allow us programmers to taste the sweet taste of having stopped said hackers with a doge meme.
#include <iostream>

template <class T, int N>
struct Indirection
    typedef typename Indirection<T, N-1>::type* type;
    typedef typename Indirection<T, N-1>::type  child_type;
    static void init(type& p)
        p = new child_type;
        Indirection<T, N-1>::init(*p);
    static void cleanup(type& p)
        Indirection<T, N-1>::cleanup(*p);
        delete p;

template <class T>
struct Indirection<T, 0>
    typedef T type;
    static void init(type& p) {}
    static void cleanup(type& p) {}

int main()

    /* there happen to be 272 stars in the doge below */
    Indirection<int, 272>::type cpu_cache_will_enjoy_this;
    Indirection<int, 272>::init(cpu_cache_will_enjoy_this);
     * set the value of our variable using the power of the shibe

         *              *    
        * *           ** *     *         *     ****     *         *
        *  *        **   *     *         *   *      *   *         *
       ***  ********     *      *       *   *        *   *       *
     ***           *  ** *      *   *   *   *        *   *   *   *
   **               **** *       * * * *     *      *     * * * *
  *   **              **  *       *   *        ****        *   *
  *  ***     ****       * *  
 *           ****        *** 
 *  ****                   * 
* ******** *               * 
*  ****  ** *               *          **              *
*   ****   *               *          *                *
 *      ***                *           **   *  *   **  * **
 *                     *  *              *  *  *  *    **  *
  **                 *    *            **    **    **  *   *
    **          ****    **   
      **********     ***                     *            *  *                *   *
                   **                                     *                  ***
                                             *  * **   ** *  *  ***  **   **  *   *   **   * **
                                             *  *   * *  **  *  *   * ** *    *   *  *  *  *   *
                                             *  *   *  ** *  *  *    **   **   *  *   **   *   *

    cpu_cache_will_enjoy_this = 20;
     * now output the value to stdout
    std::cout << " the value is: " << ****************************************** // make absolutely sure not to go over the 80 character per line limit, as to not enrage The Powers That Be (Linus Torvalds) in the mailing list
    ********************* ******************************************************
    ***cpu_cache_will_enjoy_this << std::endl;
     * clean up like good programmers - no memory leaks, check it with valgraind
     * if you don't believe
    Indirection<int, 272>::cleanup(cpu_cache_will_enjoy_this);
    return 0;
Granted, the error messages can become a little bit confusing, but all you have to do is write a batch script to parse the error output in order to understand how many stars you forgot:
/home/thecomet/documents/programming/test/main.cpp: In function ‘int main()’:
/home/thecomet/documents/programming/test/main.cpp:81:61: error: no matching function for call to ‘Indirection<int, 273>::cleanup(Indirection<int, 0>::type********************************************************************************************************************************************************************************************************************************************************************************&)’
     Indirection<int, 273>::cleanup(cpu_cache_will_enjoy_this);
/home/thecomet/documents/programming/test/main.cpp:81:61: note: candidate is:
/home/thecomet/documents/programming/test/main.cpp:15:17: note: static void Indirection<T, N>::cleanup(typename Indirection<T, (N - 1)>::type*&) [with T = int; int N = 273; Indirection<T, N>::type = int*********************************************************************************************************************************************************************************************************************************************************************************; typename Indirection<T, (N - 1)>::type = int********************************************************************************************************************************************************************************************************************************************************************************]
     static void cleanup(type& p)
/home/thecomet/documents/programming/test/main.cpp:15:17: note:   no known conversion for argument 1 from ‘Indirection<int, 272>::type {aka int********************************************************************************************************************************************************************************************************************************************************************************}’ to ‘Indirection<int, 0>::type*********************************************************************************************************************************************************************************************************************************************************************************& {aka int*********************************************************************************************************************************************************************************************************************************************************************************&}’
CMakeFiles/indirection.dir/build.make:54: recipe for target 'CMakeFiles/indirection.dir/main.cpp.o' failed

#5215724 Best programming paradigm for noobs?

Posted by TheComet on 10 March 2015 - 04:53 PM

If a noob, who knows nothing about computer programming, wishes to embark on the painful journey of developing software, what is the best paradigm to start with and why? Procedural? OO? Functional?
I've been thinking about this lately because at college Java is the first thing to be taught and I noticed there to be a lot of confusion among the students. Those who have never been close to computer code are having an extremely hard time grasping the concepts. Those who have programmed before are having less of a hard time, but are still utterly confused (try explaining to them what the "this" keyword does, for instance).
I personally started out with BASIC when I was 14, and much much later I decided to learn about OO. I think it's a lot easier to learn a procedural language before learning OO.
What do you think?