Huawei C/C ++ pen questions (2)

Source: Internet
Author: User

1.In a 32-bit system, calculate the value of sizeof for the c ++ program.

# Include <stdio. h>
# Include <malloc. h>

Void Foo (char STR [1, 100])
{
Printf ("sizeof (STR) = % d \ n", sizeof (STR ));
}

Main ()
{
Char STR [] = "www.ibegroup.com ";
Char * P1 = STR;
Int n = 10;

Void * P2 = malloc (100 );

Printf ("sizeof (STR) = % d \ n", sizeof (STR ));
Printf ("sizeof (P1) = % d \ n", sizeof (P1 ));
Printf ("sizeof (n) = % d \ n", sizeof (n ));
Printf ("sizeof (P2) = % d \ n", sizeof (P2 ));

Foo (STR );
}

 

Answer: (1) 17 (2) 4 (3) 4 (4) 4 (5) 4

 

2.Answer the following questions:

(1) What is the use of ifndef/define/endif in the header file? Preprocessing
A: prevent header files from being repeatedly referenced.

(2) What are the differences between # include <FILENAME. h> and # include "filename. H?
A:
For # include <FILENAME. h>, the compiler searches for filename. h from the standard library path.
For # include "filename. H", the compiler searches for filename. h from the user's working path.

(3) Why should I add the extern "C" declaration to call the function compiled by the C compiler in the C ++ program?
A: The names of functions and variables in the symbol library after being compiled by C ++ are different from those in the C language. The variables and functions modified by extern "C" are compiled and connected in C language. C ++ programs cannot directly call C functions because their compiled names are different. C ++ provides a C connection to exchange the specified symbol extern "C" to solve this problem.

 

3.Answer the following questions:

(1) What are the results of running the test function?

Void getmemory (char ** P, int num)
{
* P = (char *) malloc (Num );
}
Void test (void)
{
Char * STR = NULL;
Getmemory (& STR, 100 );
Strcpy (STR, "hello ");
Printf (STR );
}

A: The output is "hello"

(2) what are the results of running the test function?

Void test (void)
{
Char * STR = (char *) malloc (100 );
Strcpy (STR, "hello ");
Free (STR );
If (STR! = NULL)
{
Strcpy (STR, "World ");
Printf (STR );
}
}

 

A: output "world"

(3) What are the results of running the test function?

Char * getmemory (void)
{
Char P [] = "Hello World ";
Return P;
}
Void test (void)
{
Char * STR = NULL;
STR = getmemory ();
Printf (STR );
}

 

A: The pointer is invalid and the output is uncertain.

 

4.Compile strcat Functions

It is known that the prototype of the strcat function is char * strcat (char * strdest, const char * strsrc), where strdest is the destination string and strsrc is the source string.

(1) do not call the string library functions of C ++/C. Compile the strcat function.

A: VC source code:

Char * _ cdecl strcat (char * DST, const char * SRC)
{
Char * CP = DST;
While (* CP)
CP ++;/* Find end of DST */
While (* CP ++ = * SRC ++);/* Copy SRC to end of DST */
Return (DST);/* return DST */
}

 

(2) Why should strcat connect the content of strsrc to strdest?
A: It is convenient to assign values to other variables.

 

5.When should the program use the thread and when the single thread is highly efficient?

(1) time-consuming operations use threads to improve application response
(2) threads are used for parallel operations, such as the concurrent threads on the server side in the C/S architecture to respond to user requests.
(3) Use threads to improve CPU utilization in multiple CPU Systems
(4) Improve the program structure. A long and complex process can be divided into multiple threads to become several independent or semi-independent processes.
Such a program is easy to understand and modify.
In other cases, a single thread is used.

 

6.In C ++, what data is distributed in the stack or heap. Is the new data allocated in the near or remote heap?
A: Stack: stores local variables, function call parameters, function return values, and function return addresses. Managed by the System Heap: dynamically applied when the program is running, and the memory applied for by new and malloc is on the heap.

 

7.Memory alignment and sizof () Output
Answer: The reason for automatic compiler alignment: To improve program performance, data structures (especially stacks) should be alignment on natural boundaries as much as possible. The reason is that the processor needs to perform two memory accesses to access non-alignment memory; however, alignment memory access only needs one access.

 

8.Int I = 10, j = 10, K = 3; K * = I + J; what is the final value of K?
Answer: 60. The priority of this question is actually written as K * = (I + J). The assignment operator has the lowest priority.

 

9.How can I dynamically connect to a database?
A: There are two methods to call a function in a DLL:
1. Load-Time Dynamic Linking. The module clearly calls an export function to make it look like a local function. This requires linking the DLL import and export of those functions, and providing the information required for loading the DLL and positioning the DLL functions to the system.
2. Run-Time Dynamic Linking. You can use the loadlibrary or LoadLibraryEx function to load the DLL during running. After the DLL is loaded, the module can call getprocaddress to obtain the exit address of the DLL function, and then call the DLL function through the returned function pointer. This avoids importing and receiving files.

 

10.What are the differences between function templates and class templates?
A: The instantiation of a function template is automatically completed by the compiler when processing the function call. The instantiation of a class template must be explicitly specified by the programmer in the program.

11.Is windows a kernel-level thread.
A: see the next question.

12.Does Linux have kernel-level threads.
A: A thread is usually defined as a different execution route of code in a process. There are two types of threads: user-level threads and kernel-level threads ". A user thread is a thread implemented in a user program without kernel support. It does not depend on the core of the operating system, the application process uses the thread library to provide functions for creating, synchronizing, scheduling, and managing threads to control user threads. This kind of thread can be implemented even in operating systems like DOS, but the thread scheduling needs to be completed by the user program, which is similar to Windows 3.x collaborative multitasking. In addition, the kernel needs to participate in thread scheduling. It depends on the operating system core and is created and abolished by the internal requirements of the kernel. These two models have their own advantages and disadvantages. The user thread does not require additional kernel expenses, and the Implementation Mode of the user State thread can be customized or modified to meet the requirements of special applications, however, when a thread is in the waiting state due to I/O, the whole process will be switched to the waiting state by the scheduler, and other threads will not be able to run; there are no restrictions on Kernel threads, which are conducive to the advantage of multi-processor concurrency, but occupy more system expenses.
Windows NT and OS/2 support kernel threads. Linux supports kernel-level Multithreading

 

13.How does a thread prevent large peaks.
A: It indicates how to prevent a large number of threads from being generated at the same time by using the thread pool. The thread pool can improve the scheduling efficiency and limit resource usage at the same time, when the maximum number of threads in the thread pool is reached, other threads will wait in queue.

 

14.Write and judge whether the four expressions of ABCD are correct. If yes, write the value of A in the expression.
Int A = 4;
(A) A + = (a ++ );
(B) A + = (++ );
(C) (a ++) + =;
(D) (++ A) ++ = (a ++ );
A =?
A: C error. The left side is not a valid variable and cannot be assigned a value. You can change it to (++ A) + =;
After the change, the answers are 9, 10, 10, and 11 in sequence.

 

15.Is cstring a type security class in MFC?
A: No. You can use the cstring member function Format to convert other data types to cstring.

 

16.Why is a template class used in C ++.
A:
(1) can be used to create data structures with dynamic growth and reduction
(2) It is type-independent and therefore highly reusable.
(3) It checks the data type during compilation instead of runtime, ensuring the type security.
(4) It is platform-independent and portable.
(5) can be used for basic data types

 

17.What does csinglelock do.
A: Synchronize multiple threads to simultaneously access a data class.

 

18.Can a local variable be renamed with a global variable?
A: Yes. Global blocking will be performed in some cases. To use global variables, you must use "::"
A local variable can have the same name as a global variable. A local variable with the same name is used when this variable is referenced in a function, instead of a global variable. For some compilers, multiple local variables with the same name can be defined in the same function. For example, a local variable with the same name can be defined in both loops, the scope of the local variable is in that loop.

 

19.How to reference a defined global variable?
Answer: extern
You can use the header file reference method or the extern keyword. If you use the header file reference method to reference a global variant declared in the header file, assume that you have written the variant incorrectly, an error will be reported during compilation. If you reference it using the extern method, if you make the same mistake, no error will be reported during compilation, but an error will be reported during connection.

 

20.Can global variables be defined in header files that can be contained by multiple. c files? Why?
A: Yes. You can declare global variables with the same name in static form in different C files.
You can declare global variables with the same name in different C files, provided that only one of them can assign an initial value to this variable in the C file, and the connection will not go wrong at this time.

 

21.What is the problem with the for (; 1;) statement? What does it mean?
A: an infinite loop is the same as while (1.

 

22.Do ...... While and while ...... What is the difference between do and do?
A: The previous cycle is used again, and the latter is used again.

 

23.Write the output content of the following code

# Include <stdio. h>
Main ()
{
Int A, B, C, D;
A = 10;
B = A ++;
C = ++;
D = 10 * A ++;
Printf ("B, c, d: % d, % d, % d", B, c, d );
Return 0;
}

 

Answer: 10, 12, 120

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.