<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://wiki.kram.nz/index.php?action=history&amp;feed=atom&amp;title=SE250%3Alab-2%3Ajhor053</id>
	<title>SE250:lab-2:jhor053 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.kram.nz/index.php?action=history&amp;feed=atom&amp;title=SE250%3Alab-2%3Ajhor053"/>
	<link rel="alternate" type="text/html" href="https://wiki.kram.nz/index.php?title=SE250:lab-2:jhor053&amp;action=history"/>
	<updated>2026-05-01T23:52:32Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.3</generator>
	<entry>
		<id>https://wiki.kram.nz/index.php?title=SE250:lab-2:jhor053&amp;diff=4901&amp;oldid=prev</id>
		<title>Mark: 1 revision(s)</title>
		<link rel="alternate" type="text/html" href="https://wiki.kram.nz/index.php?title=SE250:lab-2:jhor053&amp;diff=4901&amp;oldid=prev"/>
		<updated>2008-11-03T05:18:58Z</updated>

		<summary type="html">&lt;p&gt;1 revision(s)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;                                                                     &lt;br /&gt;
                                                                     &lt;br /&gt;
                                                                     &lt;br /&gt;
                                             &lt;br /&gt;
{{Please leave this line alone (sandbox heading)}}&lt;br /&gt;
&amp;lt;!-- Hello! Feel free to try your formatting and editing skills below this line. As this page is for editing experiments, this page will automatically be cleaned every 12 hours. --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Lab 2 ==&lt;br /&gt;
===Task 1 ===&lt;br /&gt;
My code:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;int *ip;&lt;br /&gt;
char *ip2;&lt;br /&gt;
short *ip3;&lt;br /&gt;
long *ip4;&lt;br /&gt;
double *ip5;&lt;br /&gt;
float *ip6;&lt;br /&gt;
	 &lt;br /&gt;
printf(&amp;quot;int *ip %d\n&amp;quot;, sizeof(ip));&lt;br /&gt;
printf(&amp;quot;short *ip %d\n&amp;quot;, sizeof(ip3));&lt;br /&gt;
printf(&amp;quot;long *ip %d\n&amp;quot;, sizeof(ip4));&lt;br /&gt;
printf(&amp;quot;double *ip %d\n&amp;quot;, sizeof(ip5));&lt;br /&gt;
printf(&amp;quot;float *ip %d\n&amp;quot;, sizeof(ip6));&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Task 2 ===&lt;br /&gt;
My Code:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;int x = 0;&lt;br /&gt;
int x;&lt;br /&gt;
int y; &lt;br /&gt;
&lt;br /&gt;
printf(&amp;quot;&amp;amp;x = %p, &amp;amp;y = %p, diff = %ld\n&amp;quot;, &amp;amp;x, &amp;amp;y, (long)(&amp;amp;x - &amp;amp;y));&lt;br /&gt;
printf(&amp;quot;&amp;amp;x = %p, &amp;amp;y = %p, diff = %ld\n&amp;quot;, &amp;amp;x, &amp;amp;y, (long)&amp;amp;x - (long)&amp;amp;y);&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
It had 3 for the former and 12 for the last and same multiple of 4 on linux as well.&lt;br /&gt;
&lt;br /&gt;
=== Task 3 ===&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
int x = 0;&lt;br /&gt;
char arr[4] = {10};&lt;br /&gt;
int y = 0;&lt;br /&gt;
&lt;br /&gt;
printf(&amp;quot;Size of arr: %d\n&amp;quot;, sizeof(arr));&lt;br /&gt;
printf(&amp;quot;Addr of arr: %p\n&amp;quot;, &amp;amp;arr);&lt;br /&gt;
printf(&amp;quot;value of arr+4 : %d\n&amp;quot;, arr+4);&lt;br /&gt;
printf(&amp;quot;value of &amp;amp;arr[4]: %d\n&amp;quot;, &amp;amp;arr[4]);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
The size of the array was reported back as 4 bytes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;Address of arr: 0030F80C&lt;br /&gt;
value of arr+4 : 3209232&lt;br /&gt;
value of &amp;amp;arr[4]: 3209232&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
Not suprisingly arr + 4 bytes = &amp;amp;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.&lt;br /&gt;
&lt;br /&gt;
The table below is the difference between &amp;amp;x and &amp;amp;y:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 0&lt;br /&gt;
! 1&lt;br /&gt;
! 2&lt;br /&gt;
! 3&lt;br /&gt;
! 4&lt;br /&gt;
! 5&lt;br /&gt;
! 6&lt;br /&gt;
! 7&lt;br /&gt;
! 8&lt;br /&gt;
! 9&lt;br /&gt;
! 10&lt;br /&gt;
|-&lt;br /&gt;
| 3&lt;br /&gt;
| 6&lt;br /&gt;
| 6&lt;br /&gt;
| 6&lt;br /&gt;
| 6&lt;br /&gt;
| 7&lt;br /&gt;
| 7&lt;br /&gt;
| 7&lt;br /&gt;
| 7&lt;br /&gt;
| 8&lt;br /&gt;
| 8 &lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&amp;amp;x = 001AFC28, &amp;amp;y = 001AFC10, diff = 6&lt;br /&gt;
x= 0, y = 0&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
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&amp;#039;t different to what they where set (guess random error cancels out another?)&lt;br /&gt;
&lt;br /&gt;
=== Task 4 ===&lt;br /&gt;
Practically the same  but a larger difference in repeat of Task 2 of 6 bytes also&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;Size of arr: 4&lt;br /&gt;
Addr of arr: 00A27570&lt;br /&gt;
value of arr+4 : 10646900&lt;br /&gt;
value of &amp;amp;arr[4]: 10646900&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Again same difference between arr+4 and &amp;amp;arr[4].&lt;br /&gt;
&lt;br /&gt;
Difference between &amp;amp;x and &amp;amp;y when global&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! 0&lt;br /&gt;
! 1&lt;br /&gt;
! 2&lt;br /&gt;
! 3&lt;br /&gt;
! 4&lt;br /&gt;
! 5&lt;br /&gt;
! 6&lt;br /&gt;
! 7&lt;br /&gt;
! 8&lt;br /&gt;
! 9&lt;br /&gt;
! 10&lt;br /&gt;
|-&lt;br /&gt;
| 1&lt;br /&gt;
| 1&lt;br /&gt;
| 1&lt;br /&gt;
| 1&lt;br /&gt;
| 1&lt;br /&gt;
| 1&lt;br /&gt;
| 1&lt;br /&gt;
| 1&lt;br /&gt;
| 1&lt;br /&gt;
| 1&lt;br /&gt;
| 1&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Task 5 ===&lt;br /&gt;
p1 and p2 are pointers to q and r and hence their value are the memory location of the variables.&lt;br /&gt;
&lt;br /&gt;
=== Task 6 ===&lt;br /&gt;
Visual studio doesn&amp;#039;t like that code and refused to compile it but I&amp;#039;m sure If i had more time and wasnt trying to get through it all emacs or gcc may have had more luck.&lt;br /&gt;
&lt;br /&gt;
=== Task 7 ===&lt;br /&gt;
My returned results:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&amp;amp;my_struct = 00000000&lt;br /&gt;
offsets:&lt;br /&gt;
my_char: 0&lt;br /&gt;
my_short -2&lt;br /&gt;
my_int -4&lt;br /&gt;
my_long -8&lt;br /&gt;
my_float -12&lt;br /&gt;
my_double -16&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
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).&lt;br /&gt;
&lt;br /&gt;
=== Task 8===&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&amp;amp;my_struct = 00000000&lt;br /&gt;
offsets:&lt;br /&gt;
my_char: 0&lt;br /&gt;
my_short 0&lt;br /&gt;
my_int 0&lt;br /&gt;
my_long 0&lt;br /&gt;
my_float 0&lt;br /&gt;
my_double 0&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
=== Task 9 ===&lt;br /&gt;
Slightly modded code to do post-debugging:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;char *sp1, *sp2, *sp3;&lt;br /&gt;
sp1 = malloc (10);&lt;br /&gt;
printf(&amp;quot;Addr of sp1: %p\n&amp;quot;, &amp;amp;sp1);&lt;br /&gt;
sp2 = malloc (10);&lt;br /&gt;
printf(&amp;quot;Addr of sp2: %p\n&amp;quot;, &amp;amp;sp2);&lt;br /&gt;
free(sp1);&lt;br /&gt;
printf(&amp;quot;Addr of sp1: %p\n&amp;quot;, &amp;amp;sp1); //showing what happened to sp1&amp;#039;s mem address&lt;br /&gt;
sp3  = malloc (10);&lt;br /&gt;
printf(&amp;quot;Addr of sp3: %p\n&amp;quot;, &amp;amp;sp3);&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
My results for task 9:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;Addr of sp1: 0013FB34&lt;br /&gt;
Addr of sp2: 0013FB28&lt;br /&gt;
Addr of sp1: 0013FB34&lt;br /&gt;
Addr of sp3: 0013FB1C&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
===Reason for not much use of the Linux server===&lt;br /&gt;
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&amp;#039;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.&lt;br /&gt;
&lt;br /&gt;
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!)&lt;br /&gt;
&lt;br /&gt;
===Overall===&lt;br /&gt;
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.&lt;/div&gt;</summary>
		<author><name>Mark</name></author>
	</entry>
</feed>