2010-05-31

2010-05-27

The cutting edge of pointless being

Az értelmetlenség csúcsteljesítménye



From here (innen): http://kerekagy.blog.hu/2009/12/02/fitnesz

2010-05-21

C++: Initializing containers with compile time list of data

template <typename T, size_t size>
void FillSet(std::set<T>& target, const T (&list)[size])
{
    for(size_t i=0; i<size; ++i)
        target.insert(list[i]);
}
Usage example:
int startkit[] = {10,2,8,4,3,1};
std::set<int> numbers;
FillSet(numbers,startkit);

Easy C++

#define class struct
#define private public
#define protected public
#define const

Undefined increment

#include <stdio.h>

#define COMPUTE(x,y) ((x*x*x) + (y))

int main()
{
        int num1 = 100;
        int num2 = 10;

        printf("%d\n", COMPUTE(++num1, num2));

        return 0;
}
The result is undefined in both C and C++, but the behavior is consistent and interesting. The result is in the first comment.
compute.c:10: warning: operation on ‘num1’ may be undefined
Attention by http://synsecblog.com/

2010-05-12

Programming homework

https://groups.google.com/group/comp.lang.c/msg/e105e5d339edec01?pli=1

> Dear everyone,

> Can someone please help me on my HW. I'm trying to write a program
> that will display the following:

>     *
>    ***
>   *****
>  *******
> *********
> *********
>  *******
>   *****
>    ***
>     *

> I'm having a hard time writing a code for this one. I tried several
> times, but I can't get it to work properly. I live in Japan and I take
> an online C++ course. The instructor lives in the US so whenever I am
> awake, he's asleep. Therefore, I cannot contact him directly when I
> need help. I have to get this program up and running ASAP. I tried
> searching everywhere for help, but there's none except for this group.
> My textbook isn't much of a help either. I WILL GREATLY APPRECIATE THE
> HELP I WILL GET!

My pleasure.

#define      M 002354l
#define     A   000644l
#define    G     000132l
#define     I   000322l
#define      C 000374l

#define                a ;
#define               b for
#define              c    ++
#define             d       %
#define            e       int
#define           f           ,
#define          g             -
#define         h             011
#define        i                 =
#define       j                   {
#define      k                     )
#define     l                    '\n'
#define    m                      main
#define    n                         <
#define     o                       }
#define      p                     >
#define       q                  &&
#define        r                 (
#define         s              ||
#define          t             ?
#define           u     putchar
#define            v      void
#define             w     '*'
#define              x     :
#define               y ' '
#define                _ /
#define           C_O_O_L return

                   e u r e k a

                         e
                        m r
                       v k j
                      j j j j
                     j j j j j
                    j j j j j j
                   j j j j j j j
                  j e z a b r z i
                 M _ A _ G _ I _ C
                a z n G a u r z d h
               + z _ h p M _ A q z d
              h + z _ h n M _ G q z _
             h n z d h + M _ I q z _ h
            p z d h g M _ C t w x y k f
           z d h g h + 1 s u r l k f z c
          k a u r l k a j j j j j j j j j
         j j C_O_O_L M _ A _ G _ I _ C a o
        o o o o o o o o o o o o o o o o o o
                      o o o o
                      o o o o
                      o o o o
                      o o o o 
-- 
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: masked

2010-05-11

Funny programming yargons

@ stackoverflow.com
http://stackoverflow.com/questions/2349378/new-programming-jargon-you-coined/2430307

Some highlights


Yoda condition
You write if(5==count) and you read if five is the count.

Pokemon exception handling
When you catch them all!!!

A Duck
A feature added for no other reason than to draw management attention and be removed, thus avoiding unnecessary changes in other aspects of the product.

2010-05-07

Vízen járás / Walking on water



Valódi vagy kamu?

Real or fake?

2010-05-03

C++: Why initializer list?

class Parent;

class Child {
public:
    Child(
        Parent& parent_
        , const std::string& name_
        , const Color& eyeColor_
    )
    : parent(parent_)
    , name(name_)
    , eyeColor(eyeColor_)
    , money(0,"EUR")
    { }
    Parent& GetParent(){ return parent; }
    std::string GetName() const { return name; }
    Money GetMoney() const { return money; );
    Money AdjustMoney(int diff)
    { money += diff; return money; }
private:
    Parent &parent;
    const std::string name;
    Color eyeColor;
    Money money;
};
The initializer list in the above is this:
: parent(parent_)
, name(name_)
, eyeColor(eyeColor_)
, money(0,"EUR")
  • because of initializing references.

    References are like pointers with two restrictions. Firstly: You (if you are sane) point only to one object with a reference, you never point to an array. Secondly: Once the location is pointed you will not point elsewhere. This "finally" sounding behavior is perfectly good when we want to point one's (abstract) parent.

    If you want the language to force you to keep the second rule and still want your compiler in this millennium then you have to realize, that the initializer must be done before the begin ({) sign appears with it's endless possibilities.

  • because of initializing const members.

    Same as the above, but we are not talking about making up our mind early about a location but about a value. See the name member

  • because of initializing an aggregated object which do not have default constructor.

    Let's imagine the Color class which has only the copy constructor and that one: Color::Color(unsigned char r, unsigned char g, unsigned char b)! This class does not have default value / state by design, so it cannot be constructed without some instructions. See the eyeColor member.

  • because of speed

    You begin your program (constructor body) at the '{' sign. At that point all of your variables must be in a valid state. So if you give value to your members after that point you will waste processor time. One construction and one assignment, instead of simply one construction. See the money member.

    In most of my codes the constructors' body used to be empty. If we want to work in that style we will have to have handy constructors for our classes, which is not a bad thing at all.

Important: The execution of the initializer list is in the order of the data members because that order means the construction order and the reverse of that means the destruction order. If it wouldn't be like that then the program would have to remember the construction order and apply the reverse of it upon destruction. That would waste resources. So the rule came...

... and don't worry about hidden errors! Turn on the warnings in your compiler and take the advices and notes seriously! Seriously like Treat warnings as errors compiler option.

C/C++: undefined behavior explained

Figure #1:
char *s = "Hello?";
s[5] = '!';
printf("%s\n",s);
Figure #2:
char *s = "Hello?";
char s2[] = "Jumbo?";
s = s2;
s[5] = '!';
printf("%s\n",s);

It is impossible to tell that the line s[5] = '!'; contains error or not. You can have some speculation according to some static code analysis, but you cannot tell for sure. That's why it will be a run-time error (if it is an error).

Writing a string literal leads to undefined behavior. If we would say that in this case the compiler must generate a code that crashes, then it would be a feature, some kind of requirement for the compiler's manufacturer. Every usage of char * for writing would have a literal-check, which obviously has a very large overhead. The writers of the standard decided not to make any behavioral requirements in such cases. After that you can imagine other undefined behavior cases or you can look them up and think about each of them individually.

Back to the example! In some cases it will lead to changing the literal, in other cases it will crash with "access violation" or "segmentation fault", because we are trying to write to a read-only area of the memory. ... To avoid accidents, I (the const fetishist) recommend you this:

const char *s = "Hello?";

You will have a compile time error instead of undefined behavior and you can fix your code and-or idea.

Pedophile beards

... because the bad boy look is for pussies.