首页 > > 详细

C辅导CS240 Introduction to Points解析R编程、R语言讲解留学生

 

Step 0. Introduction

In this lab you will implement memdump to display memory content, introduction to pointers, and implement string functions with pointer.

Type the following commands:

cd
cd cs240
tar -xvf  /homes/cs240/lab5-ptr-memdump/lab5-src.tar

Step 1. Checking the Endianess

Integers can be represented in memory in two ways: Little-Endian or Big-Endian. In Little-Endian, the least significant byte of the integer is stored the lowest address in memory. In Big-Endian, the least significant byte is stored in the highest address in memory.

cd cs240/lab5-src

Using your favorite editor create a new file called endian.c

Then copy the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include lt;stdio.hgt;
int isLittleEndian() #123;
int a = 0x05;
char * p = (char *) amp;a;
if (*p==0x05) #123;
return 1;
#125;
return 0;
#125;

int main()
#123;

if (isLittleEndian()) #123;
printf("Machine is Little Endian\n");
#125;
else #123;
printf("Machine is Big Endian\n");
#125;
#125;

This function “isLittleEndian()” defines a variable “a” and assigns the number 5 to it. Then it defines a pointer “p” of type (char *) and assigns the address of “a” to “p”. The operand “amp;” obtains the address of the variable you place on the right side. A pointer is just an address in memory. In this way, p points to the first byte of the variable “a” and “*p” gives the contents of the first byte of “a”. If the first byte of “a”, dereferenced as “*p”, happens to contain the value 5, then it means that the byte in the lowest address of “a” is the least significant or Little Endian. Otherwise, it means that the byte in the lowest address is the most significant or Big Endian.

Then save the program. Then compile it and run it:

gcc -o endian endian.c
./endian

Step 2. Memory Sections

The memory of the program is divided in the following memory sections:

Memory Section Name Description Access
text (or code segment) This is the area of memory that contains the machine instructions that corresponds to the compiled program. This area is shared by multiple instances of a running program. The text segment also contains constants such as string literals and variables defined using the const keyword. Read, Execute
data This area in the memory image of a running program contains storage for initialized global variables, and static variables that are explicitly initialized to a non-zero value. This area is separate for each running instance of a program. Read, Write
bss This is the memory area that contains storage for uninitialized global variables and static variables that are not explicitly initialized or initialized to zero. It is also separate for each running instance of a program. Read, Write
stack This region of the memory image of a running program contains storage for the automatic (non-static local) variables of the program. It also stores context-specific information before a function call, e.g. the value of the Instruction Pointer (Program Counter) register before a function call is made. On most architecture the stack grows from higher memory to lower memory addresses. A running instance of a program can have multiple stacks (as in a multithreaded program) Read, Write
heap This memory region is reserved for dynamically allocating memory for variables, at run time. Dynamic memory allocation is done by using the malloc or calloc functions and new operator. Read, Write
shared libraries This region contains the executable image of shared libraries being used by the program.   Read, Execute

Open a file lab5-src/sections.c and copy the following program:

cd cs240/lab5-src
vim sections.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include lt;stdio.hgt;
#include lt;stdlib.hgt;

int a=5;

int buffer[1000000];

int foo() #123;
int d;
static int e = 5;

printf("amp;d=%p amp;le=%p\n", amp;d, amp;e);
#125;

int main()
#123;

int b;
static int c;
int * p = (int *) malloc(sizeof(int));
char * str = "Hello World\n";

printf("amp;a=%p\n", amp;a);
printf("amp;b=%p amp;c=%p\n", amp;b, amp;c);
printf("amp;p=%p p=%p\n", amp;p, p);
printf("amp;str=%p str=%p\n", amp;str, str);
foo();

printf("main=%p amp;foo=%p\n", main, amp;foo);
#125;

 

Then save the program, compile it and run it.

gcc -o sections sections.c
./sections

Create a table like the following one, but with all the variables and sections listed. Make sure that the memory addresses are in ascending order. Print the table and turn it in in next lab.

Address     Variable/Function   Section
----------- -----------------   -------
0x4005bc    foo                 Text
0x4005e1    main                Text

You will turnin lab5-src/memory.txt together with the other files.

Step 3. Memory Dump

Open the file lab5-src/mymemdump.c by

cd cs240/lab5-src
vim mymemdump.c

Read and complete the function mymemdump(char *p, int len) that dumps in hexadecimal byte by byte the memory starting at “p” len bytes. An example output is given at the end of the program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include lt;stdio.hgt;
#include lt;string.hgt;
#include lt;stdlib.hgt;

void mymemdump(FILE * fd, char * p , int len) #123;
// Add your code here.
// You may see p as an array.
// p[0] will return the element 0
// p[1] will return the element 1 and so on

fprintf(fd, "0x%016lX: ", (unsigned long) p);
int c = p[0]amp;0xFF;

// Print first byte as hexadecimal
fprintf(fd, "%02X ", c);

// Print first byte as character
fprintf(fd, "%c", (cgt;=32amp;amp;clt;127)?c:'.');
fprintf(fd,"\n");
#125;

Type “make” in lab5-src. The file mymemdump.c is linked together with mem.c to form the executable mem. Here is an example of the output. For every line, print the address, each byte in hexadecimal, and then, in the right column, the ascii value. The ascii value will be printed only if the ascii value is visible 32lt;=clt;=127, otherwise, it will print a ‘.’.

cs240@data ~/lab5/lab5-src $ ./mem
amp;x=0x7FFF89A62890
amp;y=0x7FFF89A628A8
0x00007FFF89A62890: 41 33 40 50 09 00 00 00 30 06 9C 50 D7 7F 00 00  A3@P....0.P..
0x00007FFF89A628A0: 94 28 A6 89 FF 7F 00 00 00 00 00 00 00 00 28 40  (........(@
0x00007FFF89A628B0: 48 65 6C 6C 6F 20 77 6F 72 6C 64 0A 00 00 00 00  Hello world.....
0x00007FFF89A628C0: FF B2 F0 00 00 00 00 00 00 00 00 00 00 00 00 00  .............
head=0x1e83010
0x0000000001E83010: 30 30 E8 01 00 00 00 00 50 30 E8 01 00 00 00 00  00.....P0.....
0x0000000001E83020: 00 00 00 00 00 00 00 00 21 00 00 00 00 00 00 00  ........!.......
0x0000000001E83030: 57 65 6C 63 6F 6D 65 20 00 00 00 00 00 00 00 00  Welcome ........
0x0000000001E83040: 00 00 00 00 00 00 00 00 21 00 00 00 00 00 00 00  ........!.......
0x0000000001E83050: 70 30 E8 01 00 00 00 00 90 30 E8 01 00 00 00 00  p0.....0.....
0x0000000001E83060: 00 00 00 00 00 00 00 00 21 00 00 00 00 00 00 00  ........!.......
0x0000000001E83070: 74 6F 20 00 00 00 00 00 00 00 00 00 00 00 00 00  to .............
0x0000000001E83080: 00 00 00 00 00 00 00 00 21 00 00 00 00 00 00 00  ........!.......
cs240@data ~/lab5/lab5-src $

Run your version of memdump that prints the output above. The output may be different than the output above because the addresses will be different. However, the content will be the same. Copy the output into a MSWORD, RTF, or HTML file lab5-src/mem.doc and then with different colors indicate where the following items are located in the output and their value. Also add the following table with the right values. Color each entry in the table with the corresponding color.

Variable            Address of Variable     Value of Variable
------------------- ---------------------   ----------------- 
str
a
b
y
x.a
x.i
x.b
x.p
head
head-gt;str
head-gt;next
head-gt;next-gt;str
head-gt;next-gt;next
head-gt;next-gt;next-gt;str
head-gt;next-gt;next-gt;next

Note: Some of the variables are pointers. The Value of pointer is the address it stores.

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!