norman's blog

Notes of an amnesiac.
Never stop thinking.
Find an aesthetic description.

Tuesday, June 30, 2009

Constructors and destructors of an class and its member which is also a class

The compiler will call the member's constructor just before the class's constructor, and call the member's destructor just behind the class's constructor. That is because the class's constructor and destructor may access the member, which is supposed to be allocated. See code example:
class B {
  public:
    B();
    ~B();
};
B::B() {
  printf("construct B\n");
}
B::~B() {
  printf("destruct B\n");
}

class A {
  public:
    A();
    ~A();
  private:
    B b;
};
A::A() {
  printf("construct A\n");
}
A::~A() {
  printf("destruct A\n");
}



int main() {

  printf("a live\n");
  {
  A a;
  }
  printf("a die\n");
}
The result is:
a live
construct B
construct A
destruct A
destruct B
a die
An alternative method is use the pointer and new&delete. new and delete will call the constructor and destructor respectively, in the case that the pointer point to a class type.
class B {
  public:
    B();
    ~B();
};
B::B() {
  printf("construct B\n");
}
B::~B() {
  printf("destruct B\n");
}

class A {
  public:
    A();
    ~A();
  private:
    B * b;
};
A::A() {
  printf("construct A\n");
  b = 0; //b is random value at first, assigning it to 0 avoid some error if new failed 
  b = new B;
}
A::~A() {
  printf("destruct A\n");
  if(b)
    delete(b);
}



int main() {

  printf("a live\n");
  {
  A a;
  }
  printf("a die\n");
}
The result is:
a live
construct A
construct B
destruct A
destruct B
a die

Labels:

Read more!

Wednesday, June 3, 2009

A list of gcc/g++ errors

new types may not be defined in a return type
That error is typically caused by forgetting a semicolon at the end of a class / struct / union... stack smashing detected I am encountered with this error when I assign a char array with a string, which is longer than the max length of the array.

Labels: ,

Read more!

Tuesday, June 2, 2009

Memory Management

Memory Errors
Differences Between New/Delete and Malloc/Free
Note:
  • new calls the constructor, delete calls the destructor. malloc and free do neither of these things.
  • new throws an exception on memory exhaustion, malloc returns NULL.

Labels:

Read more!

string and character

Type of String and Character

Characters are used to represent characters in the execution character set, which have the type char.
The execution character set is not necessarily the same as the source character set used for writing C programs. The execution character set includes all characters in the source character set as well as the null character, newline character, backspace, horizontal tab, vertical tab, carriage return, and escape sequences.

Strings are used to represent a sequence of characters which, taken together, form a null-terminated string. Strings have type array of char.This means that a string is an array with elements of type char. The number of elements in the array is equal to the number of characters in the string plus one for the terminating null character('\0').

Single Quotation and Double Quotation

A "character" is formed by enclosing a single character within single quotation marks (' ').
A "string" is a sequence of characters enclosed in double quotation marks (" ").

Representation of Characters and Escape Sequences

For common printable characters, a direct WYSIWYG representation is applicable. For instance: 'A' stands for the character A. For unprintable characters and printable characters with special meanings, escape sequences are used. Character combinations consisting of a backslash (\) followed by a letter or by a combination of digits are called "escape sequences". Actually, all characters can be represented by Oct/Hex notation of escape sequences.
\a Bell (alert)
\b Backspace
\f Formfeed
\n New line
\r Carriage return
\t Horizontal tab
r\v Vertical tab
\' Single quotation mark
\ " Double quotation mark
\\ Backslash
\? Literal question mark
\ ooo ASCII character in octal notation
\x hh ASCII character in hexadecimal notation
\x hhhh Unicode character in hexadecimal notation if this escape sequence is used in a wide-character constant or a Unicode string literal. For example, WCHAR f = L'\x4e00' or WCHAR b[] = L"The Chinese character for one is \x4e00".

See MSDN Reference

Standard C String and Character Functions

Check "string.h" and "stdlib.h".
See Functions list.
See string.h.
See stdlib.h.

ASCII Code

ASCII includes definitions for 128 characters: 33 are non-printing, mostly obsolete control characters that affect how text is processed;[6] 94 are printable characters, and the space is considered an invisible graphic.
See wiki.

Labels:

Read more!

Monday, June 1, 2009

Directory Separator

"\": Backslash 
"/": Forward Slash
":": Colon
Use the forward slash as separator because that's the UNC (Universal Naming Convention) standard, it works for both Linux/UNIX and (usually) DOS/Windows, and the Mac-heads should understand it.
See Reference

Labels:

Read more!

Sunday, May 31, 2009

argc&argv

Argument passed to main()
int argc;
argc is an abbreviation for "argument count". It is the traditional name for the first argument to a C program's main routine. By convention, it holds the number of arguments that are passed to main in the argument vector argv. Because argv[0] is always the name of the command, the value of argc is always one greater than the number of command-line arguments that the user enters.
char *argv[];
argv is an abbreviation for ``argument vector''. It is the traditional name for a pointer to an array of string pointers passed to a C program's main function; by convention, it is the second argument passed to main. By convention, argv[0] always points to the name of the command itself.
Example of argument reading codes
    if( argc == 1 )
    {
        printf( "Usage: %s\n  -data \n"
                "  -info \n"
                "  [-maxSizeDiff ]\n"
                "  [-maxPosDiff ]\n"
                "  [-sf ]\n"
                "  [-ni]\n"
                "  [-nos ]\n"
                "  [-rs ]\n"
                "  [-w ]\n"
                "  [-h ]\n",
                argv[0], maxSizeDiff, maxPosDiff, scale_factor, nos, rocsize,
                width, height );
        
        return 0;
    }

    for( i = 1; i < argc; i++ )
    {
        if( !strcmp( argv[i], "-data" ) )
        {
            classifierdir = argv[++i];
        }
        else if( !strcmp( argv[i], "-info" ) )
        {
            infoname = argv[++i];
        }
        else if( !strcmp( argv[i], "-maxSizeDiff" ) )
        {
            maxSizeDiff = (float) atof( argv[++i] );
        }
        else if( !strcmp( argv[i], "-maxPosDiff" ) )
        {
            maxPosDiff = (float) atof( argv[++i] );
        }
        else if( !strcmp( argv[i], "-sf" ) )
        {
            scale_factor = atof( argv[++i] );
        }
        else if( !strcmp( argv[i], "-ni" ) )
        {
            saveDetected = 0;
        }
        else if( !strcmp( argv[i], "-nos" ) )
        {
            nos = atoi( argv[++i] );
        }
        else if( !strcmp( argv[i], "-rs" ) )
        {
            rocsize = atoi( argv[++i] );
        }
        else if( !strcmp( argv[i], "-w" ) )
        {
            width = atoi( argv[++i] );
        }
        else if( !strcmp( argv[i], "-h" ) )
        {
            height = atoi( argv[++i] );
        }
    }

Labels:

Read more!