Tag Archives: extern

C++ Storage classes and qualifiers like auto, extern, register, volatile, const, mutable

C++ Storage classes and qualifiers like auto, extern, register, volatile, const, mutable

C++ storage classes include:
Auto: This is the default where variables are automatically created when they are defined and are destroyed at the end of a block containing their definition. They are also not visible outside of their defined block.
Register: This is similar to auto variable but suggests to the compiler to use a CPU register for performance.
Extern: A static variable that is whose definition and placement is determined when object and library modules are combined (also known as linking) to form the executable code file. It can also be visible outside of where it is defined as well. Both C and C++ require that symbols are only defined in a single translation unit (file.c|file.cpp). The extern keyword tells the compiler that a symbol is only a declaration – it is defined somewhere else. If you only ever include a header in a single .c-file, you can get away without the extern. However, if you include it in more than one, the linker will see the same symbol defined in more than one translation unit (one for each .c[pp] file including the header) and rightly flag it as an error.
Storage qualifiers:
Const keyword indicates that memory once initialized cannot be altered by the program.
Volatile keyword indicates that the value at a particular memory location can be altered event though in the program modifies its content. Volatile is to improve the compiler optimization. The volatile keyword is a type qualifier used to declare that an object can be modified in the program by something (external resources) such as the operating system, the hardware, or a concurrently executing thread. For example,
Mutable keyword indicate particular data member of a structure or class can be altered even if a particular variable is defined in a class member function as being const.
structure variable, class, or class member function is constant.
struct data
{
char name[80];
mutable double salary;
}
const data MyStruct = { “Satish Shetty”, 1000 }; //initlized by complier
strcpy ( MyStruct.name, “Shilpa Shetty”); // compiler error
MyStruct.salaray = 2000 ; // complier is happy allowed

?
int volatile nVint;

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

C++ Interview questions on name mangling, extern, and struct

C++ Interview questions on name mangling, extern, and struct

Name mangling:
What is name mangling in C++??
The process of encoding the parameter types with the function/method name into a unique name is called name mangling. The inverse process is called demangling.
For example Foo::bar(int, long) const is mangled as `bar__C3Fooil’.
For a constructor, the method name is left out. That is Foo::Foo(int, long) const is mangled as `__C3Fooil’.

What does extern “C” int func(int *, Foo) accomplish?
It will turn off “name mangling” for func so that one can link to code compiled by a C compiler.

Const:
What is the difference between const char *myPointer and char *const myPointer?
Const char *myPointer is a non constant pointer to constant data; while char *const myPointer is a constant pointer to non constant data.
Name two cases where you MUST use initialization list as opposed to assignment in constructors.
Both non-static const data members and reference data members cannot be assigned values; instead, you should use initialization list to initialize them.
Global variables:
Can you think of a situation where your program would crash without reaching the breakpoint which you set at the beginning of main()?
C++ allows for dynamic initialization of global variables before main() is invoked. It is possible that initialization of global will invoke some function. If this function crashes the crash will occur before main() is entered.

Struct:
What are the differences between a C++ struct and C++ class?
The default member and base class access specifiers are different.
The C++ struct has all the features of the class. The only differences are that a struct defaults to public member access and public base class inheritance, and a class defaults to the private access specifier and private base class inheritance.

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

C++ interview questions on storage classes like static, register, auto, extern, volatile, mutable

C++ interview questions on storage classes like static, register, auto, extern, volatile, mutable

Storage classes
What are C++ storage classes?
auto
register
static
extern
auto: the default. Variables are automatically created and initialized when they are defined and are destroyed at the end of the block containing their definition. They are not visible outside that block
register: a type of auto variable. a suggestion to the compiler to use a CPU register for performance
static: a variable that is known only in the function that contains its definition but is never destroyed and retains its value between calls to that function. It exists from the time the program begins execution
extern: a static variable whose definition and placement is determined when all object and library modules are combined (linked) to form the executable code file. It can be visible outside the file where it is defined.
What are storage qualifiers in C++ ?
They are..
const
volatile
mutable
Const keyword indicates that memory once initialized, should not be altered by a program.
volatile keyword indicates that the value in the memory location can be altered even though nothing in the program
code modifies the contents. for example if you have a pointer to hardware location that contains the time, where hardware changes the value of this pointer variable and not the program. The intent of this keyword to improve the optimization ability of the compiler.
mutable keyword indicates that particular member of a structure or class can be altered even if a particular structure variable, class, or class member function is constant.
struct data
{
char name[80];
mutable double salary;
}
const data MyStruct = { “Satish Shetty”, 1000 }; //initlized by complier
strcpy ( MyStruct.name, “Shilpa Shetty”); // compiler error
MyStruct.salaray = 2000 ; // complier is happy allowed

Reference:

What’s the difference between pointer and reference
Reference is to one object can not be changed to another object
Reference should be initialized always.
Any other Differences??

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!