C ++ Interview Basics (b)

1. The difference between the pointer array

Array subscript operation actually carried out by a pointer.
Delegates array pointing to the array element of the pointer index is 0, but there are exceptions: sizeof (array name) Returns the size of the entire array, not the size of the pointer; & array name pointer returns a pointer to the array, rather than to the subscript array elements pointer to zero.
Array name as a parameter, the name of the array will be converted to pointing to the array element at index 0 of the pointer.
Pointer operation may be higher than under standard operating efficiency, but not necessarily have good maintainability index operation.
Arrays and pointers are not equal.

2. What wild pointers?

It is a wild pointer to an object deleted or apply for access to restricted areas of memory pointer

3. introduce smart pointers in C ++

Smart pointers primarily used to manage the heap memory allocated, it is a common package stack pointer object. When the end of the life cycle of the stack of objects, the application will be freed in memory destructor, to prevent a memory leak. C ++ 11 most commonly used is the shared_ptr smart pointer type, which uses a reference counting method, the current record number is referenced memory resources smart pointer. The reference count is allocated on the heap memory. Plus 1, decrements the reference count when a new one when the reference count expires. Only when the reference count is 0, the smart pointer will automatically release the memory resource reference. You can not assign an ordinary pointer when initialized directly to shared_ptr smart pointer as a pointer, a class is. It may be passed through an ordinary pointer make_shared the constructor or function. And can be obtained by an ordinary pointer get function.

4. Is there a smart pointer memory leak case

When two objects with each other using a shared_ptr member variable pointing at each other, will result in a circular reference, the reference count failure, resulting in a memory leak.

5. How smart pointer resolve memory leaks

To resolve memory leaks caused by circular references introduced weak_ptr weak pointer, the constructor does not modify weak_ptr reference count value, so as not to manage memory object, which is similar to a normal pointer, but does not point to the shared reference count memory, but it can detect whether the managed object has been released in order to avoid unauthorized access.

6. Why destructor must be virtual? Why default C ++ destructor not virtual test sites: virtual function destructor

The destructor may be inherited parent class is set to the virtual function, it ensures that when we new subclass, then the base class pointer to the subclass object, release a base pointer can be freed space subclass, prevent memory leaks.
The default C ++ destructor virtual function is not needed because the additional virtual function tables and virtual virtual function table pointer, additional memory. For not inherited class, its destructor If a virtual function, memory is wasted. Thus the default C ++ destructor not virtual, but only when it is necessary as a parent class virtual function set.

In effect 7.C ++ destructor

Constructor and destructor corresponds, when the object end of their life cycle, such as where the object function has been invoked completed, the system will automatically execute the destructor.
Destructor name should be the same as the class name, just add a bit inversion function name in the symbol ~, ~ e.g. Stud (), to distinguish it from the constructor. It can not take any parameters, no return value (void type including). Only one destructor, can not be overloaded.
If the user does not write destructor, the compiler will automatically generate a default destructor (even custom destructor, the compiler always a destructor will be synthesized for us, and if the custom destructor function, the compiler when executed from the first call destructor defined recall synthesized destructor), it does not perform any operation. So many simple without explicit class destructor.
If a class has a pointer, and during use in a dynamic application memory, it is preferable to display the destructor prior to the destruction of the class, the application of the freed memory space, to avoid a memory leak.
Class destructor sequence: 1) itself derived class destructor; 2) destructor object member; 3) base class destructor.

8. The difference between the static functions and virtual functions

Static function at compile time it has been determined that the time is running, the virtual function dynamic binding at run time. Because the virtual function with a virtual function table mechanisms, will increase when calling a memory overhead

9. talk overloading and cover

Overload: two functions the same name, but different parameter list (number, type), the type of the return value is not required, in the same scope
rewritten: subclass inherits the parent class, is the parent class virtual function, in a subclass of this virtual function is redefined, this situation is rewritable

10. talk strcpy and strlen

strcpy string copy function prototype:
char strcpy (char dest, const char * the src);

Byte by byte copy from src to dest, until it encounters '\ 0' end, because there is no specified length, may lead to cross-border copy, resulting in a buffer overflow vulnerability, security version is strncpy function.
strlen function is a function of the calculated length of the string, returns the number of characters from the beginning to '\ 0' between.

11. you understand virtual functions and polymorphism

Multi-state is divided into static and dynamic multi-state multi-state, multi-state mainly static overloaded, it has been determined at compile time; dynamic polymorphism is implemented with virtual functions mechanism, dynamic binding at runtime. For example: a type of the parent class pointer to a subclass object when a pointer used to call the parent class subclasses the overridden virtual function of the parent class when the child class function calls after rewriting, in the parent class is declared as a function of the addition of the virtual keyword, rewriting time also you do not need to add virtual virtual function in a subclass.
Implement virtual functions: the class has virtual functions in the beginning like part is a pointer to a virtual function table, the pointer to a virtual function table, the table put the address of the virtual function, the actual virtual function snippet (.text) in. When a subclass inherits the parent class time will inherit the virtual function table, when subclasses override the virtual function in the parent class time, will be replaced by its successor to the virtual function table address re-write function address. Use the virtual function will increase access memory overhead, reducing efficiency.

12. The answer about the difference between ++ i and i ++ are

++ i to increment 1, then return, i ++ first returns i, then incremented by one

13. talk about is how in C ++ defined constant? Constants stored in the memory of which position?

Constants defined in C ++ is a plus top-level const object type, defined constants must be initialized. For topical object constants stored in the stack area, for global object stored in the global constant / static storage area. For literal constant, constant, constant stored in the storage area.

14. What answer the new / delete and malloc / free is the difference

First, new / delete the C ++ keyword, and malloc / free is a C language library functions, which must indicate the size of the application using the memory space, object classes for types, which do not call the constructor and destructor

15. The virtual function table to talk about how to achieve specific run-time polymorphism?

Subclasses override if the parent class virtual function, virtual function table, the address of the function is replaced, for the object class virtual function is present, the VS, the head object model objects contain the pointer to the virtual function table this is achieved by the polymorphic mechanism.

16. C language to talk about is how to make function calls?

Each function call will allocate function stack, perform functions within the process stack. Before calling, put the return address onto the stack, then the current function pointer push esp.

17. talk fork, wait, exec function

Parent process generates a child process using fork out a copy of a copy of the parent process, only this time a copy of the parent process page tables, both processes are reading the same piece of memory, when there is the process of using the time to write realistic copy mechanism to allocate memory, exec function elf can load a file to replace the parent process, from the parent and child processes can run a different program. fork returns child process from the parent process pid returns 0. calling the parent wait blockage will occur from the child until the child process status change, the implementation of successful return 0, -1 error. exec successful execution of the child process starts running from the new program, no return value, failed to return -1

18. The answer about the difference between static functions and virtual functions

Static function at compile time it has been determined that the time is running, the virtual function dynamic binding at run time. Because the virtual function with a virtual function table mechanisms, will increase when calling a memory overhead

19. C ++ memory management to talk about what?

In C ++, virtual memory is divided into six parts of the code segment, data segment, BSS segment, heap, file mapping area and stack area.
Code segments: a read-only memory area and a text area, wherein the read-only storage area storing a string constant, a machine code program stored in the text area.
Data segments: a program stored in the initialized global and static variables
bss group: storage uninitialized global and static variables (global + local), and all variables are initialized to 0 and the global static variable.
In the dynamic allocation of memory heap, and you need to call delete when you call new / malloc function / free to manually release the allocated memory: heap area.
Mapping area: storage and calling dynamic link library file mapping functions mmap
the stack: return address, parameters, local variables using the stack space to store function return value

20. talk about memory allocation C ++ / C's

4G 32bitCPU addressable linear space, each process has its own separate 4G logical address, wherein 0 ~ 3G is the user mode space, 3 ~ 4G is the kernel space, the same logical address different processes will be mapped to different physical addresses . Its logical address divided as follows:
the individual segments as follows:
3G user space and kernel space 1G
static area:
text segment (code segment): a read-only memory area and a text area, wherein the read-only storage area storing a string constant, the text area machine code, storing the program.
data segment (data segment): a program stored in the initialized global and static variables
bss segment: storing uninitialized global and static variables (local + global), and all initialized global and static variables 0, for uninitialized global and static variables, run the main program will be cleared when the unified before. That uninitialized global variables compiler is initialized to 0
dynamic area:
heap (heap): When the process is not malloc call is no stack segment, using assigned a heap when only calling malloc, and can be dynamically increased in the course of running heap size (break pointer movement), increased from low to higher addresses. Using this small area when allocating memory. Identified by the start address of the stack structure in mm_struct start_brk, end address identified by brk.
memory mapping segment (map area): store dynamic link library and other documents mapping application for large memory (called when malloc mmap function)
Stack (Stack): Using the stack space to store the function's return address, parameters, local variables, return values, from high address to the low address growth. When you create a process will have a maximum stack size, Linux can be specified by ulimit command.

Guess you like

Origin www.cnblogs.com/Hijack-you/p/11564998.html