Hundred battles c++ (6)

26. Describe the memory allocation methods and their differences?

1) Allocated from the static storage area. The memory is allocated when the program is compiled, and this memory exists throughout the running of the program. Such as global variables, static variables.

2) Created on the stack. When a function is executed, the storage units of local variables in the function can be created on the stack, and these storage units are automatically released when the function execution ends. Stack memory allocation operations are built into the processor's instruction set.

3) Allocation from the heap, also known as dynamic memory allocation. When the program is running, use malloc or new to apply for any amount of memory, and the programmer is responsible for when to use free or delete to release the memory. The lifetime of dynamic memory is determined by the programmer, and it is very flexible to use, but it also has the most problems.

27. The difference between struct and class

Answer: The members of a struct are public by default, while the members of a class are private by default. struct and class are otherwise functionally equivalent.

Emotionally, most developers feel a big difference between classes and structures. It feels like a structure is just a bunch of open bits of memory lacking encapsulation and functionality, whereas a class is like a living and reliable member of society with intelligent services, a solid encapsulation barrier and a well-defined interface. Since most people think so, you should probably use the struct keyword only if your class has few methods and has public data (this kind of thing exists in a well-designed system!), otherwise, You should use the class keyword.

28. When a class A does not declare any member variables and member functions, what is the value of sizeof(A) at this time, if it is not zero, please explain why the compiler did not make it zero. (Autodesk)

Answer: Definitely not zero. As a counter example, if it is zero, declare an array of class A[10] objects, and the space occupied by each object is zero, then there is no way to distinguish A[0], A[1]....

29. Under 8086 assembly, how are logical addresses and physical addresses converted? (Intel)

Answer: The address given by the general-purpose register is the offset address within the segment, and the address of the corresponding segment register *10H+the address in the general-purpose register is the address to be accessed.

30. Compare the 4 types of conversion in C++?

Please refer to: http://blog.csdn.net/wfwd/archive/2006/05/30/763785.aspx, focusing on the difference and application of static_cast, dynamic_cast and reinterpret_cast.

31. Write out the comparison statements between variable a of BOOL, int, float, and pointer type and "zero".

Answer:

BOOL : if ( !a ) or if(a)

int : if ( a == 0)

float : const EXPRESSION EXP = 0.000001

if ( a < EXP && a >-EXP)

pointer : if ( a != NULL) or if(a == NULL)

32. Please tell me what are the advantages of const compared with #define?

Answer: 1) const constants have data types, but macro constants do not. The compiler can perform type safety checks on the former. For the latter, only character replacement is performed, without type safety checks, and unexpected errors may occur during character replacement.

2) Some integrated debugging tools can debug const constants, but cannot debug macro constants.

33. Briefly describe the difference between an array and a pointer?

Arrays are created either in static storage (like global arrays) or on the stack. A pointer can point to any type of memory block at any time.

(1) Differences in revised content

char a[] = “hello”;

a[0] = ‘X’;

char *p = "world"; // note that p points to a constant string

p[0] = 'X'; // Compiler can't find this error, runtime error

(2) Use the operator sizeof to calculate the capacity (number of bytes) of the array. sizeof(p), p is a pointer to get the number of bytes of a pointer variable, not the memory capacity pointed to by p. The C++/C language has no way to know the memory capacity pointed to by the pointer, unless it is remembered when applying for memory. Note that when an array is passed as an argument to a function, the array automatically degenerates into a pointer of the same type.

char a[] = "hello world";

char *p = a;

cout<< sizeof(a) << endl; // 12 字节

cout<< sizeof(p) << endl; // 4 字节

Calculate the memory capacity of arrays and pointers

void Func(char a[100])

{

cout<< sizeof(a) << endl; // 4 bytes instead of 100 bytes

}

34. What is the difference between overloading, overriding and hiding of class member functions?

Answer:

a. Features of member functions being overloaded:

(1) the same scope (in the same class);

(2) The function names are the same;

(3) The parameters are different;

(4) The virtual keyword is optional.

b. Coverage means that derived class functions cover base class functions, and the characteristics are:

(1) Different scopes (located in derived class and base class respectively);

(2) The function names are the same;

(3) The parameters are the same;

(4) The base class function must have the virtual keyword.

c. "Hidden" means that the function of the derived class shields the base class function with the same name. The rules are as follows:

(1) If the function of the derived class has the same name as the function of the base class, but the parameters are different. At this time, regardless of whether there is a virtual keyword, the function of the base class will be hidden (be careful not to confuse it with overloading).

(2) If the function of the derived class has the same name and parameters as the function of the base class, but the base class function does not have the virtual keyword. At this point, the functions of the base class are hidden (note not to be confused with overriding)

35. There are two int variables: a and b, don’t use “if”, “? :”, “switch”or other judgement statements, find out the biggest one of the two numbers.

Answer: ( ( a + b ) + abs( a - b ) ) / 2

36. How to print out the file name of the current source file and the current line number of the source file?

Answer:

cout << __FILE__ ;

cout<<__LINE__ ;

__FILE__ and __LINE__ are system predefined macros, which are not defined in a file, but defined by the compiler.

37. After the main function is executed, is it possible to execute another piece of code and give an explanation?

Answer: Yes, you can register a function with _onexit, which will execute int fn1(void), fn2(void), fn3(void), fn4 (void) after main;

void main( void )

{

String str("zhanglin");

_onexit( fn1 );

_onexit( fn2 );

_onexit( fn3 );

_onexit( fn4 );

printf( "This is executed first.\n" );

}

int fn1()

{

printf( "next.\n" );

return 0;

}

int fn2()

{

printf( "executed " );

return 0;

}

int fn3()

{

printf( "is " );

return 0;

}

int fn4()

{

printf( "This " );

return 0;

}

The _onexit function is passed the address of a function (func) to be called when the program terminates normally. Successive calls to _onexit create a register of functions that are executed in LIFO (last-in-first-out) order. The functions passed to _onexit cannot take parameters.

38. How to judge whether a program is compiled by C compiler or C++ compiler?

Answer:

#ifdef __cplusplus

cout<<"c++";

#else

cout<<"c";

#endif

39. There is a set of integers in the file, which needs to be sorted and output to another file

Answer:

#i nclude<iostream>

#i nclude<fstream>

using namespace std;

void Order(vector<int>& data) //bubble sort

{

int count = data.size() ;

int tag = false ; // Set the flag bit whether to continue bubbling

for ( int i = 0 ; i < count ; i++)

{

for ( int j = 0 ; j < count - i - 1 ; j++)

{

if ( data[j] > data[j+1])

{

tag = true ;

int temp = data[j] ;

data[j] = data[j+1] ;

data[j+1] = temp ;

}

}

if ( !tag )

break ;

}

}

void main( void )

{

vector<int>data;

ifstream in("c:\\data.txt");

if ( !in)

{

cout<<"file error!";

exit(1);

}

int temp;

while (!in.eof())

{

in>>temp;

data.push_back(temp);

}

in.close(); //Close the input file stream

Order(data);

ofstream out("c:\\result.txt");

if ( !out)

{

cout<<"file error!";

exit(1);

}

for ( i = 0 ; i < data.size() ; i++)

out<<data[i]<<" ";

out.close(); //Close the output file stream

}

Guess you like

Origin blog.csdn.net/hebtu666/article/details/127205260