Posts

compiler, linker, loader

=====> COMPILATION PROCESS <====== | |----> Input is Source file(.c) | V +=================+ | | | C Preprocessor | | | +=================+ | | ---> Pure C file ( comd:cc -E <file.name> ) | V +=================+ | | | Lexical Analyzer| | | +-----------------+ | | | Syntax Analyzer | | | +-----------------+ | | | Semantic Analyze| | | +-----------------+ | | | Pre Optimization| | | ...

Difference Between LIB and DLL

LIB vs DLL When developing software, we are often asked whether we want to use LIB or DLLs in containing functions for the application. LIB is a static library where functions and procedures can be placed and called as the application is being compiled. A DLL or Dynamic Link Library does the same function but is dynamic in a sense that the application can call these libraries during run-time and not during the compilation. This presents a few significant advantages compared to using LIB. For starters, you would have a single file that is significantly bigger as it contains all of the code while you would have multiple smaller files when using DLL. Compiling your functions and procedures would also allow you more reusability as once you are happy with the functions on the DLL because you can keep it as is with each version of the application and not have to mess with it. You can also use the same DLL if you want to create another application that uses the same functions and proced...

Dynamic Memory Allocation and Fragmentation in C and C++

Abstract : In C and C++, it can be very convenient to allocate and de-allocate blocks of memory as and when needed. This is certainly standard practice in both languages and almost unavoidable in C++. However, the handling of such dynamic memory can be problematic and inefficient. For desktop applications, where memory is freely available, these difficulties can be ignored. For embedded - generally real time - applications, ignoring the issues is not an option. Dynamic memory allocation tends to be nondeterministic; the time taken to allocate memory may not be predictable and the memory pool may become fragmented, resulting in unexpected allocation failures. In this session the problems will be outlined in detail and an approach to deterministic dynamic memory allocation detailed. C/C++ Memory Spaces It may be useful to think in terms of data memory in C and C++ as being divided into three separate spaces: Static memory . This is where variables, which are defined outside o...

C program that won’t compile in C++

Write a C program that won’t compile in C++ Although C++ is designed to have backward compatibility with C there can be many C programs that would produce compiler error when compiled with a C++ compiler.  Following are some of them. 1)  In C++, it is a compiler error to call a function before it is declared. But in C, it may compile #include<stdio.h> int main() { foo(); // foo() is called before its declaration/definition } int foo() { printf("Hello"); return 0; } 2)  In C++, it is compiler error to make a normal pointer to point a const variable, but it is allowed in C. #include <stdio.h> int main(void) { int const j = 20; /* The below assignment is invalid in C++, results in error In C, the compiler *may* throw a warning, but casting is implicitly allowed */ int *ptr = &j; // A normal pointer points to const printf("*ptr: %d\n", *ptr); return 0; } 3)  In C, a void pointer can directly...

Placement new operator in C++

Image
Placement new operator in C++ Placement new is a variation  new  operator in C++. Normal new operator does two things : (1) Allocates memory (2) Constructs an object in allocated memory. Placement new allows us to separate above two things. In placement new, we can pass a preallocated memory and construct an object in the passed memory. new vs  placement new Normal new allocates memory in heap and constructs objects tehre whereas using  placement new , object construction can be done at  known address . With normal new, it is not known that, at what address or memory location it’s pointing to, whereas the address or memory location that it’s pointing is known while  using placement new. The deallocation is done using  delete  operation when allocation is done by new but there is no placement delete, but if it is needed one can write it with the help of  destructor Syntax: new (address) (type) initializer As we can see, we can...

explicit specifier

explicit specifier The  explicit  specifier specifies that a constructor  or conversion function   (since C++11)  doesn't allow  implicit conversions  or  copy-initialization . It may only appear within the  decl-specifier-seq  of the declaration of such a function within its class definition. Notes A constructor  with a single non-default parameter   (until C++11)  that is declared without the function specifier  explicit  is called a  converting constructor . Both constructors (other than  copy / move ) and user-defined conversion functions may be function templates; the meaning of  explicit  doesn't change. Example Run this code struct A { A ( int ) { } // converting constructor A ( int , int ) { } // converting constructor (C++11) operator bool ( ) const { return true ; } } ;   struct B { explicit B ( int ) { } ex...

static member functions in C++

Static data members in C++ Predict the output of following C++ program: #include <iostream> using namespace std; class A { public: A() { cout << "A's Constructor Called " << endl; } }; class B { static A a; public: B() { cout << "B's Constructor Called " << endl; } }; int main() { B b; return 0; } Output: B's Constructor Called The above program calls only B’s constructor, it doesn’t call A’s constructor. The reason for this is simple,  static members are only declared in class declaration, not defined. They must be explicitly defined outside the class using scope resolution operator . If we try to access static member ‘a’ without explicit definition of it, we will get compilation error. For example, following program fails in compilation. #include <iostream> using namespace std; class A { int x; public: A() { cout << "A's constructor called " << end...