SE250:lab-2:sshi080

From Marks Wiki
Revision as of 05:19, 3 November 2008 by Mark (Sọ̀rọ̀ | contribs) (34 revision(s))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Intro

This lab is to create a map to show the different memory areas used by each C compiler.

Task 1

Task 1 is to determine the sizes of different pointers.

Windows Machine

printf("Task 1:\n");
printf("Size of int pointer: %d\n", sizeof(intptr));
printf("size of double pointer: %d\n", sizeof(dblptr));
printf("Size of char pointer: %d\n\n", sizeof(charptr));

Result:

4
4
4

It's safe to say that all pointers have the same size as it doesn't vary in length, it just a number showing the address of the variable it's supposed to point to.

Linux PPC64 Machine

Testing on the linux server:

Result:

4
4
4

This surprised me as I thought the size of the pointers on the 64-bit machine would be different? The pointers should be 8 bytes (as 8*8 = 64 bits)

Pointers and other data types

Comparing the sizes between pointers vs other types.

printf("Size of int variable: %d\n", sizeof(aint));
printf("Size of double variable: %d\n", sizeof(bdouble));
printf("Size of long variable: %d\n", sizeof(clong));
printf("Size of float variable: %d\n", sizeof(dfloat));
printf("Size of char variable: %d\n", sizeof(echar));
printf("Size of short variable: %d\n\n", sizeof(fshort));

Results:

Size of int variable: 4
Size of double variable: 8
Size of long variable: 4
Size of float variable: 4
Size of char variable: 1
Size of short variable: 2

int, long and float types have the same size as the pointers.

Task 2

This task is to compare the distance between two varibles, x and y. The two different lines of code represent the bytes and bits apart respectively.

Code:

printf("Task2:\n");
printf("&x = %p &y = %p, difference = %ld\n", &x, &y, (long)(&x - &y));
printf("&x = %p &y = %p, difference = %ld\n\n", &x, &y, ((long)&x - (long)&y));

Windows Machine

Results:

Task2:
&x = 002DFB24 &y = 002DFB18, difference = 3
&x = 002DFB24 &y = 002DFB18, difference = 12

Linux Machine

Results:

Task2:
&x = 0xffa75710 &y = 0xffa7570c, difference = 1
&x = 0xffa75710 &y = 0xffa7570c, difference = 4


Task 3

Task 3 is to determine the gap between x and y with an array of changing size 'arr' in between to check the length difference.

Code:

int x;
char arr[4] = {10};	
int y;

printf("Task3: \n");
printf("Size of arr: %d\n", sizeof(arr));
printf("&arr = %p\n", &arr);
printf("&arr+4 = %p\n", &arr+4);
printf("&arr[4] = %p\n", &arr[4]);
printf("Values: x = %d, y = %d\n", x, y);

Windows Machine

Results:

Task3:
Size of arr: 4
&arr = 0xfffb870c
&arr+4 = 0xfffb871c
&arr[4] = 0xfffb8710

Then we vary the size of the array, from 0 to 10:

Results:

arr[0] =  - N/A -
arr[1] = 6, 24
arr[2] = 6, 24
arr[3] = 6, 24
arr[4] = 6, 24
arr[5] = 7, 28
arr[6] = 7, 28
arr[7] = 7, 28
arr[8] = 7, 28
arr[9] = 8, 32
arr[10] = 8, 32

It seems like every time we increase the size of the array by 4, the gap widens by 1 byte (4 bits)

Linux Machine

Results:

arr[0] =  - N/A -
arr[1] = 1, 4
arr[2] = 1, 4
arr[3] = 1, 4
arr[4] = 1, 4
arr[5] = 1, 4
arr[6] = 1, 4
arr[7] = 1, 4
arr[8] = 1, 4
arr[9] = 1, 4
arr[10] = 1, 4

It looks like on the linux machine the gap between the changing sizes is the same.

Task 4

Task 4 is the same as task 3 except we place the declarations outside any functions, setting them as globals.

The results are very strange, I'm not sure if I have done it correctly.

Windows Machine

Results:

arr[0] =  - N/A -
arr[1] = -1, -4
arr[2] = -1, -4
arr[3] = -1, -4
arr[4] = -1, -4
arr[5] = -1, -4
arr[6] = -1, -4
arr[7] = -1, -4
arr[8] = -1, -4
arr[9] = -1, -4
arr[10] = -1, -4

It seems like the gap doesn't change, its always 1 byte apart from each other. Also the negative sign means the memory is being allocated downwards.

Linux Machine

Results:

arr[0] =  - N/A -
arr[1] = -2, -8
arr[2] = -2, -8
arr[3] = -2, -8
arr[4] = -2, -8
arr[5] = -3, -12
arr[6] = -3, -12
arr[7] = -3, -12
arr[8] = -3, -12
arr[9] = -1, -4
arr[10] = -1, -4

The results here are definitely strange... the gap seem to increase as we go from 1 to 8, but when we hit 9, the gap decreases back to 1 byte. What the hell?

Task 5

Task 5 is a simple analysis task.

Code:

int *p1, *p2;
{ int q; p1 = &q; }
{ int r; p2 = &r; }

p1 and p2 in this case are just pointing to the address of int q and r respectively.

Task 6

Task 7

Task 7 is to determine the address of a struct and determine the difference within each element inside the struct.

Code:

#include <stdio.h>

struct {
	char my_char;
	short my_short;
	int my_int;
	long my_long;
	float my_float;
	double my_double;
} my_struct;

int main(void) {
	
	printf("&my_struct = %p\n", &my_struct );
	printf("offsets:\n"
	"my_char: %ld\n"
	"my_short: %ld\n"
	"my_int: %ld\n"
	"my_long: %ld\n"
	"my_float: %ld\n"
	"my_double: %ld\n",
	(long)&my_struct - (long)&my_struct.my_char,
	(long)&my_struct - (long)&my_struct.my_short,
	(long)&my_struct - (long)&my_struct.my_int,
	(long)&my_struct - (long)&my_struct.my_long,
	(long)&my_struct - (long)&my_struct.my_float,
	(long)&my_struct - (long)&my_struct.my_double);

}

Windows Machine

Results:

&my_struct = 00307560
offsets:
my_char: 0
my_short: -2
my_int: -4
my_long: -8
my_float: -12
my_double: -16

Linux Machine

Results:

&my_struct = 0x10010ac0
offsets:
my_char: 0
my_short: -2
my_int: -4
my_long: -8
my_float: -12
my_double: -16

The results between the windows and linux machines are the same, except for the memory addresses.

Task 8

This task is the same as task 7 except for instead of using a struct, we use a union

Windows Machine

Results:

&my_union = 00247560
offsets:
my_char: 0
my_short: 0
my_int: 0
my_long: 0
my_float: 0
my_double: 0

There are no offsets in the children types inside the union.

Linux Machine

Results:

&my_union = 0x10010ab0
offsets:
my_char: 0
my_short: 0
my_int: 0
my_long: 0
my_float: 0
my_double: 0

The results between the windows and linux machines are the same, except for the memory addresses.

Task 9

This task to check the memory positions of malloc() and see what the results are when malloc is freed.

Code:

int main(void) {
	
	int a;

	char *sp1, *sp2, *sp3;
	sp1 = malloc(10);
	sp2 = malloc(10);
	free(sp1);
	sp3 = malloc(10);
	
	printf("Address of a: %p\n", &a);
	printf("Address of sp1: %p, Address of sp2: %p, Address of sp3: %p\n", &sp1, &sp2, &sp3);
}

Windows Machine

Results:

Address of a: 002DF96C
Address of sp1: 002DF960, Address of sp2: 002DF954, Address of sp3: 002DF948

Linux Machine

Results:

Address of a: 0xff80972c
Address of sp1: 0xff809728, Address of sp2: 0xff809724, Address of sp3: 0xff809720