SE250:lab-2:asin185

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

Lab 2

Intro

For this lab we must generate results from both our local computers and the the Linux server. Then compare these results retrieved. This is done so that the different memory allocation can be shown for different C compilers.

Task 1

#include <stdio.h> 
int main () {

    int *ip;
    double *ip2;
    float *ip3;
    long *ip4;
    short *ip5;

    printf("int p = %d\ndouble p = %ld\nfloat p = %ld\nlong p = %ld\nshort p = %ld\n", sizeof(ip2) , sizeof(ip), sizeof(ip3),  sizeof(ip4), sizeof(ip5));
   
    return 0;
}
Int p    = 4
Double p = 4
Float p  = 4
Long p   = 4
Short p  = 4

Task 2

#include <stdio.h>
int main (){
   
   int x;
   int y;
   printf("&x = %p, &y = %p, diff = %ld\n", &x, &y, (long)&x - (long)&y);
   printf("&x = %p, &y = %p, diff = %ld\n", &x, &y, (long)(&x - &y));

   return 0;

}
&x = 0x22ccc4, &y = 0x22ccc0, diff = 4
&x = 0x22ccc4, &y = 0x22ccc0, diff = 1

Task 3

#include <stdio.h>
int main (){
   
   int x = 0;
   char arr[ 4 ];
   arr[ 4 ] = 10;
   int y = 0;
   printf("size of array = %d\n", sizeof(arr));
   printf("&arr = %p\n", &arr );
   printf("value &arr = %d\n", arr );
   printf("value &arr + 4  = %d\n", arr+4 );
   printf("value &arr[4] = %d\n", &arr[4] );
   printf("\n");
   printf("value of x = %d\nvalue of y = %d\n", x, y);
   printf("\n");
   printf("&x = %p, &y = %p, diff = %ld\n", &x, &y, (long)(&x - &y));

    return 0;

}
size of array = 4
&arr = 0x22ccc0
value &arr = 2280640
value &arr + 4  = 2280644
value &arr[4] = 2280644

&x = 0x22ccbc, &y = 0x22cc9c, diff = 8			0
&x = 0x22ccc4, &y = 0x22ccbc, diff = 2			1
&x = 0x22ccc4, &y = 0x22ccbc, diff = 2			2
&x = 0x22ccbc, &y = 0x22cc9c, diff = 8			3
&x = 0x22ccc4, &y = 0x22ccbc, diff = 2			4
&x = 0x22ccbc, &y = 0x22cc9c, diff = 8			5
&x = 0x22ccbc, &y = 0x22cc9c, diff = 8			6
&x = 0x22ccbc, &y = 0x22cc9c, diff = 8			7
&x = 0x22ccc4, &y = 0x22ccb4, diff = 4			8
&x = 0x22ccbc, &y = 0x22cc9c, diff = 8			9
&x = 0x22ccbc, &y = 0x22cc9c, diff = 8			10

value of x = 10
value of y = 0

Task 4

4.1

#include <stdio.h>

int x;
int y;
int main (){
    printf("&x = %p, &y = %p, diff = %ld\n", &x, &y, (long)(&x - &y));

return 0;
}
&x = 0x403030, &y = 0x403020, diff = 4
&x = 0x403030, &y = 0x403020, diff = 16

4.2

#include <stdio.h>
int x;
int y;
int main (){
    
   char arr[ 4 ];
    arr[ 4 ] = 10;
    printf("size of array = %d\n", sizeof(arr));
    printf("&arr = %p\n", &arr );
    printf("value &arr = %d\n", arr );
    printf("value &arr + 4  = %d\n", arr+4 );
    printf("value &arr[4] = %d\n", &arr[4] );

return 0;
}
size of array = 0
&arr = 0x22ccb0
value &arr = 2280624
value &arr + 4  = 2280628
value &arr[4] = 2280628

&x = 0x403030, &y = 0x403020, diff = 4			0
&x = 0x403030, &y = 0x403020, diff = 4			1
&x = 0x403030, &y = 0x403020, diff = 4			2
&x = 0x403030, &y = 0x403020, diff = 4			3
&x = 0x403030, &y = 0x403020, diff = 4			4
&x = 0x403030, &y = 0x403020, diff = 4			5
&x = 0x403030, &y = 0x403020, diff = 4			6	
&x = 0x403030, &y = 0x403020, diff = 4			7
&x = 0x403030, &y = 0x403020, diff = 4			8
&x = 0x403030, &y = 0x403020, diff = 4			9	
&x = 0x403030, &y = 0x403020, diff = 4			10        

value of x = 0
value of y = 0

Task 5

#include <stdio.h>

int x;
int y;
int main (){

   
    int *p1, *p2;
    { int q; p1 = &q; }
    { int r; p2 = &r; }
    
    printf("value of p1 = %d\nvalue of p2 = %d", p1, p2);

    
    return 0;
value of p1 = 2280636
value of p2 = 2280632

Task 6

 #include <stdio.h>
 
  char *local_str( ) {
 	char s[8] = "0123456";
 	return s;
  }
  char *local_str2( ) {
	char s[8] = "abcdefg";
	return s;
  }
  char *static_str( ) {
 	static char s[8] = "tuvwxyz";
 	return s;
  }
  char *malloc_str( ) {
	char *s = malloc( 8 );
	strcpy( s, "hijklmn" );
	return s;
 }

 int main (void) {
	char *sp;

	sp = local_str( );
	printf( "sp = %p(%s)\n", sp, sp );
	strcpy( sp, ”XXXXXXX” );
	printf( ”sp X’d = %p(%s)\n”, sp, sp );
	 
	sp = local_str( );
	local_str2( );
	printf( "sp = %p(%s)\n", sp, sp );
	strcpy( sp, ”XXXXXXX” );
	printf( ”sp X’d = %p(%s)\n”, sp, sp );     
 
	sp = static_str( );
	local_str2( );
	printf( "sp = %p(%s)\n", sp, sp );
	strcpy( sp, ”XXXXXXX” );
	printf( ”sp X’d = %p(%s)\n”, sp, sp );
	
	sp = malloc_str( );
	local_str2( );
	printf( "sp = %p(%s)\n", sp, sp );
	strcpy( sp, ”XXXXXXX” );
	printf( ”sp X’d = %p(%s)\n”, sp, sp );
	
	return 0;
 }

6.1

sp = 0x22cca0(456)
sp = 0x22cca0(efg)
sp = 0x402000(tuvwxyz)
sp = 0x670178(hijklmn)

6.2

sp = 0x22cca0(456)
sp X'd = 0x22cca0('1@)
sp = 0x22cca0(efg)
sp X'd = 0x22cca0('1@)
sp = 0x402000(tuvwxyz)
sp X'd = 0x402000(XXXXXXX)
sp = 0x670178(hijklmn)
sp X'd = 0x670178(XXXXXXX)

Task 7

     struct {
	  char my char;
	  short my short;
	 int my int;
	 long my long;
	 float my float;
	 double my double;
     } my struct;
     
            
     int main(void){
     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 );
              
             return 0; 
}
&my_struct = 0x404030
offsets:
my_char: 0
my_short: -2
my_int: -4
my_long: -8
my_float: -12
my_double: -16

Task 8

union {
	 char my_char;
	 short my_short;
	 int my_int;
	long my_long;
	float my_float;
	double my_double;
} my_union; 

int main (void) {
	printf( "&my_union = %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 );
	
	return 0;
}
&my_union = 0x404050
offsets:
my_char: 0
my_short: 0
my_int: 0
my_long: 0
my_float: 0
my_double: 0

Task 9

int main(void) {

	char *sp1, *sp2, *sp3;
	sp1 = malloc(10);
	sp2 = malloc(10);
	printf("sp1 = %p sp2 = %p\n", sp1, sp2); 

	free(sp1);
	sp3 = malloc(10);
	printf("sp1 = %p sp2 = %p sp3 = %p\n", sp1, sp2, sp3);
	
	strcpy( sp1, "abcdefghi" );
	printf("sp1 = %s\n", sp1);
	printf("sp3 = %s\n", sp3);
	
	return 0;
}
sp1 = 0x670188 sp2 = 0x670198
sp1 = 0x670188 sp2 = 0x670198 sp3 = 0x670188
sp1 = abcdefghi
sp3 = abcdefghi

Task 10

#include <stdio.h>

int main(void){ 

  printf("main = %p\n", main); 

  return 0;

}
local_str = 0x22cc40