<?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%3Atlou006</id>
	<title>SE250:lab-2:tlou006 - 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%3Atlou006"/>
	<link rel="alternate" type="text/html" href="https://wiki.kram.nz/index.php?title=SE250:lab-2:tlou006&amp;action=history"/>
	<updated>2026-05-14T10:13:37Z</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:tlou006&amp;diff=5328&amp;oldid=prev</id>
		<title>Mark: 4 revision(s)</title>
		<link rel="alternate" type="text/html" href="https://wiki.kram.nz/index.php?title=SE250:lab-2:tlou006&amp;diff=5328&amp;oldid=prev"/>
		<updated>2008-11-03T05:19:13Z</updated>

		<summary type="html">&lt;p&gt;4 revision(s)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== 1 ==&lt;br /&gt;
Using the code&lt;br /&gt;
 &lt;br /&gt;
 printf(&amp;quot;%d\n&amp;quot;, sizeof(ip))&lt;br /&gt;
&lt;br /&gt;
on the pointer variable &amp;#039;&amp;#039;&amp;#039;ip&amp;#039;&amp;#039;&amp;#039; returned a value of 4 on both the linux and PC.&lt;br /&gt;
&lt;br /&gt;
Pointers are 4 bytes in memory.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== 2 ==&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;
&lt;br /&gt;
returned a difference of 4 on PC and 1 on linux.&lt;br /&gt;
&lt;br /&gt;
I am not sure what this result means. Why would the pointers take up less space on linux?&lt;br /&gt;
&lt;br /&gt;
The difference between &amp;#039;&amp;#039;&amp;#039;(long)&amp;amp;x - (long)&amp;amp;y  and  (long)(&amp;amp;x - &amp;amp;y)&amp;#039;&amp;#039;&amp;#039; is that the latter converts the answer from the &lt;br /&gt;
&lt;br /&gt;
subtraction to type &amp;#039;&amp;#039;&amp;#039;long&amp;#039;&amp;#039;&amp;#039; while the former does the conversion to long &amp;#039;&amp;#039;&amp;#039;before&amp;#039;&amp;#039;&amp;#039; the subtraction.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== 3 ==&lt;br /&gt;
Declaring an array of &amp;#039;&amp;#039;&amp;#039;type char&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;size 4&amp;#039;&amp;#039;&amp;#039; between the integer declarations x and y&lt;br /&gt;
&lt;br /&gt;
 int x;&lt;br /&gt;
 char arr[ 4 ];&lt;br /&gt;
 int y;&lt;br /&gt;
&lt;br /&gt;
Sizeof(arr) showed 4 as output. This was expected.&lt;br /&gt;
&lt;br /&gt;
The address of &amp;#039;&amp;#039;&amp;#039;&amp;amp;arr&amp;#039;&amp;#039;&amp;#039; was 4 bytes in front of the address of &amp;#039;&amp;#039;&amp;#039;&amp;amp;arr[ 4 ]&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
This means that &amp;#039;&amp;#039;&amp;#039;&amp;amp;arr&amp;#039;&amp;#039;&amp;#039; points to the first value in the array, something I should have known.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
My initial thoughts were that the difference between the addresses of x and y would be bigger, due to the new &lt;br /&gt;
&lt;br /&gt;
array taking up room in the memory. But&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== 5 ==&lt;br /&gt;
using the code&lt;br /&gt;
&lt;br /&gt;
 int *p1, *p2;&lt;br /&gt;
 int q;&lt;br /&gt;
 p1 = &amp;amp;q;&lt;br /&gt;
 int r;&lt;br /&gt;
 p2 = &amp;amp;r;&lt;br /&gt;
&lt;br /&gt;
Gave outputs for p1/p2 as huge values on the PC. On linux it gave huge &amp;#039;&amp;#039;&amp;#039;negative&amp;#039;&amp;#039;&amp;#039; numbers as outputs.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== 7 ==&lt;br /&gt;
After initialising a structure containing one of every variable type and running&lt;br /&gt;
&lt;br /&gt;
 printf(&amp;quot;&amp;amp;my_struct = %p\n&amp;quot;, 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;
 		&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;
The output was &lt;br /&gt;
&lt;br /&gt;
 &amp;#039;&amp;#039;&amp;#039;char 0&amp;#039;&amp;#039;&amp;#039; - meaning the char variable is first in the memory.&lt;br /&gt;
 &amp;#039;&amp;#039;&amp;#039;short -2&amp;#039;&amp;#039;&amp;#039; - followed by variable type short. Type char takes up 2 bytes.&lt;br /&gt;
 &amp;#039;&amp;#039;&amp;#039;int -4&amp;#039;&amp;#039;&amp;#039; - Short also takes up 2 bytes.&lt;br /&gt;
 &amp;#039;&amp;#039;&amp;#039;long -8&amp;#039;&amp;#039;&amp;#039; - Int takes up 4 bytes.&lt;br /&gt;
 &amp;#039;&amp;#039;&amp;#039;float -12&amp;#039;&amp;#039;&amp;#039; - Long takes up 4 bytes.&lt;br /&gt;
 &amp;#039;&amp;#039;&amp;#039;double -16&amp;#039;&amp;#039;&amp;#039; - I was told that a structure had size 24. Double takes up 8 bytes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== 8 ==&lt;br /&gt;
Using a &amp;#039;&amp;#039;&amp;#039;union&amp;#039;&amp;#039;&amp;#039; the output was&lt;br /&gt;
&lt;br /&gt;
 &amp;#039;&amp;#039;&amp;#039;char 0&amp;#039;&amp;#039;&amp;#039; &lt;br /&gt;
 &amp;#039;&amp;#039;&amp;#039;short 0&amp;#039;&amp;#039;&amp;#039; &lt;br /&gt;
 &amp;#039;&amp;#039;&amp;#039;int 0&amp;#039;&amp;#039;&amp;#039; &lt;br /&gt;
 &amp;#039;&amp;#039;&amp;#039;long 0&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
 &amp;#039;&amp;#039;&amp;#039;float 0&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
 &amp;#039;&amp;#039;&amp;#039;double 0&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
I was a bit confused by this. Perhaps it means all the variable types are placed together in memory?&lt;br /&gt;
&lt;br /&gt;
Need to know more about &amp;#039;&amp;#039;&amp;#039;union&amp;#039;&amp;#039;&amp;#039;s&lt;/div&gt;</summary>
		<author><name>Mark</name></author>
	</entry>
</feed>