Memory hood (on)

zhaozj2021-02-12  153

Memory hood (on)

Foreword: I am a student who is going to leave the school immediately. This is the first article I wrote more than one or two months. Hustli in the article is my brother, now is the moderator of Nanjing University Little Lily CPlusplus version. I am enthusiastic, MUD is a netizen of small lily, thank you for letting me know that it is not only reading, but it is also very happy. This article is mainly to answer the first three issues and the last issue proposed by HuSTLI and the fourth issue proposed by MUD. I will happen to 9CBS for nearly two months, I want to apply for a column, I don't know if I can wish. I will correct some technical details on the original basis, use improper words, I hope everyone will correct. I don't know why, it may be that the article is too long, only two parts are published.

There is a saying that the monster is great, and it is "The Devil is in the detail" (the devil is hiding in the details.) In the few languages ​​I know, C is just the forest that is frequent. If you are a newcomer for a few days of just a few days, unfortunately meet the devil, and there is no road with a deep magician, it is really bad, a little devil may crush you just build A little confidence. Of course, each coin has another side of it. It may be his fun with a certain level of magic masters.

HuSTLI seems to be such a person. Below I try to do a clock, try to solve several small devils thrown by HuSTLI and MUD. [Note 1]

Note 1: I don't like the devil, this time is just for pure fun, because of me, often the road is one foot, the magic is one feet, the skill is too shallow, afraid of generous.

1. The comma (,) and plus sign ( ) are operators, why is a comma, can not be determined during compilation? Can the plus can? Int a [2,3]; error! INT A [2 3]; correct!

Answer: The plus " " is an operator, but the comma "," has two usage. The first usage is a separator, such as our most common I, J, K; but there is a usage, more uncommon, as a comma operator, also called sequential operators (SEQUENCE OPERATOR), the most common use is in the For statement, for example

For (int i = 0, j = i; i

In fact, as the name suggests, since the order of order operators, the result is from left to the right, and the result of the final expression is the result of the last evaluation. E.g:

INT I, J, K;

i = 2, j = i 2, k = 3 * J, i J K;

The second statement begins with one of the left to right, i = 2, j = i 2 = 4, k = 3 * j = 12, i j k = 18, the entire expression result is 18, type Is I J K Type INT.

However, in all operator priorities, the priority of the comma operator is the lowest, and the standard also specifies the expression value of the expression in the comma operator to be dynamically determined. Since it is dynamic to determine, it is of course not compilant. Determine during period. Therefore, int A [2,3]; the comma operator expression needs to be determined at runtime, but the number of arrays must be determined, contradictory, and compiled during the compilation period. There is a regulation in the standard, [const_expression], the number of array should be a const_expression, but in this const_expression, Comma Operator is not available. [Note 2] Note 2: Please refer to ANSI C Standard 5.19

Now let's make a hypothesis that if the comma operator expression can be determined at compile, that is, Int a [2, 3]; it is int a [3]; then a programmer with the fortran background first look In this expression, he will definitely think that it is int a [2] [3]; after the trouble is definitely like shaped. Therefore, the standard prohibits this use is very sensible. However, some compilers have made some extensions to C language, such as the famous GNU family's GCC compiler, so this statement can be passed under GCC, but remember that GCC is a C language compiler, g is C language compiler. The following example:

INT A [2, 3, 4];

VC7.1 cannot pass; DEVC 4.9.7 can be passed, indicating int A [4];

2. The following array is initialized, can not use Static to modify an array, how to change it?

Class A

{

PUBLIC:

A (): a ({1, 2}) {} // This is not!

Private:

Const Int A [2];

}

Answer: A non-static array cannot be initialized in a class using const modified. However, in this issue, array A is a constant array, so it is impossible to initialize in the constructor. The essential reason for array A cannot be initialized is that A is a collection of continuous objects that cannot represent an object. E.g:

INT A [2], B [2] = {1, 2};

A = b; // is not legal, A cannot be assigned!

A is just a group name, it has two meaning, ZizeOf (a), a represents the entire array, the SIZEOF (A) result is the number of bytes of the entire array; 2.int j = a [1] Among the other words of * (a 1), a value of A is the address of the first element of the array. There are two solutions to the examples.

(1). Transfer a [2] to the class hierarchy, will be changed to Static const Int a [2], you can think about it seriously, since A [2] is const, then each An object really requires a separate A [2]? Most of the answers should be NO. Now the class is defined as follows:

Class A

{

PUBLIC:

A () {}

Private:

Static const Int a [2];

}

Const Int A :: A [2] = {1, 2}; // In implementing the file.

(2) Change the array to a pointer, that is, Const Int A [2] is changed to const Int * const a; the class is now defined as follows:

Const int CA [2] = {1, 2}; //

Class A

{

PUBLIC:

A (): A (CA) {}

Private:

Const Int * Const A;

}

I prefer the first solution!

(To be contract!)

Wu Tong wrote in 2003.4.26

Recently modified 2003.6.15

转载请注明原文地址:https://www.9cbs.com/read-6146.html

New Post(0)