<?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%3Asshi080</id>
	<title>SE250:lab-2:sshi080 - 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%3Asshi080"/>
	<link rel="alternate" type="text/html" href="https://wiki.kram.nz/index.php?title=SE250:lab-2:sshi080&amp;action=history"/>
	<updated>2026-04-29T02:14:05Z</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:sshi080&amp;diff=5301&amp;oldid=prev</id>
		<title>Mark: 34 revision(s)</title>
		<link rel="alternate" type="text/html" href="https://wiki.kram.nz/index.php?title=SE250:lab-2:sshi080&amp;diff=5301&amp;oldid=prev"/>
		<updated>2008-11-03T05:19:12Z</updated>

		<summary type="html">&lt;p&gt;34 revision(s)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Intro ==&lt;br /&gt;
&lt;br /&gt;
This lab is to create a map to show the different memory areas used by each C compiler.&lt;br /&gt;
&lt;br /&gt;
== Task 1 ==&lt;br /&gt;
&lt;br /&gt;
Task 1 is to determine the sizes of different pointers.&lt;br /&gt;
&lt;br /&gt;
=== Windows Machine ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
printf(&amp;quot;Task 1:\n&amp;quot;);&lt;br /&gt;
printf(&amp;quot;Size of int pointer: %d\n&amp;quot;, sizeof(intptr));&lt;br /&gt;
printf(&amp;quot;size of double pointer: %d\n&amp;quot;, sizeof(dblptr));&lt;br /&gt;
printf(&amp;quot;Size of char pointer: %d\n\n&amp;quot;, sizeof(charptr));&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Result:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
4&lt;br /&gt;
4&lt;br /&gt;
4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It&amp;#039;s safe to say that all pointers have the same size as it doesn&amp;#039;t vary in length, it just a number showing the address of the variable it&amp;#039;s supposed to point to.&lt;br /&gt;
&lt;br /&gt;
=== Linux PPC64 Machine ===&lt;br /&gt;
&lt;br /&gt;
Testing on the linux server:&lt;br /&gt;
&lt;br /&gt;
Result:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
4&lt;br /&gt;
4&lt;br /&gt;
4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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)&lt;br /&gt;
&lt;br /&gt;
=== Pointers and other data types ===&lt;br /&gt;
&lt;br /&gt;
Comparing the sizes between pointers vs other types.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
printf(&amp;quot;Size of int variable: %d\n&amp;quot;, sizeof(aint));&lt;br /&gt;
printf(&amp;quot;Size of double variable: %d\n&amp;quot;, sizeof(bdouble));&lt;br /&gt;
printf(&amp;quot;Size of long variable: %d\n&amp;quot;, sizeof(clong));&lt;br /&gt;
printf(&amp;quot;Size of float variable: %d\n&amp;quot;, sizeof(dfloat));&lt;br /&gt;
printf(&amp;quot;Size of char variable: %d\n&amp;quot;, sizeof(echar));&lt;br /&gt;
printf(&amp;quot;Size of short variable: %d\n\n&amp;quot;, sizeof(fshort));&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Size of int variable: 4&lt;br /&gt;
Size of double variable: 8&lt;br /&gt;
Size of long variable: 4&lt;br /&gt;
Size of float variable: 4&lt;br /&gt;
Size of char variable: 1&lt;br /&gt;
Size of short variable: 2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int, long and float types have the same size as the pointers.&lt;br /&gt;
&lt;br /&gt;
== Task 2 ==&lt;br /&gt;
&lt;br /&gt;
This task is to compare the distance between two varibles, x and y.&lt;br /&gt;
The two different lines of code represent the bytes and bits apart respectively.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
printf(&amp;quot;Task2:\n&amp;quot;);&lt;br /&gt;
printf(&amp;quot;&amp;amp;x = %p &amp;amp;y = %p, difference = %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, difference = %ld\n\n&amp;quot;, &amp;amp;x, &amp;amp;y, ((long)&amp;amp;x - (long)&amp;amp;y));&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Windows Machine ===&lt;br /&gt;
Results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Task2:&lt;br /&gt;
&amp;amp;x = 002DFB24 &amp;amp;y = 002DFB18, difference = 3&lt;br /&gt;
&amp;amp;x = 002DFB24 &amp;amp;y = 002DFB18, difference = 12&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Linux Machine ===&lt;br /&gt;
Results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Task2:&lt;br /&gt;
&amp;amp;x = 0xffa75710 &amp;amp;y = 0xffa7570c, difference = 1&lt;br /&gt;
&amp;amp;x = 0xffa75710 &amp;amp;y = 0xffa7570c, difference = 4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Task 3 ==&lt;br /&gt;
&lt;br /&gt;
Task 3 is to determine the gap between x and y with an array of changing size &amp;#039;arr&amp;#039; in between to check the length difference.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int x;&lt;br /&gt;
char arr[4] = {10};	&lt;br /&gt;
int y;&lt;br /&gt;
&lt;br /&gt;
printf(&amp;quot;Task3: \n&amp;quot;);&lt;br /&gt;
printf(&amp;quot;Size of arr: %d\n&amp;quot;, sizeof(arr));&lt;br /&gt;
printf(&amp;quot;&amp;amp;arr = %p\n&amp;quot;, &amp;amp;arr);&lt;br /&gt;
printf(&amp;quot;&amp;amp;arr+4 = %p\n&amp;quot;, &amp;amp;arr+4);&lt;br /&gt;
printf(&amp;quot;&amp;amp;arr[4] = %p\n&amp;quot;, &amp;amp;arr[4]);&lt;br /&gt;
printf(&amp;quot;Values: x = %d, y = %d\n&amp;quot;, x, y);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Windows Machine ===&lt;br /&gt;
Results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Task3:&lt;br /&gt;
Size of arr: 4&lt;br /&gt;
&amp;amp;arr = 0xfffb870c&lt;br /&gt;
&amp;amp;arr+4 = 0xfffb871c&lt;br /&gt;
&amp;amp;arr[4] = 0xfffb8710&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then we vary the size of the array, from 0 to 10:&lt;br /&gt;
&lt;br /&gt;
Results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
arr[0] =  - N/A -&lt;br /&gt;
arr[1] = 6, 24&lt;br /&gt;
arr[2] = 6, 24&lt;br /&gt;
arr[3] = 6, 24&lt;br /&gt;
arr[4] = 6, 24&lt;br /&gt;
arr[5] = 7, 28&lt;br /&gt;
arr[6] = 7, 28&lt;br /&gt;
arr[7] = 7, 28&lt;br /&gt;
arr[8] = 7, 28&lt;br /&gt;
arr[9] = 8, 32&lt;br /&gt;
arr[10] = 8, 32&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It seems like every time we increase the size of the array by 4, the gap widens by 1 byte (4 bits)&lt;br /&gt;
&lt;br /&gt;
=== Linux Machine ===&lt;br /&gt;
&lt;br /&gt;
Results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
arr[0] =  - N/A -&lt;br /&gt;
arr[1] = 1, 4&lt;br /&gt;
arr[2] = 1, 4&lt;br /&gt;
arr[3] = 1, 4&lt;br /&gt;
arr[4] = 1, 4&lt;br /&gt;
arr[5] = 1, 4&lt;br /&gt;
arr[6] = 1, 4&lt;br /&gt;
arr[7] = 1, 4&lt;br /&gt;
arr[8] = 1, 4&lt;br /&gt;
arr[9] = 1, 4&lt;br /&gt;
arr[10] = 1, 4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It looks like on the linux machine the gap between the changing sizes is the same.&lt;br /&gt;
&lt;br /&gt;
== Task 4 ==&lt;br /&gt;
&lt;br /&gt;
Task 4 is the same as task 3 except we place the declarations outside any functions, setting them as globals.&lt;br /&gt;
&lt;br /&gt;
The results are very strange, I&amp;#039;m not sure if I have done it correctly.&lt;br /&gt;
&lt;br /&gt;
=== Windows Machine ===&lt;br /&gt;
Results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
arr[0] =  - N/A -&lt;br /&gt;
arr[1] = -1, -4&lt;br /&gt;
arr[2] = -1, -4&lt;br /&gt;
arr[3] = -1, -4&lt;br /&gt;
arr[4] = -1, -4&lt;br /&gt;
arr[5] = -1, -4&lt;br /&gt;
arr[6] = -1, -4&lt;br /&gt;
arr[7] = -1, -4&lt;br /&gt;
arr[8] = -1, -4&lt;br /&gt;
arr[9] = -1, -4&lt;br /&gt;
arr[10] = -1, -4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It seems like the gap doesn&amp;#039;t change, its always 1 byte apart from each other. Also the negative sign means the memory is being allocated downwards.&lt;br /&gt;
&lt;br /&gt;
=== Linux Machine ===&lt;br /&gt;
&lt;br /&gt;
Results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
arr[0] =  - N/A -&lt;br /&gt;
arr[1] = -2, -8&lt;br /&gt;
arr[2] = -2, -8&lt;br /&gt;
arr[3] = -2, -8&lt;br /&gt;
arr[4] = -2, -8&lt;br /&gt;
arr[5] = -3, -12&lt;br /&gt;
arr[6] = -3, -12&lt;br /&gt;
arr[7] = -3, -12&lt;br /&gt;
arr[8] = -3, -12&lt;br /&gt;
arr[9] = -1, -4&lt;br /&gt;
arr[10] = -1, -4&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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?&lt;br /&gt;
&lt;br /&gt;
== Task 5 ==&lt;br /&gt;
&lt;br /&gt;
Task 5 is a simple analysis task.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int *p1, *p2;&lt;br /&gt;
{ int q; p1 = &amp;amp;q; }&lt;br /&gt;
{ int r; p2 = &amp;amp;r; }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
p1 and p2 in this case are just pointing to the address of int q and r respectively.&lt;br /&gt;
&lt;br /&gt;
== Task 6 ==&lt;br /&gt;
&lt;br /&gt;
== Task 7 ==&lt;br /&gt;
&lt;br /&gt;
Task 7 is to determine the address of a struct and determine the difference within each element inside the struct.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
struct {&lt;br /&gt;
	char my_char;&lt;br /&gt;
	short my_short;&lt;br /&gt;
	int my_int;&lt;br /&gt;
	long my_long;&lt;br /&gt;
	float my_float;&lt;br /&gt;
	double my_double;&lt;br /&gt;
} my_struct;&lt;br /&gt;
&lt;br /&gt;
int main(void) {&lt;br /&gt;
	&lt;br /&gt;
	printf(&amp;quot;&amp;amp;my_struct = %p\n&amp;quot;, &amp;amp;my_struct );&lt;br /&gt;
	printf(&amp;quot;offsets:\n&amp;quot;&lt;br /&gt;
	&amp;quot;my_char: %ld\n&amp;quot;&lt;br /&gt;
	&amp;quot;my_short: %ld\n&amp;quot;&lt;br /&gt;
	&amp;quot;my_int: %ld\n&amp;quot;&lt;br /&gt;
	&amp;quot;my_long: %ld\n&amp;quot;&lt;br /&gt;
	&amp;quot;my_float: %ld\n&amp;quot;&lt;br /&gt;
	&amp;quot;my_double: %ld\n&amp;quot;,&lt;br /&gt;
	(long)&amp;amp;my_struct - (long)&amp;amp;my_struct.my_char,&lt;br /&gt;
	(long)&amp;amp;my_struct - (long)&amp;amp;my_struct.my_short,&lt;br /&gt;
	(long)&amp;amp;my_struct - (long)&amp;amp;my_struct.my_int,&lt;br /&gt;
	(long)&amp;amp;my_struct - (long)&amp;amp;my_struct.my_long,&lt;br /&gt;
	(long)&amp;amp;my_struct - (long)&amp;amp;my_struct.my_float,&lt;br /&gt;
	(long)&amp;amp;my_struct - (long)&amp;amp;my_struct.my_double);&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Windows Machine ===&lt;br /&gt;
&lt;br /&gt;
Results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;my_struct = 00307560&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&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Linux Machine ===&lt;br /&gt;
&lt;br /&gt;
Results:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;my_struct = 0x10010ac0&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&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The results between the windows and linux machines are the same, except for the memory addresses.&lt;br /&gt;
&lt;br /&gt;
== Task 8 ==&lt;br /&gt;
&lt;br /&gt;
This task is the same as task 7 except for instead of using a struct, we use a union&lt;br /&gt;
&lt;br /&gt;
=== Windows Machine ===&lt;br /&gt;
Results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;my_union = 00247560&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&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are no offsets in the children types inside the union.&lt;br /&gt;
&lt;br /&gt;
=== Linux Machine ===&lt;br /&gt;
Results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;my_union = 0x10010ab0&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&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The results between the windows and linux machines are the same, except for the memory addresses.&lt;br /&gt;
&lt;br /&gt;
== Task 9 ==&lt;br /&gt;
&lt;br /&gt;
This task to check the memory positions of malloc() and see what the results are when malloc is freed.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int main(void) {&lt;br /&gt;
	&lt;br /&gt;
	int a;&lt;br /&gt;
&lt;br /&gt;
	char *sp1, *sp2, *sp3;&lt;br /&gt;
	sp1 = malloc(10);&lt;br /&gt;
	sp2 = malloc(10);&lt;br /&gt;
	free(sp1);&lt;br /&gt;
	sp3 = malloc(10);&lt;br /&gt;
	&lt;br /&gt;
	printf(&amp;quot;Address of a: %p\n&amp;quot;, &amp;amp;a);&lt;br /&gt;
	printf(&amp;quot;Address of sp1: %p, Address of sp2: %p, Address of sp3: %p\n&amp;quot;, &amp;amp;sp1, &amp;amp;sp2, &amp;amp;sp3);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Windows Machine ===&lt;br /&gt;
&lt;br /&gt;
Results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Address of a: 002DF96C&lt;br /&gt;
Address of sp1: 002DF960, Address of sp2: 002DF954, Address of sp3: 002DF948&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Linux Machine ===&lt;br /&gt;
&lt;br /&gt;
Results:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Address of a: 0xff80972c&lt;br /&gt;
Address of sp1: 0xff809728, Address of sp2: 0xff809724, Address of sp3: 0xff809720&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mark</name></author>
	</entry>
</feed>