SE250:lab-2:jhor053

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



Template:Please leave this line alone (sandbox heading)

Lab 2

Task 1

My code:

int *ip;
char *ip2;
short *ip3;
long *ip4;
double *ip5;
float *ip6;
	 
printf("int *ip %d\n", sizeof(ip));
printf("short *ip %d\n", sizeof(ip3));
printf("long *ip %d\n", sizeof(ip4));
printf("double *ip %d\n", sizeof(ip5));
printf("float *ip %d\n", sizeof(ip6));

The pointers are all the same size at 4 bytes (due to only have memory references and not storing actual data I guess). This was also true to Linux as well.

Task 2

My Code:

int x = 0;
int x;
int y; 

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

It had 3 for the former and 12 for the last and same multiple of 4 on linux as well.

Task 3

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

printf("Size of arr: %d\n", sizeof(arr));
printf("Addr of arr: %p\n", &arr);
printf("value of arr+4 : %d\n", arr+4);
printf("value of &arr[4]: %d\n", &arr[4]);

The size of the array was reported back as 4 bytes.

Address of arr: 0030F80C
value of arr+4 : 3209232
value of &arr[4]: 3209232
<code><pre>

Not suprisingly arr + 4 bytes = &arr[4] as pointer arithmetic would mean that arr + 4 is moving along 4 bytes to equal that of the 4 element in the array.

The table below is the difference between &x and &y:

0 1 2 3 4 5 6 7 8 9 10
3 6 6 6 6 7 7 7 7 8 8
&x = 001AFC28, &y = 001AFC10, diff = 6
x= 0, y = 0

As I overheard and as I slightly expected but didnt get was that x and y was going to change due to the array changing but wasn't different to what they where set (guess random error cancels out another?)

Task 4

Practically the same but a larger difference in repeat of Task 2 of 6 bytes also

Size of arr: 4
Addr of arr: 00A27570
value of arr+4 : 10646900
value of &arr[4]: 10646900

Again same difference between arr+4 and &arr[4].

Difference between &x and &y when global

0 1 2 3 4 5 6 7 8 9 10
1 1 1 1 1 1 1 1 1 1 1

Task 5

p1 and p2 are pointers to q and r and hence their value are the memory location of the variables.

Task 6

Visual studio doesn't like that code and refused to compile it but I'm sure If i had more time and wasnt trying to get through it all emacs or gcc may have had more luck.

Task 7

My returned results:

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

I think that the returned memory address for my_struct was at the start of the program memory before the main function itself or an error it seems more likely (not of the whole physical memory though).

Task 8

&my_struct = 00000000
offsets:
my_char: 0
my_short 0
my_int 0
my_long 0
my_float 0
my_double 0

I did a quick google search to confirm what I think I remembered from teh lectures and saw here is that the union structure stores it all in the same address. Further I could see how this helps so that a function could rely on a piece of information having different types of data somewhat vaguely similar to overloading in Java.

Task 9

Slightly modded code to do post-debugging:

char *sp1, *sp2, *sp3;
sp1 = malloc (10);
printf("Addr of sp1: %p\n", &sp1);
sp2 = malloc (10);
printf("Addr of sp2: %p\n", &sp2);
free(sp1);
printf("Addr of sp1: %p\n", &sp1); //showing what happened to sp1's mem address
sp3  = malloc (10);
printf("Addr of sp3: %p\n", &sp3);

My results for task 9:

Addr of sp1: 0013FB34
Addr of sp2: 0013FB28
Addr of sp1: 0013FB34
Addr of sp3: 0013FB1C

The pointers in malloc are located decreasing in size and are set till termination really but the problem lies if it is freed and the free variable tries to use its old memory location again I think that data corruption or leaks would occur as another varible could be using the space previously occupied.

Reason for not much use of the Linux server

I tried to use the Linux server early on but there was a problem. The problem lies (I kinda realise the full extent of it now) in that I was using Visual Studio to write the code and compile on the windows PC and using the same *.c file to compile on the linux server (both use afs to read and write to so trying to use the same file :X ) I would stop using teh c file in VS but gcc didn't like it even being open in VS so I had to close VS to get around this which gets quite annoying when it thinks your opening VS for the first time every time and taking 5 mins to load.

PS on a side note I guess I should use emacs more and use the ssh server (putty is a far better than bash I think!)

Overall

I Think this lab was very good for getting into the realm of memory and pointers and I learnt quite a lot form it. ON the negative side was that trying to use both the Linux server and windows was quite a lot even though it does provide a good comparison and nice intro to ssh.