June 12, 2008

C++ Q&A


What are Guard bytes ?
malloc_dbg calls malloc, but requests a few more bytes. These bytes are filled with a predefined value. free_dbg then checks if these extra bytes still hold this value. If they did, you probably wrote beyond the allocated memory (the VC Runtime prints a diagnostic message to the debug output). Typical implementations add 4 bytes at the end.In this case, using free instead of _free_dbg is often not a problem, but depending on the implementation, it might leak the guard bytes. But if an implementation adds guard bytes *before* the memory provided to you (to detect underflows, which are less common), using free instead of -free_dbg is likely to corrupt the heap.



Difference Between WinSock 1.1 and WinSock 2 ?

One of the most important new features is official support for multiple transport protocols. Although Winsock 1.1 was not actually limited to TCP/IP, that was the only protocol that had official support written into the spec. There was no standard way for a vendor to add support for another transport protocol.
Winsock 2 also adds support for technical initiatives like quality of service (QoS) and multicasting. These technologies will become increasingly important as bandwidth requirements become more regimented and intense. For example, QoS allows a videoconferencing program to reserve a certain amount of bandwidth so that a sudden file transfer.
Winsock 2 also allows for "Layered Service Providers." This enables many neat things, such as security plug-ins: drop in, say, an SSL service provider, and all of a sudden your data is automatically encrypted.



What is #Pragmas?
#Pragmas can be used in conditional statements, to provide new preprocessor functionality, or to provide implementation-defined information to the compiler. If the compiler finds a pragmas it does not recognize, it issues a warning, but compilation continues. Some pragmas provide the same functionality as compiler options. When a pragmas is encountered in source code, it overrides the behavior specified by the compiler option.
#pragma once : Used to Compile one time…..Specifies that the file will be included (opened) only once by the compiler when compiling a source code file.



What is Structure Alignment?
Structure Alignment is same as memory padding in structure, add 2 byte for each member for alignment. At the heart of document/view are four key classes:



Q: What are dependent names?
A: Dependent names are names whose definitions are considered to depend upon the template parameters and for which there is no declaration within the template definition. They are resolved only when the template is instantiated. Those that are intended to refer to types or templates may require disambiguation. If the resolution of a dependent function name uses argument-dependent lookup, declarations in the arguments' namespaces that are visible at the point of instantiation will be considered as well as declarations visible at the point of definition. (The former is normally a superset of the latter, but may not be.)


Q: What are non-dependent names?
A: Non-dependent names are those names that are considered not to depend upon the template parameters, plus the name of the template itself and names declared within it (members, friends and local variables). They are resolved when the template is defined, in the normal way, and do not require disambiguation.


Q: Is there a difference between a function template and a template function, or between a class template and a template class?
A: The term "function template" refers to a kind of template. The term "template function" is sometimes used to mean the same thing, and sometimes to mean a function instantiated from a function template. This ambiguity is best avoided by using "function template" for the former and something like "function template instance" or "instance of a function template" for the latter. Note that a function template is not a function. The same distinction applies to "class template" versus "template class".



What is Object Slicing?

Suppose that class D is derived from class C. We can think of D as class C with some extra data and methods. In terms of data, D has all the data that C has, and possible more. In terms of methods, D cannot hide any methods of C, and may have additional methods. In terms of existing methods of C, the only thing that D can do is to override them with its own versions.

If x is an object of class D, then we can slice x with respect to C, by throwing away all of the extensions that made x a D, and keeping only the C part. The result of the slicing is always an object of class C.

What is the difference between GetMessage and PeekMessage?


The major difference between the two is that GetMessage() doesn't return until it finds a message to retrieve from the Application Queue, this allows us to free up precious CPU usage for other programs to use. PeekMessage() returns immediately weather there are any messages or not, this allows us to utilize the time between messages, for example to render a 3D scene.

How to handle command line arguements from simple MFC application ?

Using GetCommandLineArgs() function you can get the arguments in MFC application at any time. In InitInstance () function you can change the behaviour of the application using those values.

No comments: