Programmer's written test FAQ is easy to make mistakes. Keep on going !!!

Source: Internet
Author: User

1. execution sequence of for statements

For (sta1; sta2; sta3)
{
Sta4;
}
1. Execute sta1 in a loop; // It is executed only when it enters.
2. Execute sta2; // execute sta4 only when the condition is true, otherwise it will jump out of.
3. Execute sta4;
4. Execute sta3;

5. Go Back To Step 1 and start execution.

2. For A & B,

If expression A is true, B is executed. If expression A is false, B is not executed and result false is obtained directly.

3. decompilation from the C file to the Assembly:

Decompilation from C to Assembly: gcc-s file. c

4. the printf rule is to press the stack from right to left and calculate from right to left:

Example:

# Include <stdio. h>

Main ()

{

Int B = 3;

Int arr [] = {6, 7, 8, 9, 10 };

Int * ptr = arr;

* (Ptr ++) + = 123;

Printf ("% d, % d \ n", * ptr, * (++ ptr ));

}

Result: 8, 8

5. Difference Between int and unsight int in C ++

  1. IntMain ()

  2. {

  3. IntA=-6;

  4. UnsignedIntB=4;

  5. If (a + B>0)

  6. Cout <"a + B> 0" <endl; // This statement is printed.

  7. Else

  8. Cout <"a + B <0" <endl;

  9. IntZ=A + B;

  10. If (z>0)

  11. Cout <"z> 0" <endl;

  12. Else

  13. Cout <"z <0" <endl; // The statement is printed.

  14. Return 0;

  15. }

  16. The reason is as follows: when int and unsigned in are added, int must be converted to unsigned int, and int is smaller than 0. Therefore, the highest bit is the sign bit, which is 1, therefore, the conversion result is a very large positive number. In the first if statement, two "positive numbers" are added, and the result is naturally greater than 0. When z = a + B, it regards the result of a + B as an int type, and the highest bit of a + B is 1, so z is a negative number, therefore, the second statement is printed.

6. EAX register

EAX-EDX can be called a data register, in addition to direct access, you can also access their high 16-bit and low 16-bit respectively. Their 16-bit low is to remove the E in front of them, that is, the 16-bit low of EAX is AX. In addition, their low 16-bit access can be carried out separately, that is, AX can be further decomposed, that is, AX can also be divided into AH high 8-bit) AL low 8-bit access ).

Therefore, during the calculation process, the number of digits temporarily stored in EAX is 16. When you reverse the number, consider the value 0 before the original number and the value 1 after the reverse.

7. memory usage of pointer variables (in bytes ).

On 32-bit machines, all pointer-type variables occupy 4 bytes of memory
Because 32-bit machines are 4 bytes * 8 binary bit/byte calculated.
If you are on a 64-bit machine, the memory occupied by the pointer is:

8 bytes.

8. the sizeof parameter of the array is not degraded. If it is passed to strlen, It is degraded to a pointer.

An array has a pointer and an array has a special character. The length of the compilation period is known.

The sizeof parameter uses the known characters in the compilation period, and is still an array without degradation.
Passed To strlen will not use the special features of the array, the behavior is exactly the same as the pointer, degrades to a pointer

9. sizeof (string)

About sizeof (string), I was a little surprised to see this expression when I read the interview book today. The book says sizeof (string) = 4. I was wondering at the time, is it possible to allocate 4 bytes of memory to the string? After reading the relevant information, we can conclude that the implementation of string may be different in different databases, but the same point in the same database is that no matter how long your string contains, its sizeof () is fixed, and the space occupied by the string is dynamically allocated from the heap, regardless of sizeof.
Sizeof (string) = 4 may be one of the most typical implementations, but there are also library implementations where sizeof () is 12 and 32 bytes. However, after the VC6.0 test, sizeof (string) = 16. It is still related to the compiler.


10. The answer to this question on page 77 of baodian is as follows:

Is about the array and pointer, the description is as follows:

Main ()

{

Int a [5] = {1, 2, 3, 4, 5 };

Int * ptr = (int *) (& a + 1 );

Printf ("% d, % d", * (a + 1), * (ptr-1 ));

}

Output: 2, 5

Please explain the output results of the above Code.

The answer is as follows:

* (A + 1) is actually a [1], and the output is 2.

The key is that the second point, * (ptr-1) is the number of output?

The explanation is as follows: & a + 1 is not the first address + 1. The system will consider that an entire a array is added, which is offset by the size of the entire array a, that is, the size of five int values ). So int * ptr = (int *) (& a + 1); in fact, ptr is actually & (a [5]), that is, a + 5.

Why?

& A is an array pointer and its type is int (*) [5];

The pointer plus 1 should add a certain value according to the pointer type. Different types of pointers + 1 will increase the size differently. a is an int array pointer with a length of 5, therefore, we need to add 5 * sizeof (int), so ptr is actually a [5], but ptr is different from & a + 1), which is very important, so the ptr-1 will only subtract sizeof (int *), a, & a address is the same, but the meaning is different, a is the first address of the array, that is, the address of a [0], & a is the first address of the object array), and a + 1 is the address of the next element of the array, that is, a [1], & a + 1 is the address of the next object, that is, a [5].

11. new is used to allocate memory space in C ++. For example, the following statement:

Int * p = new int;
This statement first defines a pointer p pointing to the int, and then allocates a storage area on the heap to save the integer "new int ", finally, the address of the storage area is paid to the pointer p. The new storage zone will not be automatically released, so you need to release it with delete in the appropriate place: "delete p ;"
New can also be used to allocate a continuous storage area for the array type:
Int * p = new int [10];
This statement is allocated with a continuous storage area that can save 10 integers. The corresponding release statement has a slight change:
Delete [] p;
If you are familiar with malloc () and free () in c, they can be compared with new and delete, however, new and delete call the object or structure constructor and destructor when processing storage resources. This is the most important difference from malloc and free.

12. sizeof, strlen, char * str1

Char * str1 = (char *) malloc (100 );

Str1 = "sfsdfdfdf ";


Sizeof (str1) = 4; Because str1 is a pointer object, it occupies 4 bytes of space. This is also true for other types of pointers.

Sizeof (* str1) = 1; Because * str1 refers to 'S', * (str1 + 1) After 'F' is unbound from

Strlen (str1) = 9; returns the number of characters in the string, excluding '\ 0'


13. Differences Between reserve and resize of vector

The vector reserve adds the vector capacity, but its size has not changed! Resize changes the capacity of the vector and increases its size!
The reason is as follows:
Reserve is a reserved container space, but does not actually create element objects in the space. Therefore, elements in the container cannot be referenced before a new object is added. When adding a new element, call the push_back ()/insert () function.

Resize is used to change the container size and create an object. Therefore, after calling this function, you can reference objects in the container. Therefore, when a new element is added, use the operator [] operator or the iterator to reference element objects. Now call the push_back () function, which is added after the new space.

14. generally placed behind the function body, such as void fun () const;

If a member function does not modify the data member, it is best to declare it as const, because the const member function cannot modify the data member. If it is modified, the compiler reports an error, this greatly improves the robustness of the program.

15. Example 9 on page 71 of baodian

Will be in s. p [0] = 2; error.

Because int * p = & s. i; so the s. p = p. p also points to s. i, because the two member variables in struct S are saved adjacent to each other. p = p, s. p [0] is s. I variable, and s. p [1] is the same as s. I adjacent s. p pointer, so s. p [1] = 1; that is, for s. p pointer variable assignment, that is, changing s. p pointer pointing, so after the execution of s. p [1] = 1; after, s. p no longer points to the member variable I, but to address 1. This is obviously an Invalid Address and cannot be written, but the last s. p [0] = 2 (equivalent to * s. p = 2) is directed to s. the p Pointer Points to the address value, that is, to write a value to address 1, of course, an error occurs.


Key Points:In the same address, the integer is forcibly converted to the address type and assigned to the pointer.

S. p [1] is an integer, and s. p is a pointer type, but the addresses of the two are the same, in the same place. Therefore, when the value of s. p [1] is 1, 1 is forcibly converted to address type 0x00000001; 615

16. A single integer to character type only requires + '0'

17. array space in the stack and malloc In the heap

This article is from the blog "the vitality of combustion technology" and will not be reposted!

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.