文档库 最新最全的文档下载
当前位置:文档库 › 系统级编程作业_lab1

系统级编程作业_lab1

系统级编程作业_lab1
系统级编程作业_lab1

C++ fundamentals and Type conversion

Student ID _____1043111051________

Student Name 王金科____ ___

Start Time 2012年09月4日星期二 16:20

Finish Time 2012年09月9日星期日 23:55

1. Objectives:

To write simple C++ programs covering:

?Understand how a character is stored in memory;

?Understand how an integer is stored in memory;

?Understand array and pointer; and

?Perform conversion from eight bits (character) to eight bytes to be displayed on screen.

2. Type and Format in memory – character, integer, short and float

2.1

This exercise is to determine the memory size of different type.

lab1_1.cpp

//Determine the memory size of declaration and variable type

#include

#include

#include

#include

#include

#include

void main()

{

char c;

char s[128];

short i;

short n[64];

printf("%3d %3d\n", sizeof(c), sizeof(char));

printf("%3d %3d\n", sizeof(s), sizeof(char[128]));

printf("%3d %3d\n", sizeof(i), sizeof(short));

printf("%3d %3d\n", sizeof(n), sizeof(short[64]));

}

The display is:

Now execute the above program and fill in the following:

Type Size in byte Size in bit

Short 2 16

Char 1 8

Short[32] 64 512

Char[64] 64 512

2.2

Use the approach of the above, determine the memory size of integer, float, integer[128], float[16] (lab1_2.cpp)

Write a program to verify your answer and fill in the following: [hint, use sizeof(…) ]

Type Size in byte Size in bit

Int 4 32

Float 4 32

Int[128] 512 4096

Float[16] 64 512

Write down your codes below:

#include

#include

#include

#include

#include

#include

void main(){

int c; int s[128]; float I; float n[16];

printf(“%3d%3d\n”,sizeof(c),sizeof(int));

printf(“%3d%3d\n”,sizeof(s),sizeof(int[128]));

printf(“%3d%3d\n”,sizeof(i),sizeof(float));

printf(“%3d%3d\n”,sizeof(n),sizeof(float[16]));

}

Write down your expression amongst the difference in size between character, integer, float, short, char[8], int[8], short[8], float[8]

Character = 1 byte

Integer = 4bytes

Float = 4bytes

Short = 2bytes

Char[8] = 8bytes

Int [8] = 32bytes

Short[8] = 16bytes

Float[8] = 32bytes

2.3

This exercise is to display the decimal and octal values so that you know how it is stored in memory.

lab1_3.cpp

//Determine the memory size of declaration and variable type

#include

#include

#include

#include

#include

#include

void main()

{

for (char i = 30; i <41; ++i)

printf("i: dec=%d oct=%o \n", i, i);

}

the output is:

Now modify the above programme to display decimal, octal, hex and unsigned as well. (Hints: hex, %x, unsigned %u)

Write down your codes below:

#include

#include

#include

#include

#include

#include

void main(){

for(char i = 30; i < 41;++i)

printf("i: dec=%d oct=%o hex=%x unsigned=%u \n", i, i,i,i);}

Write down the output of the first 4 lines so that you understand the difference among them.

i:dec=30 oct=36 hex=1e unsigned=30

i: dec=31 oct=37 hex=1f unsigned=31

i: dec=32 oct=40 hex=20 unsigned=32

i: dec=33 oct=41 hex=21 unsigned=33

2.4

This exercise is to dump the content of memory of different type so that you know how it is stored in memory.

lab1_4.cpp

//Determine the memory size of declaration and variable type

#include

#include

#include

#include

#include

#include

void main()

{

char a = '0'; // in hex 0x30, not 0

int i = 0x00000013; //in decimal is (1 x 16 + 3) = 19

short j = 18; //occupies two bytes,

float f = 1.25; //occupies 4 bytes

}

In order to display, you have to set a break point by pressing “F9”beside the line, the program will display a red circle as follows:

Now, type the address of each variable as shown in the left-bottom frame to locate whether they are. Here, we dump the address of variable …a?. Since, visual C++ reserves 4 bytes but char c uses one byte, you can see that the rest three bytes are set to CC CC CC (means 10101010 10101010 10101010 in binary, the default setting).

1memory location of a is 0x0065fdf4, you have to type &a

2then in the Address, key in 0x0065fdf4,

3it displays 30 CC CC CC

0x0065FDF4, the hexadecimal is 0x30 (ASCII)

Now, type and execute the above program and fill in the following, note that your addresses might be different from what I have below.

Variable Memory address Hexadecimal value

a

0x0012ff7c 0x30 i

0x0012ff78 0x13 j

0x0012ff74 0x12 f

0x0012ff70 0xCC

3. Array and Pointer

3.1

The following is a very simple array definition. It defines an array called char a[4] which occupies 4 bytes, the value is 0x30 0x31 0x32 0x00. Note that 0x30 (ASCII) is 0 in decimal. The last character must be terminated by 0x00 or is called null. (ref. lab1_5.cpp)

lab1_5.cpp

#include #include #include

void main() { char a[4] = "012"; }

Based on the above, note that the address of array a is 0x0065fdf4. Now fill in the following:

Address Type Value

0x0012ff7c a[0]

0x30 0x0012ff7d a[1]

0x31 0x0012ff7e a[2]

0x32 0x0012ff7f a[3]

0x00

0x30 or 0

Null, 0x00

3.2

The following program will display the content of an array a[11].

#include

#include

#include

#include

void main()

{

char a[11] = "0123456789";

for (int i = 0; i <10; ++i)

printf("The value of %d is %c in hex %x \n", i, a[i], a[i]);

}

Now modify the above program to display the address of each location in hexadecimal and the value a[12], a[13], explain why you can display a[12] and [13] as you only defined them up to a[10] as follows. (Hint: The address of a[0] is &a[0])

(note that your value might be different.)

Write down the program

#include

#include

#include

#include

#include

#include

void main(){

char a [11] = “01234567879”;

for(int i= 0; i<14;++i)

printf( "The value of %d is %x in hex %x \n", i, &a[i], a[i]);

}

Write down your explanation (hint: dump the memory location and look at the location of a[11], a[12] and a[13] etc. you then realise why?)

The value of 11 is 12ff7f in hex ffffffcc

The value of 12 is 12ff80 in hex ffffffc0

The value of 13 is 12ff81 in hex ffffffff

a[11],a[12],a[13] beyond boundary

3.3

#include

#include

#include

#include

#include

#include

void main()

{

char a[11] = "0123456789";

char *ptr;

ptr = &a[0]; // it can be &a with same effect

for (int i = 0; i <11; ++i, ++ptr)

printf("The value of %d is %x in hex %x \n", i, ptr, *ptr);

}

Now explain the meaning of the following of the above program.

%x : 以十六进制的形式输出整数

\n: 相当于回车键,

i: 整形变量

ptr: 指针变量,表示地址

*ptr: 指针指向地址的值

3.4

The following is about the integer array.

#include

#include

#include

#include

#include

#include

void main()

{

int a[7] = {12, 21, 31, 41, 51, 61};

for (int i = 0; i <6; ++i)

printf("The value of %d is %x in hex %d \n", i, &a[i], a[i]);

}

Now explain increment of address is 4 instead of 1.

Because int take up four bytes,so increment of address is 4 instead of 1.

3.5

Modify the above program with the same output with pointer instead of array a[]. Write down the program

#incude

#include

#include

#include

void main()

{

int a[7] = {12, 21, 31, 41, 51, 61};

int *ptr;

ptr = &a[0];

for(int i = 0; i < 6; ++i; ++ptr)

printf(“The value of %d is %x in hex %d\n”, i, ptr, *ptr);

}

Further discussion

You don?t need answer the following question, just think about it. void main()

{

char a[4];

char b[3];

char c;

}

why increment of address is 4 for array b and character c.

相关文档