Possible iOS written exam questions (4)--c language

Source: Internet
Author: User
Tags function prototype integer numbers sprintf

Possible iOS written exam questions (4)--c language may be encountered in the iOS written test face questions (4)--c language

C language, the basic foundation of development, iOS many advanced applications have to deal with C language, so, C language in the development of iOS importance, you understand. Some of these problems may not be a C language problem, but belong to some of the computer's original intellectual points, so I will not write another article, directly written here.

What happens when you write the following code?
    • least = MIN (*p++, b);
    • The result is: ((p++) <= (b)? (p++): (*p++)) This expression can have side effects, and pointer p will do three + + self-increment operations.
Declare a constant with preprocessor directive # # to indicate how many seconds in 1 years (ignoring leap year issues) define SECONDS_PER_YEAR (60 -* 365) UL (ul unsigned long Shaped) write a "standard" macro min, this macro input two parameters and return the smaller one. Define MIN (A) (A) <= (B)? (a): (B) write a standard macro max and give the output of the following code
Intarray[5] = {1,2,3,4,5};int *p = &array[0];int max = max (*p++,1);printf"%d%d", Max, *p); Reference answer:1,2#Define MAX (x, y) ((x) > (y)? (X): (Y) When you see a macro, you think about the side effects of the macro definition. For + +, – The use of macros is most likely to cause side effects, so use caution. Analysis: The P pointer points to an arrayThe first address of the array, which is the address of the first element, with a value of1. It is important to note that each place is added with parentheses *p++ equivalent to *p, p++, so Max (*p++, 1) is equivalent to: (*p++) > (1)? (*p++): (1) = (1) > (1)? (*p++): (1) and the result of the first *p++ is that the value pointed to by P becomes 2, but 1 > 1, so the final max value is 1. Later (*p++) will not be executed, so p points to the address corresponding to the value is 2, instead of 3. Extension: If the above *p++ is changed to * (++p How to (*++p) > (1)? (*++p): (1) = (2) > (1)? (*++p): (1) = max = *++p;=> *p = 3,max = 3;                 
What is the difference between a define defined macro and a const-defined constant?
λ    #define定义宏的指令,程序在预处理阶段将用#define所定义的内容只是进行了替换。因此程序运行时,常量表中并没有用#define所定义的宏,系统并不为它分配内存,而且在编译时不会检查数据类型,出错的概率要大一些。λ    const定义的常量,在程序运行时是存放在常量表中,系统会为它分配内存,而且在编译时会进行类型检查。#define定义表达式时要注意“边缘效应”,例如如下定义:#define N 2 + 3 // 我们预想的N值是5,我们这样使用Nint a = N / 2; // 我们预想的a的值是2.5,可实际上a的值是3.5
What does the keyword volatile mean? And give three different examples
    • Instead of using the backup stored in the register, the optimizer must carefully reread the value of the variable each time it uses this variable. Here are a few examples of volatile variables:
    • Hardware registers for parallel devices (e.g., status registers)
    • A non-automatic variable that is accessed in an interrupt service subroutine (non-automatic variables)
    • Variables shared by several tasks in multi-threaded applications
You can use sprintf, strcpy, and memcpy functions to complete a string copy, what is the difference between these functions? which one do you like? Why?
The difference between these functions is that they implement functions and manipulate objects differently. -strcpy: The object of the function operation is a string that completes the copy function from the source string to the destination string. -sprintf: This function is mainly used to implement the conversion function (string or basic data type) to the string. If the source object is a string, and you specify the%s format character, you can also implement the string copy feature. -memcpy: The function, as its name implies, is a memory copy that enables the copying of the contents of one block of memory to another block of memory. The memory block is determined by its first address and its length. SoThe memcpy object is suitable for any data type, as long as it can give the object's starting address and memory length information, and the object is operable. GivenThe characteristics of the memcpy function and the physical meaning of the data type,memcpy functions are usually limited to copies of the same type of data or objects, including, of course, string copies and copies of basic data types. -For a string copy, the above three functions can be implemented, but the efficiency and ease of use of the implementation are different:-strcpy is undoubtedly the most suitable choice: high efficiency and easy to invoke. - snprintf to specify format characters and convert them in a format that is cumbersome and inefficient. - memcpy is efficient, but requires additional copies of the memory length of this parameter, easy to use and inconvenient, and if the length is specified too large (the optimal length is the source string length + 1), but also a performance degradation. in fact, strcpy function is generally called in the internal memcpy function or the assembly is directly implemented, in order to achieve efficient purposes. Therefore,  there should be no significant difference in performance between using memcpy and strcpy copy strings. -For non-string types of data replication,strcpy and snprintf generally powerless, but to memcpy but no impact. However, for basic data types, although copies can be made with memcpy, because there are assignment operators that can easily and efficiently copy between data of the same or compatible type, the memcpy is rarely used in this case . The advantage of memcpy is to make a copy of the structure or array (usually the internal implementation), which is either efficient, convenient, or even both. 
sprintf,strcpy,memcpy what to pay attention to on the use of the place
    • strcpy is a string copy function whose function prototype is strcpy (char DST, const char src);

    • Copy the string starting with SRC to the beginning of DST memory, the end of the glyph is ' "", because the length of the copy is not controlled by ourselves, so this string copy is prone to error.

    • The function with the string copy function has memcpy, which is a memory copy function, its function prototype is memcpy (char DST, const char src, unsigned int len); a section of memory with Len length The length of this function is controllable from the SRC copy to DST. However, there will be memory read-write errors. (e.g. Len is longer than the space or destination space to be copied)

    • sprintf is a format function. Formats a piece of data into a string buffer in a specific format. sprintf the length of the formatted function is not controllable, it is possible that the formatted string will exceed the size of the buffer, causing overflow.

The role of the static keyword
    • Hide. When compiling multiple files, all global variables and functions that do not have a static prefix are globally visible.
    • Preserves the persistence of variable content. Both global and static variables are stored in a static storage area, and the program starts to initialize and initialize only once. Static controls the scope of the variable.
    • The default is initialized to 0. In the static data area, all bytes in memory are 0x00, and global and static variables are initialized to 0 by default.
Static keyword differences:
    • What is the difference between a static global variable and a normal global variable: the static global variable is only initialized once, preventing it from being referenced in other file units;
    • What is the difference between a static local variable and a normal local variable: the static local variable is initialized only once, the next time based on the last result value;
    • What is the difference between a static function and a normal function: The static function has only one copy in memory, and the normal function maintains a copy of each call
Keyword const
    • const int A;int const A; The function is the same: A is a constant integer number
    • const int A;int const A; A is a pointer to a constant integer number (the integer number is not modifiable, but the pointer can)
    • int * Const A;A is a constant pointer to an integer number (the integer number pointed to by the pointer can be modified, but the pointer is not modifiable)
    • the int const * Const A;A is a constant pointer to a constant integer number (the integer number pointed to by the pointer is not modifiable and the pointer is not modifiable)
Stack
    • Management: For the stack, is automatically managed by the compiler, without our manual control, for the heap, the release of work by the programmer control, easy to generate memory leak.
    • Application Size:

      • Stack: Under Windows, the stack is the data structure to the low address extension, which is a contiguous area of memory. This sentence means that the top of the stack of the address and the maximum capacity of the stack is the system pre-defined, in Windows, the size of the stack is 2M (also said 1M, in short, is a compile-time determination of the constant), if the request for more space than the stack's remaining space, will prompt overflow. Therefore, the space available from the stack is small.

      • Heap: A heap is a data structure that extends to a high address, and is a discontinuous area of memory. This is because the system is stored with a linked list of free memory address, is naturally discontinuous, and the chain of the list of traversal direction is from the low address to high address. The size of the heap is limited by the valid virtual memory in the computer system. Thus, the space of the heap is more flexible and relatively large.

    • Fragmentation issues:
      For the heap, frequent new/delete is bound to cause memory space discontinuity, resulting in a large number of fragments, so that program efficiency is reduced. For the stack, there is no problem, because the stack is advanced out of the queue, they are so one by one correspondence, so that there will never be a memory block from the middle of the stack popped
    • Allocation method:
      The heap is dynamically allocated and there are no statically allocated heaps. Stacks are allocated in 2 ways: static allocation and dynamic allocation. Static allocations are done by the compiler, such as the allocation of local variables. The dynamic allocation is assigned by the Alloc function, but the dynamic allocation of the stack is different from the heap, and his dynamic allocation is released by the compiler without our manual implementation.
    • Allocation efficiency:
      The stack is the data structure provided by the machine system, the computer will support the stack at the bottom: allocate the address of the special register storage stack, the stack stack has special instruction execution, which determines the efficiency of the stack is high. The heap is provided by the C + + function library, and its mechanism is very complex.
The difference between an array and a pointer
    • Arrays can be applied to the stack area and data area; pointers can point to any type of memory block
      When sizeof acts on an array, it gets the amount of memory the array occupies, and when it acts on the pointer, it gets 4 bytes.
    • The array name represents the first address of the array, is a constant pointer and cannot be modified to point to. For example, you can not use the + + function on the array name, the value of the normal pointer can be changed, such as can be applied to the pointer
    • Initializing a character array with a string is a copy of the contents of the string into a character array; the character pointer is initialized with a string that assigns the first address of the string to the pointer, which is the pointer pointing to the string
The difference between a reference and a pointer
    • The pointer points to a piece of memory where the content stores the address of the referred memory.

    • A reference is an alias for a block of memory.

    • References do not need to be dereferenced (*) and pointers require

    • References are initialized only at the time of definition, immutable, and pointers are mutable.

    • Reference does not have a const

    • Reference cannot be empty

    • sizeof refers to the size of the pointed variable (object), and the sizeof pointer is the size of the pointer itself.

    • Pointer and reference self-increment (+ +) operation has different meanings: reference + + is the Reference object itself + +, pointer + + is the memory that points to the back of the object

    • The program needs to assign a memory area to the pointer, which is not required.

Use variable A to give the following definition
  • An integer number (an integer)
  • A pointer to the integer number (a pointer to an integer)
  • A pointer to a pointer to a pointer that points to an integer number (a pointer to a pointer to an intege) r
  • An array of 10 integers (an arrays of ten integers)
  • An array of 10 pointers that point to an integer number. (An array of ten pointers to integers)
  • A pointer to the array of 10 integer numbers (a pointer to an array of ten integers)
  • A pointer to the function that has an integer parameter and returns an integer number (a pointer to a function that takes an integer as an argument
    and returns an integer)
  • An array of 10 pointers pointing to a function that has an integer parameter and returns an integer number (an array of ten pointers to functions T
    Hat take a integer argument and return an integer)

  • The answer is:

  • int A; An integer
  • int *a; A Pointer to an integer
  • int **a; A pointer to a pointer to an integer
  • int a[10]; An array of ten integers
  • int *a[10]; An array of ten pointers to integers
  • int (*a) [10]; A pointer to an array of ten integers
  • Int (*a) (int); A pointer to a function A, takes an integer argument and returns an integer
  • Int (*a[10]) (int); An array of ten pointers to functions A, a integer argument and return an integer
Please write out the following code output
int a[5] = {1,2,3,4,5};int *ptr = (int *) (&a +1);printf"%d,%d", * (A +1), * (PTR +1)); Reference answer:2, random values This type of question seems quite common. The test is the understanding of pointers and arrays of C language. Analysis: A represents aThe first address of an array of 5 elements, a[5] The elements are1,2,3,4,5. Next, A + 1 means the first address of the data plus 1, then A[1], That corresponds to a value of 2. However, here is &a + 1, because a represents the entire array, its space size is 5 * sizeof (int), so &a + 1 is A+5. A is a constant pointer to the first address of the current array, the pointer +1 is the move sizeof ( int) bytes. Therefore, PTR is a pointer to the int * type, while PTR points to a + 5, then ptr + 1 is also equivalent to a + 6, so the final * (ptr + 1) is a random value. and * (Ptr–1) is equivalent to A + 4, the corresponding value is 5.  
Brief description of memory partitioning
    • Code area: Storing the binary code of the function
    • Data area: The system runs the request memory and initializes, the system exits when the system is released, storing global variables, static variables, constants
    • Heap area: A dynamic application by a function such as malloc or a new operator, requiring the programmer to manually apply and release
    • Stack: Application within the function module, automatically released by the system at the end of the function, storing local variables, function parameters
Output a floating-point type with the NSLog function, rounding the result and preserving a decimal
float money = 1.011;NSLog(@"%.1f", money);

If you have any questions, please leave a message and I will correct it in time.

Possible iOS written exam questions (4)--c language

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.