https://stackoverflow.com/questions/29285546/when-do-so-files-get-loaded-linux
Thursday, 9 November 2017
Monday, 11 September 2017
Threads
http://www.cs.iit.edu/~cs561/cs450/ChilkuriDineshThreads/dinesh%27s%20files/User%20and%20Kernel%20Level%20Threads.html
http://www.geeksforgeeks.org/multithreading-c-2/
Kernel level threads are 100 times slower than user level threads as kernel need to manage other processes as well & a thread is a overhead for the kernel. It makes things more complex for the kernel.
http://www.geeksforgeeks.org/multithreading-c-2/
Kernel level threads are 100 times slower than user level threads as kernel need to manage other processes as well & a thread is a overhead for the kernel. It makes things more complex for the kernel.
Important C++ concepts
-static member functions can directly be accessed. No need to create class object for accessing static variables.
-static classes do not exist.
-private constructors:
private constructors exist, private destructors do not.
private constructors are used to create a singleton class.
http://www.yolinux.com/TUTORIALS/C++Singleton.html
protected constructors also exist. They are created so that only child class can call its constructor.
Virtual Destructors:
virtual destructors exist in C++. If we have a derived class object stored in base class pointer & if we delete that pointer, it will be undefined behaviour. So, we use virtual destructors for this purpose.
http://www.geeksforgeeks.org/virtual-destructor/
virtual constructors do not exist as there will be no point creating an object whose constructor is virtual. Creating an object is a compile time decision.
Except inline no other keyword is allowed with constructors.
Copy Constructor:
http://quiz.geeksforgeeks.org/copy-constructor-in-cpp/
http://www.geeksforgeeks.org/copy-constructor-vs-assignment-operator-in-c/
http://www.geeksforgeeks.org/can-virtual-functions-be-private-in-c/
-static classes do not exist.
-private constructors:
private constructors exist, private destructors do not.
private constructors are used to create a singleton class.
http://www.yolinux.com/TUTORIALS/C++Singleton.html
protected constructors also exist. They are created so that only child class can call its constructor.
Virtual Destructors:
virtual destructors exist in C++. If we have a derived class object stored in base class pointer & if we delete that pointer, it will be undefined behaviour. So, we use virtual destructors for this purpose.
http://www.geeksforgeeks.org/virtual-destructor/
virtual constructors do not exist as there will be no point creating an object whose constructor is virtual. Creating an object is a compile time decision.
Except inline no other keyword is allowed with constructors.
Copy Constructor:
http://quiz.geeksforgeeks.org/copy-constructor-in-cpp/
http://www.geeksforgeeks.org/copy-constructor-vs-assignment-operator-in-c/
http://www.geeksforgeeks.org/can-virtual-functions-be-private-in-c/
Exceptions Handling in C++
-If there is no catch statement for a try statement, then program ll not crash but will show abnormal behavior.
-try{
throw derived
}
catch(base){}
catch{derived}
The program will go in the base segment as it is written before derived statement.
-If you've one if/else block instead of one try/catch block, and if an exception throws in the try/catch block, then the if/else block is faster (if/else block: around 0.0012 milliseconds, try/catch block: around 0.6664 milliseconds). If no exception is thrown with a try/catch block, then a try/catch block is faster. But if you use 100 try/catch blocks in your program, and if one exception throws, then 100, if/else blocks, is faster.
Abstract class, sizeof class with virtual function
If we do not override the pure virtual function in derived class, then derived class also becomes abstract class.
http://www.geeksforgeeks.org/pure-virtual-functions-and-abstract-classes/
Sizeof class with virtual function maintins a virtual pointer which increases sizeof class by pointer size - 8 bytes.
http://www.geeksforgeeks.org/pure-virtual-functions-and-abstract-classes/
Sizeof class with virtual function maintins a virtual pointer which increases sizeof class by pointer size - 8 bytes.
auto and decltype
// function that returns a 'reference to int' type
int& fun() { }
// m will default to int type instead of
// int& type
auto m = fun();
// n will be of int& type because of use of
// extra & with auto keyword
auto& n = fun();
Create .so in linux & dynamic vs static libraries
One major advantage of static libraries being preferred even now “is speed”. There will be no dynamic querying of symbols in static libraries. Many production line software use static libraries even today.
To generate a shared library you need first to compile your C code with the
-fPIC (position independent code) flag.gcc -c -fPIC hello.c -o hello.o
This will generate an object file (.o), now you take it and create the .so file:
gcc hello.o -shared -o libhello.so
EDIT: Suggestions from the comments:
You can use
gcc -shared -o libhello.so -fPIC hello.c
http://www.geeksforgeeks.org/static-vs-dynamic-libraries/
One drawback of static libraries is, for any change(up-gradation) in the static libraries, you have to recompile the main program every time
malloc() vs new, wild, dangling ptr
malloc() vs new
Following are the differences between malloc() and operator new.
1) new calls constructors, while malloc() does not. Infact primitive data types (char, int, float.. etc) can also be initialized with new. For example, below program prints 10.
#include<iostream>using namespace std;int main(){ int *n = new int(10); // initialization with new() cout<<*n; getchar(); return 0;} |
2) new is an operator, while malloc() is a function.
3) new returns exact data type, while malloc() returns void *
http://www.geeksforgeeks.org/dangling-void-null-wild-pointers/
Headers for macros, system calls etc
types
dev_t linux/types.h
macros
MAJOR(dev_t dev) linux/kdev_t.h
MINOR
MKDEV
dev_t linux/types.h
macros
MAJOR(dev_t dev) linux/kdev_t.h
MINOR
MKDEV
Templates in c++
using with templates: http://en.cppreference.com/w/cpp/language/type_alias
SFINAE:
http://en.cppreference.com/w/cpp/language/sfinae
Types:
http://en.cppreference.com/w/cpp/types
const, static important examples must see. References ca be used as lvalue
- const object cannot call non-constant functions.
-
Some important examples on static:
1)
#include<iostream>
class Test {
static int x[10];
//int y;
};
int main()
{
std::cout << sizeof(Test) << std::endl;
return 0;
}
2)
#include<iostream>
class Test {
private:
static const int c[2] = { 1, 2 }; /// Error c should be integral type
public:
static int fun() {
return c;
}
};
int main()
{
std::cout << Test::fun() << std::endl;
return 0;
}
https://stackoverflow.com/questions/9656941/why-cant-i-initialize-non-const-static-member-or-static-array-in-class
3)
#include<iostream>
class Test {
public:
static int x;
public:
static int fun() {
return x;
}
};
int Test::x = 5; // Error if comment this line:: undefined reference, as in func() x will seem to be undefined. So, basically static variables which we want to use in static functions must be initialised somehow either in a class declaration by const static int x = 10; or outside class as per this line.
int main()
{
std::cout << Test::fun() << std::endl;
return 0;
}
4)
Even static private variables can also be initialized outside class without class object.:
5)
6)
#include<iostream>
class Test {
const static int x = 5; // Error if changes to : static int x = 5; (A)
public:
static int fun() {
return x;
}
};
int main()
{
std::cout << Test::fun() << std::endl;
return 0;
}
Errors:
(A) ISO C++ forbids in-class initialization of non-const static member 'Test::x'
-
Some important examples on static:
1)
#include<iostream>
class Test {
static int x[10];
//int y;
};
int main()
{
std::cout << sizeof(Test) << std::endl;
return 0;
}
Ans: 1, static members don't belong to the instances of class. they don't increase instances and class size even by 1 bit!
2)
#include<iostream>
class Test {
private:
static const int c[2] = { 1, 2 }; /// Error c should be integral type
public:
static int fun() {
return c;
}
};
int main()
{
std::cout << Test::fun() << std::endl;
return 0;
}
https://stackoverflow.com/questions/9656941/why-cant-i-initialize-non-const-static-member-or-static-array-in-class
3)
#include<iostream>
class Test {
public:
static int x;
public:
static int fun() {
return x;
}
};
int Test::x = 5; // Error if comment this line:: undefined reference, as in func() x will seem to be undefined. So, basically static variables which we want to use in static functions must be initialised somehow either in a class declaration by const static int x = 10; or outside class as per this line.
int main()
{
std::cout << Test::fun() << std::endl;
return 0;
}
4)
Even static private variables can also be initialized outside class without class object.:
5)
#include<iostream>using namespace std;int &fun(){ static int x = 10; return x;}int main(){ fun() = 30; cout << fun(); return 0;}When a function returns by reference, it can be used as lvalueoutput = 30 |
#include<iostream>
class Test {
const static int x = 5; // Error if changes to : static int x = 5; (A)
public:
static int fun() {
return x;
}
};
int main()
{
std::cout << Test::fun() << std::endl;
return 0;
}
Errors:
(A) ISO C++ forbids in-class initialization of non-const static member 'Test::x'
static int x = 5
Interrupts in linux
Interrupt values are often called interrupt request (IRQ) lines. Each IRQ line is assigned a numeric value—for example, on the classic PC, IRQ zero is the timer interrupt and IRQ one is the keyboard interrupt. Not all interrupt numbers, however, are so rigidly defined. Interrupts associated with devices on the PCI bus, for example, generally are dynamically assigned.
In OS texts, exceptions are often discussed at the same time as interrupts. Unlike interrupts, exceptions occur synchronously with respect to the processor clock.
Much of the discussion of interrupts (asynchronous interrupts generated by hardware) in this chapter also pertains to exceptions(synchronous interrupts generated by the processor).
In OS texts, exceptions are often discussed at the same time as interrupts. Unlike interrupts, exceptions occur synchronously with respect to the processor clock.
Much of the discussion of interrupts (asynchronous interrupts generated by hardware) in this chapter also pertains to exceptions(synchronous interrupts generated by the processor).
Subscribe to:
Comments (Atom)
