Summary code for a set of interview questions

Source: Internet
Author: User
Tags define find

Summary code for a set of interview questions

Interview Example 1:
How can I use a macro to define "find" to find the variable cnum IN THE ctype of a struct relative to the size of the ctype?
For example:


View sourceprint? Stuct student

{

Int;

Char B [20];

Double ccc;

}

Then:
Find (student, a); // equals 0
Find (student, B); // equals to 4

 

Answer:

# Define find (struc, e) (size_t) & (struc *) 0)-> e)
(Struc *) 0 // indicates to forcibly convert constant 0 to the address pointed to by struc * pointer
& (Struc *) 0)-> e)/* indicates the address of e, a member of struc * 0, because the first address of the struct is 0,
So we get the offset of member e from the first address of the struct .*/
(Size_t) // is a type of data that is defined to facilitate transplantation between different systems,
Generally unsigned int */
(Struc *) 0 // indicates that there is a structure struc at the 0 address.
(Struc *) 0)-> e // represents the struc member e at the zero address.
& (Struc *) 0)-> e) // represents the address of member e of struc at the zero address
(Size_t) & (struc *) 0)-> e) // converts the address of member e of struc at the zero address to an integer.

 

Ii. const
1: what does the keyword "const" means in c program? Please at least make
Two examples about the usage of const.

 

Parse: In c Programs, const is mainly used to define constants, modify function parameters, and modify function return values.
In the c ++ program, it can also modify the definition body of the function, and define a member function in the class as a constant state function, that is, do not change
Data member in the change class.

Answer:
(1) const constants can be defined.
(2) const can modify the parameters and return values of a function, or even the definition body of a function. All things modified by const are strongly affected.
Protection can prevent unexpected changes and improve program robustness.

2: What is the difference between const and # define?
The c ++ language can use const to define constants or # define to define constants, but the former has more advantages than the latter.

 

Answer:
(1) const constants have data types, while macro constants do not have data types. The compiler can perform security checks on the former and on the latter.
Only character replacement, no type security check, and unexpected errors (marginal effect) may occur during character replacement ).

(2) Some integrated debugging tools can debug const constants, but cannot debug macro constants.
Only use const constants instead of macro constants, that is, const constants completely replace macro constants.
For more information, click constants in c ++.

3: What should I do if I want to modify the member variables of a class in the following circumstances?
Class a_class
{
Void f () const
{

}
}
Answer:
In the const member function, after mutable is used to modify the member variable name, you can modify the member variable of the class.
Sample
# Include <stdio. h>
Class c {
Private:
Mutable int m_count;

Public:
C (int I): m_count (I ){}
Int add () const {
Return ++ m_count;
}

Int minus () const {
Return -- m_count;
}

Int print (){
Printf ("m_count = % dn", m_count );
}
};

Int main (){
C tmp (5 );
Printf ("before add :");
Tmp. print ();
Tmp. add ();
Printf ("after add :");
Tmp. print ();
Puts ("");
Printf ("before minus :");
Tmp. print ();
Tmp. minus ();
Printf ("after minus :");
Tmp. print ();
Return 0;
}
Sizeof
# Include <stdio. h>
Struct node1 {
Char a3;
Int a1;
Short a2;
};

Struct node2 {
Int a1;
Char a3;
Short a2;
};

Int main (){
Printf ("% dn", sizeof (node1 ));
Printf ("% dn", sizeof (node2 ));
Return 0;
}
Answer:
Structure alignment: The longest data unit in the structure is the alignment unit.
Therefore, the length of the struct is an integer multiple of the longest data element.
Sizeof (node1 );
| Char | --- |
| ------- Int ------ |
| -- Short-| ------- |

Sizeof (node2 );
| ------- Int ------ |
| Char | --- |-short-|
What is output by the following program?

# Include <stdio. h>
Int main (){
Int a = 4;
Printf ("% dn", sizeof (a = 6 ));
Printf ("% dn", );
Return 0;
}
Answer:
Sizeof is not a function or unary operator. It is a special keyword similar to macro definition. It is in the brackets of sizeof ().
In the compilation process, the content is not compiled, but is replaced by the type, such as int = 8; sizeof (). Compile
No matter what the value of a is in the process, it is replaced with the sizeof (int) type, and the end is 4. If it is sizeof (a = 6)
What about it? It is also converted to sizeof (int), but note that a = 6 is not compiled, so after execution,
The value of a remains unchanged.
4
4: What is the output result of the following code?
# Include <stdio. h>
Char var [10];
Int test (char var []) {
Return sizeof (var );
}
Int main (){
Printf ("% dn", test ("hello world "));
Return 0;
}
Answer:
The c/c ++ passed array is always the first address of the passed array and a pointer.
Int test (char va []); equivalent to int test (char var *); equivalent to int test (char var [8]);
As long as it is a pointer type, the size of sizeof is 4.
4


5: How much space does an empty class occupy? What about multiple inherited empty classes?
Answer: The space occupied by an empty class is 1, and the space occupied by multiple inherited empty classes is still 1.
Sample:
# Include <iostream>
Using namespace std;
Class {};
Class a1 {};
Class B: public {};
Class c: public virtual B {};
Class d: public a, public a1 {};

Int main (){
Cout <sizeof (a) <endl;
Cout <sizeof (B) <endl;
Cout <sizeof (c) <endl; // virtual inheritance design to virtual table (Virtual pointer), so the size of sizeof (c) is 4
Cout <sizeof (d) <endl;
Return 0;
}
Differences between inline functions and macro definitions

 


Compared with normal functions, inline functions can speed up program running, because they do not need to be interrupted, so they can be directly embedded into the target code during compilation.

Inline functions are used to check the parameter type, which is an advantage of inline functions over macros.

Inline refers to embedded code, that is, the code is written directly to the place where the function is called instead of the jump. For short code, inline can improve efficiency,

Inline is more secure and reliable than macro functions in the c era. However, this is at the cost of increasing space consumption. Whether or not the inline function is required depends on your actual situation.

Inline is generally only used in the following scenarios:

(1) A function is repeatedly called.

(2) The function has only a few simple rows and does not contain for, while, or switch statements.

● In general, writing a applet does not need to be defined as inline. However, if you want to complete a project, consider using inline when a simple function is called multiple times.

● Macros are extremely important in the c language, and are rarely used in c ++. The first rule about macros is: never use them unless you have. Almost every macro

It indicates a defect in the programming language or in the program or the programmer, because it will re-manipulate the body before the compiler sees the body of the program. Macro

It is troublesome for many programming tools. Therefore, if you use a macro, you should prepare only a variety of tools (such as troubleshooting systems, cross-reference systems, profile programs, etc)

. (-- Pass: macros are very convenient. You must be careful when using them. This does not mean that macros are useless, but they are reasonable if they exist)

 


Macros are a simple alternative to code without any verification, while inline functions insert Code directly into the calling place, reducing the resource consumption during normal function calls.

A macro is not a function, but replaces the string in the program with the macro body before compilation (in the pre-compilation phase.

Inline functions are functions, but they do not generate code independently during compilation. Instead, they embed the relevant code into the call.


Inline fact (float I) {return I * I;} // printf ("bb = % d", fact (8) with no return value written )); // execute printf ("bb = % d", 8*8) during the call );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.