C++ Part4

Why do C++ compilers need name mangling?

Name mangling is the rule according to which C++ changes function’s name into function signature before passing that function to a linker. This is how the linker differentiates between different functions with the same name.

Is there anything you can do in C++ that you cannot do in C?

No. There is nothing you can do in C++ that you cannot do in C. After all you can write a C++ compiler in C. 

 

 


This is my little collection of C++ Tipps/Tricks/little known facts.
Every time i discover something worth adding, i will put it here.

1. Discovering Memory Leaks
2. CPUID/RDTSC
3. Exact Time Measuring
4. Functionpointer to class memberfunctions
5. Setting Fullscreen mode under windows
6. Know thy stringstreams
7. std::sort with non-static compare-method in a class

1. Discovering Memory Leaks
Of course if you are working on a serious project you will use a sophisticated memory manager that will also discover memory leaks (along with their exact position in the code), but if you just want to do a quick test there is an easy way to let the debugger check for memory leaks:
– Include crtdbg.h in your project.
– At the beginning of you main() / WinMain () put this code:

int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
flag |= _CRTDBG_LEAK_CHECK_DF;
_CrtSetDbgFlag(flag);

This works in VC++ ( i don’t know about other compilers tho ) and, if you run the programm in debug mode (Hit F5), it will report memory leaks in the debug output window.

2. CPUID/RDTSC
You have propably heard about the assembler instruction CPUID and RDTSC, which are supported by VC++ 6 and higher, but if you have an older compiler you can make use of the __emit function. This is a pseudo-asm instruction that lets you insert bytes directly into the outputted .exe. So, with a compiler that doesn’t know the cpuid/rdtsc instuctions we can just use a little macro to put their respective opcodes directly as binary values into our program:

#define rdtsc __asm __emit 0fh __asm __emit 031h
#define cpuid __asm __emit 0fh __asm __emit 0a2h

How the proxy server works ?

 How the proxy server works
Once you have configured your web browser as described, your browser will load a CGI script each time it is restarted. This script redefines the function that determines how to fetch the URL that is being requested. If the URL matches a pattern or a site that requires a proxy, it will connect to the proxy server rather than going directly to the site.

——————————————————————————–
(USR = User on IBM.Net or another ISP; PXY = BOL web proxy server; SRV = web server restricted to UCLA)

Step 1: User starts browser, connects to proxy server to get the Proxy Automatic Configuration (PAC) file.
  fetch request: proxy.ucla.edu/cgi/proxy

    +—–+                    +—–+
    |  U  |                    |  P  |
    |  S  | ==================>|  X  |
    |  R  |                    |  Y  |
    +—–+                    +—–+

Step 2: Proxy server returns “proxy”, which configures
  browser to do proxying.

    +—–+                    +—–+
    |  U  | “proxy” loaded     |  P  |
    |  S  |<================== |  X  |
    |  R  |                    |  Y  |
    +—–+                    +—–+

Step 3: User requests URL, URL compared against function retrieved
  from “proxy”, connection route is determined.

Step 4a: Route for URL returned from function is “DIRECT”
  (no proxy required).  Browser retrieves URL directly.  (*Note: At this
  point, every URL returns “DIRECT” unless it is one that is known to
  restrict access to ucla.edu*)

    +—–+                    +—–+
    |  U  | fetch URL          |  S  |
    |  S  |<==================>|  R  |
    |  R  |                    |  V  |
    +—–+                    +—–+

Step 4b1: Route for URL returned from function is “PROXY xxxxx”
  (proxy required).  Browser contacts proxy server and requests page.

    +—–+                    +—–+                    +—–+
    |  U  | request URL        |  P  |                    |  S  |
    |  S  | ==================>|  X  |                    |  R  |
    |  R  |                    |  Y  |                    |  V  |
    +—–+                    +—–+                    +—–+

Step 4b2: Proxy contacts server and requests page.

    +—–+                    +—–+                    +—–+
    |  U  |                    |  P  | request URL        |  S  |
    |  S  |                    |  X  | ==================>|  R  |
    |  R  |                    |  Y  |                    |  V  |
    +—–+                    +—–+                    +—–+

Step 4b3: Server sees request from ucla.edu domain (proxy address)
  and returns URL to proxy.

    +—–+                    +—–+                    +—–+
    |  U  |                    |  P  | return URL         |  S  |
    |  S  |                    |  X  |<================== |  R  |
    |  R  |                    |  Y  |                    |  V  |
    +—–+                    +—–+                    +—–+

Step 4b4: Proxy returns URL to User.

    +—–+                    +—–+                    +—–+
    |  U  | return URL         |  P  |                    |  S  |
    |  S  |<================== |  X  |                    |  R  |
    |  R  |                    |  Y  |                    |  V  |
    +—–+                    +—–+                    +—–+
 

Video Code Convertor – RGB to YUV Pixel Conversation

Video Code Convertor – RGB to YUV Pixel Conversation

Let me first give introduction about RGB and YUV.
RGB and YUV are two different way of storing video.

RGB (Red, Green and Blue)
The RGB color model is an additive model in which red, green and blue
are combined in various ways to reproduce other colors.The name of the
model and the abbreviation “RGB” come from the three primary colors,
Red, Green and Blue.

YUV (Brightness and color)
The color encoding system used for analog television worldwide
(NTSC, PAL and SECAM). The YUV color space (color model) differs
from RGB, which is what the camera captures and what humans view.

YUV also saves transmission bandwidth compared to RGB. For rendering on
screen, all these color spaces must be converted back again to RGB
by the TV or display system

The YUV model defines a color space in terms of one luminance and
two chrominance components.YUV is used in the analog variant of the
PAL system of television broadcasting, which is the standard in
much of the world.Y stands for the luminance component (the brightness)
 and U and V are the chrominance (color) components

The Mathmatically conversion formula:

RGB to YUV Conversion
Y  = (0.257 * R) + (0.504 * G) + (0.098 * B) + 16

V =  (0.439 * R) – (0.368 * G) – (0.071 * B) + 128

U = -(0.148 * R) – (0.291 * G) + (0.439 * B) + 128
YUV to RGB Conversion
B = 1.164(Y – 16)                  + 2.018(U – 128)

G = 1.164(Y – 16) – 0.813(V – 128) – 0.391(U – 128)

R = 1.164(Y – 16) + 1.596(V – 128)

Tricky Algorithm -Implementation Swap function Code in c++

Differnt Swap Algorithm

    -Swap Classic

    -Swap without Temporay

   -Swap using Bitwise

 You can use this snippet to implement tricky Swap Algorithm

//****************************************
// Author : Sanjay Vyas
// Purpose : To Implement Differnt Swap Alogorithm

//****************************************
#include <iostream.h>

// Swap Classsic Using Temp Variable
void Swap_Classic(int &ValFirst,int & ValSecond)
{
 int ValTemp;
 ValTemp =ValFirst;
 ValFirst= ValSecond;
 ValSecond = ValTemp;
}
// Swap New Without Using Temp Variable
void Swap_New(int &ValFirst,int & ValSecond)

 ValFirst = ValFirst + ValSecond ;
 ValSecond= ValFirst – ValSecond ;
 ValFirst = ValFirst – ValSecond ;
}
// Swap New using bitwise
void Swap_Bitwise(int &ValFirst,int & ValSecond)

 ValFirst= ValFirst ^ ValSecond ; //  1080 = 1000 ^ 2000
 ValSecond=ValFirst ^ ValSecond ; // 1000 = 1080 ^ 2000
 ValFirst= ValFirst ^ ValSecond ; // 2000 = 1080 ^ 1000
}
void Reset_Value(int &ValFirst,int & ValSecond)
{
 ValFirst = 1000;
 ValSecond= 2000;
}
int main()
{
 int ValueFirst=1000;
 int ValueSecond=2000;
 cout << “Implemented Different Swap Function” << endl;
 cout << “Original Value:” << endl;
 cout << ” First :” << ValueFirst << endl;
 cout << ” Second:” << ValueSecond << endl;
 
 Swap_Classic (ValueFirst,ValueSecond);
 cout << “Swap Classsic:” << endl;
 cout << ” First :” << ValueFirst << endl;
 cout << ” Second:” << ValueSecond << endl;

 Reset_Value(ValueFirst,ValueSecond);

 Swap_New(ValueFirst,ValueSecond); 
 cout << “Swap New:” << endl;
 cout << ” First :” << ValueFirst << endl;
 cout << ” Second:” << ValueSecond << endl;

 Reset_Value(ValueFirst,ValueSecond);

 Swap_Bitwise(ValueFirst,ValueSecond); 
 cout << “Swap New:” << endl;
 cout << ” First :” << ValueFirst << endl;
 cout << ” Second:” << ValueSecond << endl;
 
 cout << “<- End ->” << endl;
}