Yesterday afternoon (3/19) Three o'clock, received a telephone from Hangzhou, is Ali's. Ask me whether it is convenient to chat, I said I am in class, four O ' class. Then he called again four o'clock.
Project experience
Come up and ask me whether there is a large project experience, I am sorry, I said no ... And asked me how much code, I said before there are often brush ACM topic, so the amount of code can also.
C Language Variables
Q: "Where are local variables stored in the function?" ”
Answer: "Stack"
Q: Where are local static variables stored in the function? ”
Answer: "Static zone. ”
Q: "Is there a difference between a local static variable and a global static variable, and where is the difference?" ”
Answer: ". Not very different, all exist together. ”
Q: "Not the storage location of the question, what about other aspects?" ”
A: "Oh, the viewable range is different." Global static variables are globally visible, and local static variables are visible only inside the function. ”
Q: What is the difference between a global variable and a global static variable?
A: "The location is next to each other, to say the different words, is also the visual range bar, global static variables are only visible within the current file, the global variable is the project all the files are visible. ”
Union (Union)
Q: "Do you know the union?" ”
Answer: "Union"
Q: "What is the difference between a struct and a structural body?" ”
A: "Each member of a union has a common starting address (shared storage space), and the struct allocates space for each member individually." ”
Q: "What is the purpose of the design of Union (what is the purpose of union)?" ”
I'm going to have a quick brainstorm. I racked my brains to express my humble opinion. This part of the content you can ignore, I think I pulled a bit too far.
"This design saves memory space, and sometimes in a particular case, we just need to use a certain type, and how to waste storage space like a struct." In the past, the structure of the IP address (struct IN_ADDR) in Linux programming (POSIX) was a union (or perhaps a struct member is a union), such as a member is a 4-element char array, a short array of two elements, or an int, and so on. This allows us to freely obtain the network number or host number of the address based on different network types (Class A, Class B, Class C) (for example, to get the network number of a network.) For a Class A address, we read the first element of the char array. Class B Address We read the first element of short) "
Now, of course, the struct in_addr is actually just a struct that contains an integral type. It's not a union. The above statement on IN_ADDR and union is from "UNP".
Algorithm for adding large numbers of algorithms
Q: "How do I add two numbers (more than a long long range)?" ”
A: "Use a character array to store numbers, and then iterate through each character, converting to a number by minus the ' 0 ' character, and then adding each bit ... ”
This is a more classical algorithm for large numbers. But he didn't actually interrupt me until I finished.
Q: "Yes, of course, but this method is inefficient, and there is no efficient way to do it."
Answer: "No".
Q: "Think about half a minute."
Answer: "Really will not (to oneself also no language, ask Netizen to inform algorithm)"
Other algorithms
Q: "What other algorithms do you know?"
Answer: "Most of the data structure involves the algorithm, BFS,DFS, the smallest spanning tree, the shortest path and so on." Hash is also an algorithm, there are sorting algorithms. Others, such as the data structure like and check it. ”
About the algorithm I did not dare to mention, because I am also afraid of him in depth to ask, for a long time did not make algorithm, this is not prepared, will certainly kneel.
But he didn't go into it.
Books
Q: "You have no project experience, have you read any classic books?" ”
A: "C + + Primer,think in C + + has read a little. (actually read a little of the classic books there are many.) )”
Const pointer
Q: "Declare a constant pointer, point to an integer, but point to an address that is immutable"
Ah, this I know is the focus, but also easy to confuse the knowledge point, the other day I also specially tidied up a bit. However, give me a little time to comb my own can answer well, he this question, I found that I still do not hold firm.
The answer is wrong. He gave me a second guidance.
The correct answer is: const int * A (int const * a).
int * Const A is the value of the integral type that is pointed to is immutable and the pointer itself is mutable.
--------------------------------------------------------------------------------------------------------------- -----------------------------------------------
To summarize, the shorthand method: The key is the position of the const and asterisk (*). int is always on the left of the asterisk. Just remember to "reverse". can be ignored to int. Then there are only two forms of
CONST * A and * const A. The Surface const * A const in front of the asterisk should be a modifier pointer, but to reverse memory, it is a modifier variable. That is, a variable is a constant.
* On the surface of const A, the cosnt should be a modifier variable in front of a, in fact it is a modifier pointer, that is, the address is constant cannot change.
These are just shorthand methods, not the design intentions of C language designers ...
--------------------------------------------------------------------------------------------------------------- -----------------------------------------------
Memory alignment
Q: "For example, you malloc a memory, its address is not memory-aligned, how to achieve 8-byte memory alignment?" ”
A: "A pre-#pragma can be implemented (#pragma pack (8))"
Q: "This is implemented with a compiler, is there a software approach?" ”
Next, at his prompt, I guessed the answer.
Answer: "Determine if the memory address of malloc is not memory-aligned"
Q: "How to judge?" ”
A: "8-byte alignment, then the memory address should be a multiple of 8, you can%8 (to 8 redundancy)"
Q: "This involves division operations, which are less efficient." ”
Answer: "Then use the bit operation, you can bitwise with, the front several is 0 after three bits is 1, oh, I say is binary (decimal 7)." Then determine whether the value is 0 "
Q: "If the result is not aligned, how do I align it?" ”
And then it was all my speculation.
Answer: "Then give the address pointer to add, how much difference to add, may also be based on the pointer type for some conversion." "( a bad answer.) However, he did not raise any objection, the next question.
----------------------------------------------------------------------------------------------------------- ---------------------------------------------------
Then I manually tapped the code, the problem is that the pointer is not directly to the redundancy or bit operation, the pointer to the type of int cast is a forced type failure. There are two ways to choose from:
If it is C + +, you can use static_cast to transform, such as:
int pp = static_cast<int> (p); P is pointer type
In the case of C, I defined a transformation function:
int Ptoi (char *p) { char a[20]; sprintf (A, "%p", p); Return Aoti (a);}
It is also important to note that the return value of malloc is best to force type conversion to char *:
For example, allocate 100 characters of space. Char *p = (char *) malloc (sizeof (char) * (100+8)); Allocate more than 8 bytes of space, in order to offset the sufficient space later.
Although the return value of malloc can theoretically be converted to any pointer type, such as int *. But notice the addition and subtraction of the pointer, and the offset is the size of the pointing type. Like what:
if p is int * Type p + = 1; P offsets 1*4 bytes (int is 4 bytes)//if P is char * type p + = 1; P Backward offset 1*1 bytes (char is 1 bytes)
It is clear that the pointer offset of the char * type is more accurate. This is why we typically convert malloc return values to char * instead of int *.
----------------------------------------------------------------------------------------------------------- ---------------------------------------------------
callback function
Q: "How to implement callback functions in C + +"
callback function, very familiar name, callback ... But it's really hard to say exactly what it means. I remember seeing it in Android. Just a little bit of Android.
Q: "So how do you do it in C + +?"
And then, indeed, luck. A function pointer came out of the head, blurted out, said a general function pointer usage. It seems to be right.
A: "function pointer, first what kind of function pointer, then you can implement this type of function, and then pass the function as a parameter to the function (the parameter is a function pointer function)." ”
Memory allocation principle
Q: "Have you seen the source of memory allocation management?" Like malloc or something like that. ”
A: "No, that's probably a compilation" (remember Linus said that early malloc was made with a compilation.) I don't know now. )
Q: "is not the specific language involved, is the memory management algorithm understand it?" ”
A: "I have not seen this aspect of the understanding." ”
Then the problem is over. Now, think about what he meant. I would like to talk about the memory management algorithm from the knowledge aspect of the operating system, such as scanning, where unused space is allocated.
Then asked me what the problem was. I have no problem at all. Asked a little mentally retarded question.
Q: "You're going to call (and find an inside push in a group). Or do you call all the people who apply for an internship at the official office? ”
A: "We will call all the applications we have." ”
Remember Ali Electric plane experience