Practical, Pragmatic, C++
RSS icon Email icon Home icon
  • Update: qt and git-svn

    Posted on May 11th, 2009 wozname No comments

    QT

    I’m currently learning the ins and outs of the QT gui toolkit and I got to say that it is quite neat. It’s available for free at http://www.qtsoftware.com/. It seems to be dogged by a fair amount of controversy. Do some googling about KDE and QT and their licences. It’s been released under the LGPL which is good news for small commercial developers, since their commercial licences are quite expensive. There is much debate about it’s signals and slots system which uses a custom preprocessor and is only type checked at runtime.

    Git-svn

    I have also been playing with git-svn. Git is my favourite version control system. You can grab it from http://git-scm.com/. The windows version that I use day-to-day is available at: http://code.google.com/p/msysgit/ it’s got a gui installer for those allergic to the cli and it’s maturing quite fast.

    Git-svn is a git command that allows you to contribute to a subversion based project using git. The first thing it does is download the entire subversion repository, including all branches and tags. This can take quite a while, but once you have it, browsing the projects history is a dream. I tested it out on some of the smaller Sourceforge projects and it seems quite foolproof.

    For the larger projects, I would recommend you find a download of a preconverted git repository and work from there. With a bit of googling it should be no problem.

    Happy Programming :)

  • What character set does C++ use?

    Posted on May 4th, 2009 wozname No comments

    I believe the answer to this question, is that it depends on the implementation. The standard only states that char is wide enough to contain the “basic execution character set” and that wchar_t is wide enough to contain any character from all the character sets in all locales on an implementation.

    Read the rest of this entry »

  • TCHAR in new C++ programs

    Posted on May 3rd, 2009 wozname 1 comment

    Wheres the beginning C++ 4 article?

    I have been debating with myself whether I should use TCHAR in my C++ examples in this blog.
    When you generate a new C++ console project in Visual Studio 2008, it automatically includes
    TCHAR and friends. I think this would be a bit confusing for the beginner who is trying to learn
    C++ for the first time. The approach that I think I’m going to take, is to delete all the generated
    files and let the beginner build his first program up from scratch in a new file using only standard
    types.

    First a review of the standard C++ char types

    A null terminated string is an array of chars with a null (0) character at the end
    to signal the end of the string. It is usually referred to with a char* pointer to the
    first letter in the string. Alternatively it can be embedded in a std::string or std::vector
    which is safer.

    Read the rest of this entry »

  • C++ Types 3

    Posted on May 2nd, 2009 wozname No comments

    Integer Types

    Unsigned integer types

    These types only vary in the amount of bits they use to represent numbers. The numbers are encoded in the bits as binary. C++03 mandates a set of unsigned integer types and a minimum number of bits and range for each type. The actual number of bits is implementation specific as long as it follows the following rules

    1. (for the number of bits of each) unsigned char <= unsigned short int <= unsigned int <= unsigned long int.
    2. char (and thus unsigned char) has enough bits to store the basic character set.
    3. int has the “natural” bit size of the architecture (Usually 16, 32 or 64 bits)

    Type

    C++03 minimum bits

    C++03 Minimum range

    VS2008 bits

    VS2008 Range

    unsigned char

    8

    0-255

    8

    0-255

    unsigned short int

    16

    0-65535

    16

    0-65535

    unsigned int

    16

    0-65535

    32

    0-4294967295

    unsigned long int

    32

    0-4294967295

    32

    0-4294967295

    It is interesting to note that the following condition is legal in a C++ implementation:

    • The number of bits in the following types can all be equal: unsigned char = unsigned short int = unsigned int = unsigned long int (It would have to be at least 32 bits to satisfy the standard)
    • For example the following is legal:

      Type

      Bits

      Range

      unsigned char

      60

      0-1152921504606846976

      unsigned short int

      60

      0-1152921504606846976

      unsigned int

      60

      0-1152921504606846976

      unsigned long int

      60

      0-1152921504606846976

    Signed integer types

    Signed integer types can be encoded in three different ways, sign magnitude, 1’s complement and 2’s complement. The C++03 standard allows an implementation to use any of these representations. However most implementations use 2’s complement. 2’s complement is really easy to understand, it’s just like the normal unsigned binary representation, except the highest digit represents the negation of the power of 2 of it’s position. For example 1011 means 1*(-8) + 0*4 + 1*2 + 1 = -8 + 2 + 1 = -5.

    The signed types follow the same rules as the unsigned types (see above) and the ranges are as follows:

    Type

    C++03 minimum bits

    C++03 Minimum range

    VS2008 bits

    VS2008 Range

    signed char

    8

    -127 – 127

    8

    -128 – 127

    short int

    16

    -32767 – 32767

    16

    -32768 – 32767

    int

    16

    -32767 – 32767

    32

    -2147483648 – 2147483647

    long int

    32

    -2147483647 – 2147483647

    32

    -2147483648 – 2147483647

    chars

    There are some special rules for chars. In many ways char is the “base” type of a C++ system. It’s the smallest addressable set of bits when programming in C or C++. Other types’ sizes are always measured in terms of chars, sizeof(T) returns the number of chars needed to store T.

    • char, signed char and unsigned char types must:
      • All have the same alignment requirements and have the same number of bits.
      • Large enough to represent the basic character set.
    • char can either be signed or unsigned, it depends on the implementation.
      • VS2008 uses signed chars by default.
  • C++ Types 2

    Posted on May 1st, 2009 wozname 1 comment

    Encoding Numbers

    To start of with we will want to store numbers. Let’s assume we want to store unsigned integers. Believe it or not there are many ways of encoding unsigned integers into bits.

    The simplest method I can imagine is the following, we could encode the number by the number of sequential 1 bits, e.g. 1 is 1, 11 is 2, 111 is 3, 1111 is 4 etc. Use 0 as a marker for the end of the number and we have 10 is 1, 1110 is 3 and 1111111110 is 9. You could represent any number using this scheme. The major disadvantage of this method is that the space used to represent the number is the same as the magnitude of the number. This means it will take 1 Megabit to represent the number 1048576. To resolve this we need to use a positional notation to represent a number.

    Read the rest of this entry »