http://www.dietmar-kuehl.de/mirror/c++-faq/index.html
Q: What is the difference between malloc() and calloc() in C?
A: malloc() allocates a block of memory of a specified size but does not initialize it (the memory contents are indeterminate). calloc() allocates memory for an array of elements and initializes all bytes to zero.
Q: What is a “dangling pointer” and how can you avoid it?
A: A dangling pointer is a pointer that points to a memory location that has been deallocated. To avoid it, ensure that after freeing memory, the pointer is set to NULL to prevent accidental access.
int *ptr = (int *)malloc(sizeof(int));
free(ptr);
ptr = NULL; // Safe practice to prevent dangling pointer
Q: Explain the use of pointers and memory allocation in C?
A:
Osnovne fukcije za upravljanje dinamicke memorije: malloc, calloc, realloc, free
Q: Razlika između pokazivača i referenci?
A: TODO
Q: Objasniti knjučnu reč const
A: const
označava objekte koji ne mogu da se menjaju.
int a = 200 // ceo broj
const int b = 200 // konstantan ceo broj
const int *p = &a; // pokazivac na const ceo broj
// *p = 5 GRESKA
p = &b
// Ono na šta pokazuje ne može da se menja
// ali može da pokazuje na više stvari:
const tip *name = ...
Ako predefinisemo izlazne i ulazne operatore za neki nas tip, onda za ostream drugi argument je const jer ga samo ispisujemo, a za istream drugi ne sme da bude const jer ce se promeniti u funkciji.
Q: Razlika izmedju strukture i klase u C++?
A: Postoje dve ključne razlike:
Q: Šta predstavlja ključna reč virtual
u C++?
A: Označava da funkcija moze da se override-uje u izvedenoj klasi od neke bazne klase. Takođe znači
Postoje i ciste virtuelne funkcije koje se iznacavaju sa virtual <tip> <name() = 0
. One nemaju svoju definiciju već moraju da se ona napise u izvedenoj klasi. Drugi naziv za njih je abstract function.
Any class with one or more pure virtual functions becomes an abstract base class, which means that it can not be instantiated! Also any derived class must define a body for this function, or that derived class will be considered an abstract base class as well.
Any class with pure virtual functions should also have a virtual destructor.
Q: Sta znaci = default
, a sta = delete
unutar klasa?
A: Oni mogu samo da se koriste sa kostruktorom, destruktorom, operator dodele, kostruktorom kopije, konstruktorom pomeranja, operatorom pomeranja??
= default
- means that you want to use compiler-generated version of that function.= delete
- means that you want to disable that function, preventing certain operation.Q: <iostream>
vs <stdio.h>
A:
Upotreba <iostream>
je u nacelu efikasnija nego upotreba <stdio>
, zato sto se prepoznavanje tipova vrsi vec pri prevodjenju.
Ipak, kod nekih verzija prevodilaca postoje neki faktori usporavanja koji to mogu da obrnu:
\n
<stdio>
, upotreba tokova se interno uskladjuje sa upotrebom printf, sto dovodi do dodatnih usporenja. Ako se u programu ne koristi <stdio>
, onda to moze da se iskljuci pozivom: std::ios::sync_with_stdio(false)
Q: How does the function calling work in C/C++? A:
Kako se prosledjuju argumenti? C - po vrednosti, kopira se argument. Promene nad parametrima unutar funkcije ne uticu na originali parametar. Sa pokazivacima - kopira se adresa promenljive i omogucava da se promeni original. C++ - po referenci, adresa promenljive se direktno prosledjuje, sve promene unutar funcije uticu na originali parametar.
Pravi se novi stek frejm/okvir -
How the Call Stack Works
Q: Explain classes and objects in C++
A:
Classes: A class in C++ is a blueprint for creating objects. It defines a type that encapsulates data (attributes) and functions (methods) that operate on the data. A class can include constructors, destructors, member functions, and access specifiers (public
, private
, protected
).
Objects: An object is an instance of a class. When a class is defined, no memory is allocated until an object of that class is created. Each object has its own copy of the class’s attributes and can call the class’s methods.
Q: What is inheritance in C++? How does it work?
A:
To je mehanizam koji omogucava da klasa (izvedena) nasledi atribute i metode druge klase (bazne). Time se omogucava ponovna upotreba koda kao i uspostavljanje hijerarhije. C++ podrzava viseklasno nasledjivanje (problem dijamanata).
Key Point: Derived classes can override base class methods.
Q: Describe polymorphism and provide an example
A: TODO
Q: What are the differences between private
, proteced
and public access modifiers?
A:
private
:
- clanovi klase su dostupni samo unutar nje - nisu dostupni izvan nje, kao ni izvedenim klasama
proteced
:
- clanovi su dostupni unutar same klase kao i u izvedenim klasama - inace, nisu dostupni
public
:
- clanovi klase su dostupni svuda, izvan klase, unutar, izvedene klase.
Q: What are destructors and constructors and how are they used in cpp?
A:
Constructor: Initializes the object when it is created. Tj. njegove clanove. Postoje tri vrste konstruktora: podrazumevani, parametrizovani, copy konstruktor
Destructor: Cleans up resources when the object is destroyed (goes out of scope or is explicitly deleted). Kada se objekat unistava bilo sa delete ili kad dodje do kraja scope-a.
Q: What is a virtual function? Explain dynamic polymorphism.
A:
Virtuelna funkcija je ona f-ja bazne klase koja moze biti override u izvedenoj klasi. Omogucava dinamicki polimorfizam. It enables dynamic (or runtime) polymorphism by ensuring that the correct function is called for an object, regardless of the type of reference (base or derived).
Dynamic polymorphism is achieved through virtual functions and inheritance. It allows a derived class’s function to be called through a base class pointer or reference at runtime, enabling the behavior to be determined dynamically.
#include <iostream>
class Animal {
public:
virtual void sound() const { std::cout << "Animal sound" << std::endl; }
virtual ~Animal() {} // Virtual destructor
};
class Dog : public Animal {
public:
void sound() const override { std::cout << "Woof!" << std::endl; }
};
class Cat : public Animal {
public:
void sound() const override { std::cout << "Meow!" << std::endl; }
};
int main() {
Animal* animal = new Dog();
animal->sound(); // Outputs: Woof!
animal = new Cat();
animal->sound(); // Outputs: Meow!
delete animal;
return 0;
}
Q: What is the “Rule of Three” in C++?
A: The Rule of Three states that if a class requires a custom implementation of a destructor, copy constructor, or copy assignment operator, then it likely requires custom implementations of all three to manage resources properly.
Q: Why is the size of an empty class not zero?
A: To ensure that the addresses of two different objects will be different.
class Empty { };
void f()
{
, b;
Empty aif (&a == &b) cout << "impossible: report error to compiler supplier";
* p1 = new Empty;
Empty* p2 = new Empty;
Emptyif (p1 == p2) cout << "impossible: report error to compiler supplier";
}
Q: Difference between C and C++ (e.g. pointers, memory managment, OO programming
A:
C:
- Proceduralni jezik (fokus na funkcije i strukturno prog) - koristi malloc i free za upravljanje memorijom - koristi pokazivace - jednostavna std biblioteka - nema overloading funkcija - koristi errno i error codes for error handling
C++:
- OOP i proceduralni jezik - Ima: klase, nasledjivanje, polimorfizam, enkapsulacija - new i delete - Pored pokazivaca ima i smart pointers: unique_ptr, shared_ptr - bogata std biblioteka - try, catch, throw za error handling
Q: What is a “memory leak” in C/C++, and how do you prevent it?
A: Kada se memorija alocira sa malloc, calloc i new ali se ne oslobodi sa free ili sa delete. To za posledicu ima da ta memorija postaje nedostupna za ponovno koriscenje. Tokom vremena ce ta nedostupna memorija rasti sto moze da uspori ili da prekine program.
Prevenica:
free
ili delete
kada memorija nije potrebna.unique_ptr
, shared_ptr
Q: What are the advantages of using const
and volatile
keywords in C/C++?
A:
const
volatile
Q: Koje vrste memorije postoje?
A:
Q: Sta je procesor, od cega se sastoji?
A: Mozak kompijutera, zaduzen za izvrsavanje instrukcija i izracunavanja.
Here are concise answers to each question:
microkernel? - Monolithic kernel** is a single large kernel that includes all core OS services, offering faster performance but less modularity. Microkernel has a smaller core, with other services running as separate processes, offering more stability and flexibility but at a performance cost.
These answers provide a solid foundation for these topics in a technical interview.