Lin Rui c++/c high quality programming Appendix answer (-)

Source: Internet
Author: User

Appendix C:C++/C the answers and grading criteria of the questions
First, please fill in bool, float, pointer variable and "0 value" comparison of the IF statement. (10 points)
Please write the IF statement of BOOL flag compared to "0 value". (3 points)
Answer
if (flag)
if (!flag)
The following writing is a bad style, do not score.
if (flag = = TRUE)
if (flag = 1)
if (flag = = FALSE)
if (flag = 0)
Please write the If statement for float x compared to "0 value". (4 points)
Example of a standard answer:
const float Epsinon = 0.00001;
if ((x >=-Epsinon) && (x <= Epsinon)
You cannot use the floating-point variable "= =" or ". = "with number
Compare, should try to convert to ">=" or "<=" this class
Form.
The following is the wrong wording, do not score.
if (x = = 0.0)
if (x!= 0.0)
Please write the IF statement comparing Char *p with "0 value". (3 points)
Answer
if (p = = NULL)
if (P!= NULL)
The following writing is a bad style, do not score.
if (p = = 0)
if (P!= 0)
if (p)
if (!)
Second, the following is under Windows NT 32-bit C + + program, please calculate the value of sizeof (10 points)
void Func (char str[100])
{
Please calculate
sizeof (str) = 4 (2 points)
}
Char str[] = "Hello";
char *p = str;
int n = 10;
Please calculate
sizeof (str) = 6 (2 points)
sizeof (P) = 4 (2 points)
sizeof (N) = 4 (2 points)
void *p = malloc (100);
Please calculate
sizeof (P) = 4 (2 points)
High quality C++/C Programming Guide, v 1.0
2001 Page 101
Three, Jane Answer (25 points)
1, the header file in the Ifndef/define/endif what to use. (5 points)
A: Prevent the header file from being repeatedly referenced.
2, #include <filename.h> and #include "filename.h" what is the difference. (5 points)
A: For #include <filename.h>, the compiler starts searching from the standard library path filename.h
For #include "filename.h", the compiler starts searching from the user's work path filename.h
3. What is the use of const? (Please specify at least two types) (5 points)
A: (1) You can define const constants
(2) const can modify the function's parameters, return values, and even the definition of the function. The east which is modified by the const
It is compulsory to protect, can prevent accidental change, can improve the robustness of the program.
4, in the C + + program to call the compiler compiled by the function, why add extern "C". (5 points)
A: the C + + language supports the function overload, and it does not support function overloading. The name of the function that is compiled by C + + in the Library
Different from the C language. Suppose a function is a prototype: void foo (int x, int y);
The function is compiled by the C compiler in the library after the name is _foo, and C + + compiler will produce like
Names like _foo_int_int.
C + + provides an extern "C" of the specified symbol, "C." To resolve the name matching problem.
5. Please outline the advantages and disadvantages of the following two for loops (5 points)
for (i=0; i<n; i++)
{
if (condition)
DoSomething ();
Else
Dootherthing ();
}
if (condition)
{
for (i=0; i<n; i++)
DoSomething ();
}
Else
{
for (i=0; i<n; i++)
Dootherthing ();
}
Advantages: Simple Procedure
Disadvantages: More than N-1 logical judgments are performed, and
Interrupts the Loop "pipelining" job, making compiling
The loop can not be optimized to deal with, reducing the efficiency
Rate.
Advantages: High efficiency of cycle
Disadvantage: The procedure is not concise

Four, about the memory of the study questions (5 points per question, a total of 20 points)
void GetMemory (char *p)
{
p = (char *) malloc (100);
}
void Test (void)
{
char *str = NULL;
GetMemory (str);
strcpy (str, "Hello World");
printf (str);
}
What would be the result of running the test function, please?
Answer: The program crashes.
Because GetMemory is not able to deliver dynamic memory,
STR in the Test function is always NULL.
strcpy (str, "Hello World"); will cause the program to collapse
Break.
Char *getmemory (void)
{
Char p[] = "Hello World";
return p;
}
void Test (void)
{
char *str = NULL;
str = GetMemory ();
printf (str);
}
What would be the result of running the test function, please?
A: It may be garbled.
Because GetMemory is returning to "stack memory"
Pointer, the address of the pointer is not NULL, but its original
Now the content has been cleared, the new content is unknown.
void GetMemory2 (char **p, int num)
{
*p = (char *) malloc (num);
}
void Test (void)
{
char *str = NULL;
GetMemory (&AMP;STR, 100);
strcpy (str, "Hello");
printf (str);
}
What would be the result of running the test function, please?
For:
(1) Can output hello
(2) Memory leak
void Test (void)
{
Char *str = (char *) malloc (100);
strcpy (str, "Hello");
Free (str);
if (str!= NULL)
{
strcpy (str, "World");
printf (str);
}
}
What would be the result of running the test function, please?
A: Tamper with the contents of the dynamic memory area, the consequences are difficult to advance
Material, very dangerous.
Because of free (str); then STR becomes a wild pointer,
The IF (str!= NULL) statement does not work.

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.