SE250:lab-2:sdal039

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

This lab started off a lot smoother than the last one. Partly due to more exposure to the C language, but also because the lab handout gave very specific instructions making it easy to follow and understand.

One problem encountered was using the wiki. After typing up a signification amount of text and data the wiki decided it would log me out, and so I had to start again.

Another issue encountered was with C. On compiling I sometimes got a very large list of errors that appear very intimidating and don't look like they will be much fun to fix. However if you read through your code again you can see that it might only be one small mistake that has caused problems to carry on through the code, creating the mass of errors.

Question 1

Size of pointer for different file types:

         Windows      Linux
Int          4           4   
Float        4           4  
Double       4           4  
Long         4           4    
printf("%d\n", sizeof(ip));

This shows that a pointer is a fixed sized, no matter what it is pointing to. This can be explained by the fact that they contain a memory address instead of a value. For the same pc, this memory address will be a fixed size. However both the linux and windows machine gave a size of '4' even though the windows machine is 32bit and the linux machine 64bit.

Question 2

Find the difference in memory address between two local int variables.

       Windows                  Linux    
Address                        Address                           
&x =  0x22ccc0                 0xff97a730                                  
&y =  0x22ccb8                 0xff97a728

Difference                                                                            
long(diff) =          2          2
diff(longx - longy) = 8          8

While the memory locations from the linux and windows machines are completely different, from the same machine both values are in a very similar area of memory. The different 'difference' values from computing long(x-y) and longx - longy can be attributed to how C makes us think memory is stored. When &x - &y is computed and a value of 2 is returned, C is telling us that there are 2, 4bit values between them. longx-longy is the actual numerical value between the two memory address and it is saying that there is simply 8 bits difference between them.

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

Question 3

Part 1

      Windows                  Linux                          
Size:    4                      4
arr:     0x22ccbc               0xff97a72c
arr + 4: 0x22cccc               0xff97a73c   
arr[4]:  0x22ccc0               0xff97a730

What this shows is that the array starts at a specific memory address. Adding 4 to the address of this array gives a larger value for memory address, whereas the memory address of the 4th element of the array is a smaller address. This implies that largest memory address in the array is arr[0], and it decreases for each subsequent array element.

printf("size of array: %d\n", sizeof(arr));
printf("location of array: %p\n", &arr);
printf("location of array + 4: %p\n", &arr + 4);
printf("location of array[4]: %p\n", &arr[4]);
printf("array size: %d, x/y diff: %ld\n", sizeof(arr), (long)&x -(long)&y);
printf("x:%d, y:%d\n", x ,y);  

Part 2

   Windows                     Linux                          
array size: 4                  4
x/y diff:   8                  8   
x:          10                 167772160
y:          0                  0                       

Trying to set the value of an array element that doesn't exist causes it to overflow into the next memory slot, which is where the variable 'x' resides. On windows the variable takes the value without a problem, on linux however something different happens.

Part 3 This data shows the separation of variables x and y in memory as and array that is declared inbetween them increases in size.

Size      Windows        Linux
0         28             4        
1         8              4       
2         8              4       
3         28             4        
4         8              8       
5         28             4        
6         28             4        
7         28             4        
8         12             4        
9         28             4        
10        28             4

I was not able to make sense of these results as they appear to be very random.

Question 4

     Windows                   Linux                          
x:                                                   
y:                                                   


Question 5

p1 has been declared as a pointer (*p1). p1 is then assigned to &q, which is the location of the integer q. p1 will now point to the value of the int q.

p2 has been assigned in the same way and so will point to the value of int r.


Question 6

On running this code, these errors were returned

memory.c: In function `local_str': memory.c:5: warning: function returns address of local variable memory.c: In function `local_str2': memory.c:9: warning: function returns address of local variable

Windows 
sp = 0x22cc70(456)
sp X'd = 0x22cc70(E1@)

sp = 0x22cc70(efg)
sp X'd = 0x22cc70(E1@)

sp = 0x402000(tuvwxyz)
sp X'd = 0x402000(XXXXXX)

sp = 0xda01a0(hijklmn)
sp X'd = 0xda01a0(XXXXXX)

When run of the windows machine, the first thing that is noticed is that the value of the address that sp is pointing to has lost data. Instead of 0123456 and abcdefg we have 456 and efg. In local_str and local_str2, char s has been declared inside the function. This makes it a local variable to the function, and so when you attempt to access it outside of the function, something goes wrong. This is also noticble with sp X'd. For local_str and local_str2, there is garbage data returned instead of XXXXXX.

Another point to note is the location of the different variable types. Local function variables are stored in a similar area to the variables x and y that were used in question 2 and 3.

The static variable is in memory location 0x402000 which is in an entirely different area of memory.

The malloc variable is in memory location 0xda01a0 which, again, is in a completely different area of memory.

These are the results from running on the linux machine.

sp = 0xffd496f4(0123456)
sp X'd = 0xffd496f4(XXXXXX)

sp = 0xffd496f4(abcdefg)
sp X'd = 0xffd496f4(XXXXXX)

sp = 0x10011124(tuvwxyz)
sp X'd = 0x10011124(XXXXXX)

sp = 0x10012008(hijklmn)
sp X'd = 0x10012008(XXXXXX)

All of the results appear as one would expect. The memory locations of local, static and malloc variables are also similarly spread as they are on the windows machine.