C++ some common interview questions---continuous update

1. What is the difference between variable declaration and definition

The definition of the variable allocates the address and storage space for the variable, and the declaration of the variable does not allocate the address. A variable can be declared in multiple places, but only defined in one place. At the same time, add the declaration of the extern modified variable, indicating that this variable will be defined outside the file or in the back part of the file.
Description-Many times a variable is just declared that it will not allocate memory space, and will not be initialized until it is specifically used, such as external variables.

int main()
{
extern int A;
//这是个声明而不是定义,声明A是一个已经定义了的外部变量
//注意:声明外部变量时可以把变量类型去掉如:extern A;
dosth(); //执行函数
}
int A; //是定义,定义了A为整型的外部变量

2. Briefly describe the role of #ifdef, #else, #endif and ifndef

In the program, you can use #ifdef and #endif to specially define the code of a certain module to provide this function to specific users. Users can block them when they are not needed.

#ifdef MATH
#include "math.c"
#endif

At the same time, when dealing with hardware compilation under the window, you can use this kind of compilation to bypass the hardware and directly compile to get the expected result.
Note-Although the if statement can be used directly without the conditional compilation command to meet the requirements, the target program is long and the running time is long. When the conditional compilation is performed, the compiled statements can be reduced, thereby reducing the length of the target program and the running time .

3. Write an if statement for comparing int, bool, float, pointer variable and "zero value"

//int与零值比较
if ( n == 0 )
if ( n != 0 )
//bool与零值比较
if (flag) // 表示flag为真
if (!flag) // 表示flag为假
//float与零值比较
const float EPSINON = 0.00001;
if ((x >= - EPSINON) && (x <= EPSINON) //其中EPSINON是允许的误差(即精度)。
//指针变量与零值比较
if (p == NULL)
if (p != NULL)

4. Can the structure be assigned directly?

It can be initialized directly when declared, and different objects of the same structure can also be directly assigned. However, you must be careful when the structure contains pointer "members".
Note-When there are multiple pointers to the same memory, releasing this memory by a pointer may cause illegal operations of other pointers. Therefore, you must ensure that other pointers no longer use this memory space before releasing

5. The difference between sizeof and strlen

sizeof is an operator, strlen is
the parameter of the library function sizeof, which can be a data type or a variable. And strlen can only use a string ending with'\0' as a parameter. The
compiler calculates the result of sizeof at compile time, while the strlen function must be calculated at runtime, and the data type calculated by sizeof occupies the size of the memory. And strlen calculates the actual length of the string
as the parameter of sizeof does not degenerate, and it degenerates into a pointer when passed to strlen

6. What is the difference between the keyword static of c language and the keyword static of C++

In c, the static keyword is used to modify local static variables and external static variables and functions. In addition to the above functions in C++, it is also used to define class member variables and functions. That is, static members and static member functions.
Attention-static memory and global characteristics in programming can allow functions called at different times to communicate and transfer information, while C++ static members can be carried out between multiple object instances Communication

7. The difference between Malloc in c language and new delete in C++

New and delete are operators and can be overloaded.
Malloc can only be used in C++ . Free is a function that can be overwritten. Both C and C++ can use
new to call the object's constructor. The corresponding delete calls the destructor.
malloc only allocates memory, free only reclaims memory, and does not perform construction and destructor functions.
New and delete return pointers of a certain data type. Malloc and free return void pointers.
Note-the memory space applied by malloc must be released with free. The memory space applied for by new should be released with delete instead of mixed

8. Write a "standard" macro MIN

# d e f i n e m i n ( a , b ) ( ( a ) < = ( b ) ? ( a ) : ( b ) )

9. The difference between ++i and i++

++i increments 1 and then returns
i++ returns 1 first, then increments 1

10. What is the role of volatile

Parallel device hardware registers such as status registers. A
non-automatic variable accessed by an interrupt service subroutine
. Variables shared by several tasks among multiple threads.
Note-Although volatile is used more in embedded applications, there are more software on the PC side. In threads, volatile modified critical variables are also very practical

11. Can a parameter be both const and volatile

The answer is yes. Using const and volatile to modify the variable at the same time means that the variable is read-only inside the program and cannot be changed. It can only be changed under external changes, and the compiler will not optimize this variable. Every time you use this variable, you must be careful to read the value of this variable from the memory instead of going to the register to read its backup.
Note-We must pay attention to the meaning of const here. const just does not allow the code in the program to change a certain variable, and it plays a role during compilation. It does not actually prohibit the read and write of a certain section of memory.

12. What is the difference between a and &a?

&a means when the address of variable a
has different meanings in different places.
In the declaration statement, a only shows that a is a variable, int a is
in other statements, when there is no operand in front of a and a is a pointer, a represents the data stored in the address pointed to by pointer a, such as b=*a

13. Compile an endless loop program with c

while(1)
{ }

Note-Many ways can achieve the same function, but different methods have different time and space occupancy, especially for reading and embedded software, the processor speed is relatively slow, and the storage space is small, so choose a variety of time and space advantages The first consideration of the method.

14. What is the difference between global variables and local variables? How is it achieved? How do the operating system and compiler know?

All variables are variables that can be accessed by the entire program. Anyone can access them. The lifetime of the entire program is from run to end-the memory occupied at the end of the program is released.
Local variables exist in the module (subroutine, function), and only the module is located. Can be accessed, other modules cannot be directly accessed. After the module is called, the local variables disappear, and the occupied memory is released. The
operating system and compiler may know the location of memory allocation. Global variables are allocated in the global data segment and in the program. It is recorded when it is run, and local variables are allocated on the stack.

15. Briefly describe the memory allocation of C and C++ program compilation

Allocation from the static storage area-the memory is allocated when the program is compiled, and this memory exists during the entire running period of the program. It is fast and not easy to make mistakes, because there is a system that will clean up the aftermath. For example, global variables, static variables, constant strings, etc.
are allocated on the stack-when the function is executed, the storage units of the local variables in the function are created on the stack, and these storage units are automatically released when the function is executed.
Allocation on the heap-dynamic memory allocation. When the program is running, use malloc or new to apply for any size of memory, and the programmer is responsible for when to use free and delete to release the memory. The lifetime of dynamic memory is determined by the programmer. If space is allocated on the heap, it is the responsibility to reclaim it, otherwise the running program will have a memory leak. Frequent allocation and release of different sizes of heap space will produce heap fragments

Guess you like

Origin blog.51cto.com/13475106/2586629