Thursday, July 10, 2014

Imperative Spaghetti Programming

I was really bored one day and I decided to post this piece of art on Code Review. It is in the process of being deleted because it's too beautiful for the site.

Tested with Visual Studio 2012.

#include <iostream>
using namespace std ;

const int size = 3 ;
const char * names [] = {"Bob", "Alice", "Eve"} ;
int ages [] = {35, 28, 22} ;

int main (void)
{
    int *i = new int ;
    *i = 0 ;

    bool add_ages_please = true ;

Print:
    if (size <= *i) {
        if (add_ages_please)
            goto AddAges ;
            goto CleanUp ;
    }

    cout << names [*i] << " " << ages [*i] << "\n" ;
    ++(*i) ;
    goto Print ;

AddAges:
    --(*i) ;

    ages [*i] += 1 ;

    if (0 < *i) {
        goto AddAges ;
    }

    add_ages_please = false ;
    goto Print ;

CleanUp:
    delete i ;
    i = nullptr ;

    return 0 ;
}
 
"I use nothing but the very best modern
 programming practices. This is mission-critical production quality 
code. The fact that nobody has been able to suggest any improvements is a
 testament to its quality."

Modified Version of Russian Roulette

I feel like I found the base of this code so on some Powershell tutorial, but I'm not sure. Anyways, I've modified how the game plays out because I thought going from one chamber to the next was a bit boring. Instead, you rotate the barrel (or whatever you call it) to land on a random chamber. For the sake of the demo ending quickly, I made it so that it would always be a new random chamber that you did not previously land on.

I'll probably modify this a little bit to make it interactive and have multiple players playing. Then I'll post it on Code Review Stack Exchange.

Tested on Powershell v2

$seed = Get-Date
[int]$bullet_chamber = Get-Random -Minimum 1 -Maximum 6 -SetSeed $seed.Millisecond
[int]$trigger_pulls = 0
$vecChambers = New-Object System.Collections.ArrayList

for ($i = 0; $i -lt 6; $i++) {
    [void]$vecChambers.Add($i + 1)
}

Write-Host "Powershell Russian Roulette. The bullet is in chamber #$bullet_chamber."
Sleep -Seconds 2

while (1) {
    $trigger_pulls++
    [int]$vec_index = 0
   
    if ($vecChambers.Count -gt 1) {
        $vec_index = Get-Random -Maximum ($vecChambers.Count - 1)
    }
   
    [int]$nChamber = $vecChambers[$vec_index]
   
    Write-Host "Pulled the trigger on chamber #$nChamber!"
   
    if ($nChamber -eq $bullet_chamber) {
        Write-Host "Bang! You died after $trigger_pulls shots."
        break
    }
   
    else {
        Write-Host "You survived this time."
        $vecChambers.RemoveRange($vec_index, 1)
    }
   
    Sleep -Seconds 2 
}


Wednesday, July 9, 2014

Simulate C++11 std::copy_n () function

Let's say you are using a compiler that doesn't have C++11 features on it. Here is a way to simulate std::copy_n (). I believe my implementation is correct.

Tested with Visual Studio 2008 and Visual Studio 2012 (Yes, I know VS2012 has std::copy_n)

    // Simulate C++11 std::copy_n
    template <class InputIterator, class Size, class OutputIterator>
    OutputIterator copy_n (InputIterator first, Size count, OutputIterator result)
    {
        if (count > 0) {
            for (Size n = 0; n < count - 1; ++n, ++first, ++result) {
                        *result = *first ;
            }
           
            *result = *first ; // We don't want to advance `first` again.
            ++result ; // `result` points to one past the last value.
        }

        return result ;
    }

Read one address and get two different values

This post is about fun with undefined behavior. There are a few different things going on:
  1. We are casting a struct to a int**.
  2. We are modifying the value of a constant private member.
  3. We are modifying the value of a constant variable on the stack.
When we read the address and value of size, the compiler is probably performing optimizations. Instead of reading the value of size, it's probably just replacing it with the literal 3.

Tested with Visual Studio 2012

#include <iostream>

struct PtrHolder
{
    PtrHolder (const int *p) : p (p)
    {
    }

private:
    const int *p ;
};

int main (void)
{
    const int size = 3 ;

    char buffer [size] = {} ;

    PtrHolder ph (&size) ;

    int **pp = reinterpret_cast <int **> (&ph) ;

    std::cout << "*pp: " << *pp << ", pp[0][0]: " << pp[0][0] << "\n" ;
    std::cout << "&size: " << &size << ", size: " << size << "\n" ;

    pp [0] [0] = 2 ;

    std::cout << "*pp: " << *pp << ", pp[0][0]: " << pp[0][0] << "\n" ;
    std::cout << "&size: " << &size << ", size: " << size << "\n" ;

    return 0 ;
}

Output
*pp: 0041FD28, pp[0][0]: 3
&size: 0041FD28, size: 3
*pp: 0041FD28, pp[0][0]: 2
&size: 0041FD28, size: 3

Print vector recursively

Someone asked a question on Stack Overflow about printing the contents of a vector recursively. The OP provided no code and the question was downvoted and eventually deleted. Here's a quick and dirty way to do it using iterators.

Tested with Visual Studio 2012.

#include <iostream>
#include <vector>

template <class Iterator>
void print (Iterator iter, Iterator end)
{
    if (iter == end) {
        return ;
    }

    std::cout << *iter << "\n" ;

    print (++iter, end) ;
}

int main (void)
{
    int vals [] = {5, 10, 15, 20} ;
    std::vector <int> v (vals, vals + 4) ;

    print (v.cbegin (), v.cend ()) ;

    return 0 ;
}