SE250:lab-2:sgha014

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

The first task was to compare the sizes of differnt types of pointers.

I started out with int:

int main (void)
{
	int x = 1;
	int *ip;

	ip=&x;

	printf("%d\n", sizeof(ip));

	return 0;
}

The output was: 4


Next i tried it with type double:

int main (void)
{
	double x = 1.0;
	double *ip;

	ip=&x;

	printf("%f\n", sizeof(ip));

	return 0;
}

The output was: 0.000000

This seemed like a really crazy result so with the help of a tutor i realised that in the print statement it should always be %d because we are always printing out the size so we want in to be an integer.

So i changed that part to

printf("%f\n", sizeof(ip)); and the output was: 4 (again???)

Next i tried it using char type and the result was.....4 again!? At this point i decided to ask the tutor what was going on....

SO while i was listening to the explaination i realised that he had talked about this in the lecture and i had completely forgotten about it. So the reason why its always 4 is beacause the adress is stored in 4bytes so beacause the pointer points to an adress, the size we get is always 4.

Moving on to the 2nd step....

int main (void)
{
	int x;
	int y;

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

	return 0;
}

the output was: &x=0021FBD0, &y=0021FBC4, diff=3 with (long)&x-9long)&y the differnce came out as 12

Step 3....

int main (void)
{
	int x;
	char arr[4];
	int y;

	printf("%d\n", sizeof(arr));
	printf("arr=%d, &arr=%d\n", arr, &arr);
	printf("arr+4=%d, &arr[4]=%d\n", arr+4, &arr[4]);
	
	return 0;
}

the output was: 4

arr=2620704, &arr=2620704

arr+4=2620708, &arr[4]=2620708

varying the size of the array gave these results: for size zero the compiler gave and error.. for 1-10 the output was: 6 6 6 6 7 7 7 7 8 8

on the linux server the output was (for size 0 to 10): 1 1 1 1 2 1 1 1 1 1 1

hmmmmm........??????

STEP 4 printing out the differnce between the adresses of x and y when they are global variables... output is: &x=00247168, &y=00247164, diff=1

and for the task with the array...

int x;
char arr[4];
int y;

int main (void)
{
	printf("%d\n", sizeof(arr));
	printf("arr=%d, &arr=%d\n", arr, &arr);
	printf("arr+4=%d, &arr[4]=%d\n", arr+4, &arr[4]);

	return 0;
}

The output is: 4

arr=11956600, &arr=11956600

arr+4=11956604, &arr[4]=11956604

varying the size of the array and printing out the differnce between &x and &y always gave an output of 1 each time


STEP 5

the values i got for p1 and p2 is 2554772 and 2554760


STEP 7 results: my_struct=-858993460

offsets:

my_char: 0

my_short: -2

my_int: -4

my_long:-8

my_float: -12

my_double: -16


STEP 8:

union???? whats a union??



There was waaaay to much to do in this lab.....the tasks themselves werent hard....but there was so much to do...especially since we had 2 do it both on windows and linux.... and since i spent about 30mins of my lab trying 2 actually get into the linux server....so i only tested one of the tasks on linux.... i did most of this lab after the lab had actually ended....there was simply too much work to finih in the 2hrs we get for our lab...