"Analysis of several sets of SIZEOF information" complement

zhaozj2021-02-16  68

Example

1:

C

Reference

The description information of the SIZEOF operator is as follows:

THE

SizeOf Operator is a compile

-time

Operator That Returns the size

, In bytes

, Of the argument passed to it

.

Means of

The output information of SIZEOF is determined during compilation, and at the same time, there is no limit to the compiler to pass

SizeOf's expression specifically processed, in fact, the compiler is generally processing only the type information of the expression, and the following example can explain this.

//sizeofdemo1.c

#include

Int func

(

int

* Pi

) {

PRINTF

(

"in func / n"

* Pi

;

Return

0

}

int

main

() {

INT i

=

0

;

PRINTF

(

"SIZEOF (Func (& I)) =% D / N"

,

Sizeof

(Func)

(& I

))))

// Here with Sizeof (int) equivalent printf

(

"Current value of i is:% d / N"

, I

);

PRINTF

(

"SIZEOF (i ) =% D / N"

,

Sizeof

(i

));

/ / Here is also Printf

(

"Current value of i is:% d / N"

, I

);

PRINTF

(

"SIZEOF (1.0) =% D / N"

,

Sizeof

(

1.0

));

Return

0

}

Therefore, we don't try to

Modification of variables in SIZEOF

(At the time of program development in the VC, you also need to pay attention, do not try to modify the variable in the macro in Assert, otherwise, when compiling in the Release mode, you will completely ignore your program code, causing a difficult error that is difficult to discover.

).

Example

2:

Use a bit domain

(Bit)

-field

)Case

(C

Primer said: There is a special class data called a bit domain, which can be declared to store a specific number of bits. In the species in the species, if possible, they are placed in the continuous bits of the same integer and provide spatial compression.

).

//sizeofdemo2.c

#include

Typedef struct tagstruct

{

Unsigned char x

:

4

;

/ / Indicates 4 BIT

Unsigned char y

:

4

} Struct

;

int

main

() {

PRINTF

(

"SIZEOF (STRUCT) =% D / N"

,

Sizeof

(Struct)

));

// (4 4) / 8 = 1

Return

0

}

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

New Post(0)