FCI-Helwan blog

Just another FCI-H weblog

Although I’m a java fan c++ is of course faster

Whatever that there is an opinions that Java or any JITs languages gonna be faster than c++
that is totally wrong and i’d like to write on this but Dejan Jelovic have written a great article on this so here what I want to say 😀

http://www.jelovic.com/articles/why_java_is_slow.htm

January 15, 2008 Posted by | C/C++, JAVA | Leave a comment

Differences between Pointers and References

1- Reference must be initialized and assigned to the object it references to, like

int &ref_integer ; // error

int number;

int &ref_integer = number; // right

pointers have not to point to something when declared, it’s enough to mention to which type it points

int *p_integer; // right

But also in reference you do not need to mention to which object or variable it references iff (if and only if) you make it external

int &ref_integer ; // error

extern int &ref_integer ; // right

2- Reference can not be reinitilized
int number, anotherNumber;

int &ref_integer = number; // right

&ref_integer = anotherNumber; //error

where pointer can be.

int number, anotherNumber;

int* p_integer;

p_integer = &number;

p_integer = &anotherNumber;

3- ‘&’ operator when is used with reference it returns the address of the variable the reference references, like

int integer;

int &ref_integer = integer;

cout<<&integer<<endl; // prints XXXXXXX

cout<<&ref_integer<<endl; // prints XXXXXXX

but in pointers, when ‘&’ operator is used it references to the address of the pointer not the address of the varible it poitns to
int* p_integer;

p_integer = &integer;

cout<<&integer<<endl; // prints XXXXXXX

cout<<&p_integer<<endl; // prints YYYYYYY

February 4, 2007 Posted by | C/C++ | 2 Comments

Casting in C++

There are 4 types to convert from type to type (Casting) as demonstrated below

1. The standard cast to convert one type to another.
2. There is a special cast to do away with the const type-modification
3. A third cast is used to change the interpretation of information
4. There is a cast form which is used in combination with polymorphism

1 – The first (static_cast) is to convert from one type to another like from double to int.
Example 1
int anInteger = 0;
double aDouble = 12.75;
anInteger = static_cast<int>(aDouble);

Example 2
What’s the situation in where the arithmetic assignment operators used
in mixed type like:
anInteger+= aDouble; //anInteger = anInteger + aDouble;
Let’s see what happens behind the scene
anInteger = static_cast<int>(aDouble) + anInteger; //done if you cast anInteger to double

2- The second (const_cast) is to convert from a constant variable to a non constant variable.

Example 1
If I’ve function like that
void ShowText(char *c)
{
printf(“%s\n”, c);
}

And I want to pass it a parameter like that
char const fci[] = “Hello, FCI-H!”;
ShowText(fci); //error
If I try that, Compile complains and shoots this error “Error
1 error C2664: ‘ShowText’ : cannot convert parameter 1 from ‘const char [14]’
to ‘char *’”

So, we should do that
ShowText(const_cast<char*>(fci)); //no error and it prints Hello, FCI-H!

3- The third (reinterpret_cast) is to convert from any integral type to any pointer type and vice versa, but it does not work with constants (see No. 2). Plus it’s very unsafe, try to not use it.There is a practical use of reinterpret_cast is in hash function, because it generates two indexes for two different values rarely end up with the same index and it’s allow pointers to be treated as integral type.

Example 1
void pointerToInteger(void *ptr)
{
int aNumber = reinterpret_cast<int>(ptr);
printf(“%d\n”, aNumber * 2 );
}
Your assignment is to know how to call this function :p

4- The fourth is (dynamic_cast) is to convert from base class pointer to derived class pointer or from base class reference to derived class reference. But it should be one virtual function at least in the base class. (I’ll talk about it in details in the next article after Allah willing).

To be continued…

February 2, 2007 Posted by | C/C++ | Leave a comment

OpenCV

OpenCV is Intel® Open Source Computer Vision Library. It is a collection of C functions and a few C++ classes that implement some popular Image Processing and Computer Vision algorithms.
If you want to gain the great performance of C without being involved in the tiny details (like creating your own data structures & customized functions, etc) in the same way you used to on Mat-lab, OpenCV is your best choice. Yet, other stuff not related to image processing & computer vision directly (like neural networks, etc) won’t be provided through OpenCV (yet you can find them available on the Internet in C, also).

For example, the above image was captured by a web-cam, segmented, masked with the original image from the web-cam again, & finally saved on disk with a few lines of code.

The key features :

OpenCV has cross-platform middle-to-high level API that consists of a few hundreds (>300) C functions. It does not rely on external libraries, though it can use some when it is possible.

OpenCV is free for both non-commercial and commercial use (see the license for details).

OpenCV provides transparent interface to Intel® Integrated Performance Primitives (IPP). That is, it loads automatically IPP libraries optimized for specific processor at runtime, if they are available. That grantees even faster execution (C is already fast on modest hardware) for usually complex image processing operations, making use of the available hardware (if it’s Intel®’s). More information about IPP can be retrieved @ http://www.intel.com/software/products/ipp/index.htm

For more information about OpenCV :

January 27, 2007 Posted by | C/C++, open source | 2 Comments