2015 a classic question written by Baidu, and 2015 a classic question written by Baidu

Source: Internet
Author: User

2015 a classic question written by Baidu, and 2015 a classic question written by Baidu

Encode and implement the memcpy function: void * memcpy (void * dst, const void * src, unsigned int count) is obviously a memory replication function

The following is a test case based on the source code of memcpy.

# Include <stdio. h> void * memcpy (void * dst, const void * src, unsigned int count) {char * p_dst = (char *) (dst); char * p_src = (char *) (src); unsigned int I; if (p_dst> p_src & p_dst <= (p_src + count-1) {while (count) {* (p_dst + count-1) = * (p_src + count-1); count -- ;}} else {for (I = 0; I <count; I ++) {* (p_dst + I) = * (p_src + I) ;}} return dst ;}int main () {int arr [] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; memcpy (arr + 4, arr, sizeof (int) * 6 ); // copy src <dst & src + count-1> dst in this way, that is, overwrite is generated. // Therefore, use the forward copy from the back, enter if for (int I = 0; I <10; I ++) printf ("% d \ t", arr [I]); // memcpy (arr, arr + 4, sizeof (int) * 6); // copy src> dst & src <dst + count-1, // This is the same as normal copy, enter else // for (int I = 0; I <10; I ++) // printf ("% d \ t", arr [I]); printf ("\ n"); return 0 ;}


1 someone may ask: Your algorithm ensures that data can be correctly copied to the destination address under any circumstances, but have you ever considered that you may have modified the original data during the copy process?
Has the function such as the original string been modified?

Answer: Yes, dude, you are right. This problem does exist. For example, hello world. We want to remove the spaces in the middle, filter out spaces is similar to the memory allocation method in the operating system, which is called the multi-position partition allocation. It is to move a small job, remove the gaps in the middle, and then empty a large memory address, for use by other programs. In this way, you can apply this function without saving the original data.

2. You may still have questions: What should I do if the last dst + count = '\ 0' is passed in a char array? How do you determine whether it is null or not when you return dst? -- Okay, this is also my problem. Please help me to answer the question ????

3Memcpy () andThe difference between normal strncpy () replication functions is as follows:

# Include <iostream> # include <cstring> // for c style string manipulationusing namespace std; int main () {char str1 [] = "To be or not to be "; // 18 characters (including spaces) + '\ 0' = 19 characters, because' \ 0' cout <sizeof (str1) <endl; char str2 [40]; cout <str1 <endl; // copy to sized buffer (overflow safe): strncpy (str2, str1, sizeof (str2 )); cout <str2 <endl; // partial copy (only 5 chars): strncpy (str2, str1, 5); cout <str2 <endl; str2 [5] = '\ 0'; // null character manually added cout <sizeof (str2) <"" <str2 <endl; strncpy (str1 + 3, str1, 6); // memory coverage problem cout <str1; return 0 ;}
Summary: The strcpy source string is all copied to the target string, including '\ 0'. However, the programmer must ensure that the target string is long enough and does not overlap the source string. Strncpy if the target length >=specify the length> the source length, the source string is copied to the target string, together with '\ 0' if the length is specified <The Source length, copy the truncated source string to the target string according to the specified length, excluding '\ 0'. manually add' \ 0 ', we recommend that you manually add the first two methods. If the length is specified, the target length is incorrect!
# Include <iostream> # include <cstring> // for c style string manipulationusing namespace std; const int maxn = 8; int main () {int I; char dest [] = "Hell99iam! "; // 10 characters, occupying 11 spaces, the last '\ 0' char src [] =" abc \ 0def "; strncpy (dest, src, maxn ); dest [maxn] = '\ 0'; for (I = 0; I <sizeof (dest); I ++) {if (* (dest + I) = '\ 0') cout <"*"; else cout <* (dest + I) <"";} cout <endl <dest <endl; char str [maxn]; strncpy (str, src, 1); for (I = 0; I <sizeof (str ); I ++) {if (* (str + I) = '\ 0') cout <"*"; else cout <* (str + I) <"" ;}// str [1] = '\ 0'; if this sentence is not found, cout is returned. <endl < <Str <endl; return 0;}/* (c/c ++) copy the content (characters, numbers, Chinese characters...) in the src string ....) in the string dest, the number of copies is determined by the size_t value, and a pointer to the dest is returned. If an empty character ('\ 0') [1] is encountered, all characters after the null character are blank (character), and maxn determines how many spaces */

 

Result of the first function in 3:

Result of the second function in 3:

4 void * memset (void * ptr, int ch, size_t num );

The function is used to: fill the first num bytes (note that it is not an element) of block of memory (pointed by the NULL pointer ptr) with value. Finally, we will explain the meaning of this sentence. In C ++, size_t is designed to adapt to multiple platforms. The introduction of size_t enhances the portability of programs on different platforms. Size_t is a type of data customized for the system, generally an integer (int), because the C/C ++ standard only defines a minimum number of digits, rather than a required fixed number of digits.

/* memset example */#include <iostream>#include <cstring>using namespace std;const int maxn = 11;int main (){    int arr[maxn], i;    char str[] = "almost every programmer should know memset!";    memset(str, '-', 6);    cout << str << endl;    memset(arr,10,maxn*sizeof(int));    for(i=0; i<maxn; i++) {       cout << arr[i] << " ";    }    cout << endl;    memset(arr,-1,maxn*sizeof(int));    for(i=0; i<maxn; i++) {       cout << arr[i] << " ";    }    cout << endl;    return 0;}

Problem: in the preceding example, the output of the char array Initialization is as expected. However, the integer array does not meet the expectation. Why. The answer is that memset assigns a value of one byte to the memory block. Because an int element in an int array has four bytes, it is not as expected. I believe it can be changed to 0, and it can be changed to-1:

The result is as follows:






An intelligence question written by Baidu

It is indeed an old question, but unfortunately you copied the wrong question. The answer to your question is: neither of them knows.
But the original question is:
Mr. P and Mr. Q both have sufficient reasoning capabilities.
On this day, they were waiting for a reasoning interview.
They know that there are 16 cards in the drawer of the table:
Peach A, Q, 4
Taotao J, 8, 4, 2, 7, 3
Caohua K, Q, 5, 4, 6
Square A, 5
Professor John picked a card from the 16 cards and told Mr. P about the number of cards and the color of the card to Mr. Q.
Professor John asked Mr. P and Mr. Q: Can you tell from the known points or colors what the card is?
Mr. P: "I don't know this card. "-- P1
Mr. Q: "I know you don't know this card. "-- Q1
Mr. P: "Now I know this card. "-- P2
Mr. Q: "I know. "-- Q2
Excuse me: What is this card?
1. Set Mr. P to P1 and P2 for "row", and Q to Q1 and Q2 for "column. The number of cards is N.
2, by the P1--N is not unique in the row, is a repeated number. Number of blue lines (A, Q)
3. The Q1--N is not in the column that can be directly determined by the row, that is, in the (square, red peach)
4, by the P2--N = A, N = (4, 5, Q)
5. The Q2--N does not repeat in the column. N = 5 (square ).
So we all know "rows" and "columns.




A java interview pen question, very classic

Public A (int radio ){
This. radio = radio;
System. out. println ("A construt ");
}

Essentially:

Public A (int radio ){
Super (); // must be in this position
This. radio = radio;
System. out. println ("A construt ");
}

As you can see, if you write:
Public A (int radio ){
This. radio = radio;
Super ();
System. out. println ("A construt ");
}

If it can be executed, A. draw (), radio = 1
Of course, this assumption is not true because super () must be in the first sentence.

Welcome to the discussion group: 75549598

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.