SE250:lab-2:srag014

From Marks Wiki
Jump to navigation Jump to search

This second lab is primarily based on pointers.

Task 1

#include <stdio.h>
int main(){
      int *ip;
      printf("%d\n", sizeof(ip));
}

Size of all pointers no matter the data type or OS, they all had the same value of 4. I thought the value will vary in Linux but guess it doesnt.

Task 2

In task 2 we had to declare two variables x and y and print out their addresses and also the difference between these addresses

#include <stdio.h>
int main(void){
        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));
}

For Windows:
&x=0023FDB0 &y=0023FDA4 diff=3
&x=0023FDB0 &y=0023FDA4 diff=12

For Linux:
&x = 0xffa75710 &y = 0xffa7570c, difference = 1
&x = 0xffa75710 &y = 0xffa7570c, difference = 4
Note: Had some problems getting into linux as my pc didnt bring up my home drive so had to look at neighbours comp.

Task 3

In task 3 we introduce the statement char arr[4] between the x and y variables.

#include <stdio.h>
int main(void){
int x;
char arr[4];	
int y;
printf("enter needed statement here",)
}

Using Windows,we had to find:

  • Sizeof array: 4
  • Address of array: 0012FF54
  • Value of &arr:1245012
  • Value of arr+4:1245016
  • Value of &arr[4]:1245016

Varying the size of the array from 0 to 10, and recording the difference between &x and &y.

#include <stdio.h>
int main(){
int x;
char arr["enter number here"];
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));
}

Array sizes and their differences:
0|Not Applicable
1|6,24
2|6,24
3|6,24
4|6,24
5|7,28
6|7,28
7|7,28
8|7,28
9|8,32
10|8,32
Note: No results for linux due to me having technical issues.

Next step is reverting back to array size 4 and setting x and y to 0 and arr[4] to 10 and printing out values

#include <stdio.h>
int main(){
int x=0;
char arr[4] = {10};	
int y=0;
printf("Value of x=%d\nValue of y=%d\n", x, y);
}

x=0
y=0
Note: not sure if its correct.

Task 4

Declaring x and y as global variables.

#include <stdio.h>
int y;
int x;
int main(){
char arr["enter number here"];
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));
}

Array sizes and their differences:
0|Not Applicable
1|1,4
2|1,4
3|1,4
4|1,4
5|1,4
6|1,4
7|1,4
8|1,4
9|1,4
10|1,4
Note:Are the results meant to be the same??Also i get different results on different computers which is expected i suppose.

Task 5

int *p1, *p2;
{ int q; p1 = &q; }
{ int r; p2 = &r; }

I think the values of p1 and p2 will be whatever the address if q and r are since they will be pointing at p and q respectively.

Task 6

Program woudnt compile in visual studio.

Task 7

#include <stdio.h>
struct {
char my_char;
short my_short;
int my_int;
long my_long;
float my_float;
double my_double;
} my_struct;
int main ()
{   
printf( "&my struct = %p\n", my_struct );
printf( "offsets:\n"
"my_char: %ld\n"
"my_short: %ld\n"
"my_int: %ld\n"
"my_long: %ld\n"
"my_float: %ld\n"
"my_double: %ld\n",
(long)&my_struct - (long)&my_struct.my_char,
(long)&my_struct - (long)&my_struct.my_short,
(long)&my_struct - (long)&my_struct.my_int,
(long)&my_struct - (long)&my_struct.my_long,
(long)&my_struct - (long)&my_struct.my_float,
(long)&my_struct - (long)&my_struct.my_double );
}

Results:
offsets:
my_char: 0
my_short: -2
my_int: -4
my_long: -8
my_float: -12
my_double: -16

Task 8

#include <stdio.h>
union {
char my_char;
short my_short;
int my_int;
long my_long;
float my_float;
double my_double;
} my_union;
int main ()
{   
printf( "&my struct = %p\n", my_union );
printf( "offsets:\n"
"my_char: %ld\n"
"my_short: %ld\n"
"my_int: %ld\n"
"my_long: %ld\n"
"my_float: %ld\n"
"my_double: %ld\n",
(long)&my_union - (long)&my_union.my_char,
(long)&my_union - (long)&my_union.my_short,
(long)&my_union - (long)&my_union.my_int,
(long)&my_union - (long)&my_union.my_long,
(long)&my_union - (long)&my_union.my_float,
(long)&my_union - (long)&my_union.my_double );
}

Results from using union:
offsets:
my_char: 0
my_short: 0
my_int: 0
my_long: 0
my_float: 0
my_double: 0

Task 9

#include <stdio.h>
int main()
{
char *sp1, *sp2, *sp3;
sp1 = malloc( 10 );
printf("sp1=%p\n",sp1);	
sp2 = malloc( 10 );
printf("sp2=%p\n",sp2);	
free( sp1 );
sp3 = malloc( 10 );
printf("sp3=%p\n",sp3);
printf("sp1=%p\n",sp1);	
}


Results (windows):
sp1=00924B28
sp2=00922288
sp3=009222C0
sp1=00924B28
Results(linux):
sp1= 0xff809728
sp2= 0xff809724
sp3= 0xff809720
sp1= 0xff809728