SE250:lab-2:dcho040

From Marks Wiki
Jump to navigation Jump to search

Introduction

To get more information about the memory, all the codes of questions are gethered in one source file.

codes

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>

/* Q1_S*/
typedef struct {
	int struct_int_1;
	double struct_doube_1;
} type_struct;
/* Q1_F */

/* Q4_S */
int x_4;
char arr_4[4]; //change arr_4[x] x=0 to 10
int y_4;
/* Q4_F */

/* Q6_S */
char *local_str_6() {
	char s_6[8] = "0123456";
	printf("  s(local)  = %p(%s)\n", s_6, s_6);
	return s_6;
}

char *local_str2_6() {
	char s_6[8] = "abcdefg";
	printf("  s(local2) = %p(%s)\n", s_6, s_6);
	return s_6;
}

char *static_str_6() {
	static char s_6[8] = "tuvwxyz";
	printf("  s(static) = %p(%s)\n", s_6, s_6);
	return s_6;
}
char *malloc_str_6() {
	char *s_6 = malloc(8);
	strcpy(s_6, "hijklmn");
	printf("  s(malloc) = %p(%s)\n", s_6, s_6);
	return s_6;
}
/* Q6_F */



int main(void)
{

	/* Q1_S */
	int *ip_1;
	int int_1;
	long long_1;
	short short_1;
	float float_1;
	double double_1;
	char string_1[10];
	int array_1[5];
	type_struct struct_1;

	printf("1.type pointers(bytes)\n"
			" size of int pointer     = %d\n"
			" size of string pointer = %d\n"
			" size of array pointer  = %d\n"
			" size of struct pointer = %d\n\n"
			,sizeof(ip_1), sizeof(&string_1), sizeof(&array_1), sizeof(&struct_1));
	printf(" values(bytes)\n"
			" size of int      = %d\n"
			" size of long   = %d\n"
			" size of short  = %d\n"
			" size of float  = %d\n"
			" size of double = %d\n"
			" size of string(10 chars)     = %d\n"
			" size of array (5 ints)          = %d\n"
			" size of struct(int + double) = %d\n\n"
			, sizeof(int_1), sizeof(long_1), sizeof(short_1), sizeof(float_1)
			, sizeof(double_1), sizeof(string_1), sizeof(array_1), sizeof(struct_1));
	/* Q1_F */

	/* Q2_S */
	int x_2;
	int y_2;

	printf("2.address differences\n"
			" &x = %p(%d)\n"
			" &y = %p(%d)\n"
			" input value arr[4] = 10\n"
			" address diff(long)&x-(long)&y) = %ld\n"
			" ints    diff(long)(&x-&y)         = %ld\n\n"
			, &x_2, &x_2, &y_2, &y_2
			, (long)&x_2-(long)&y_2, (long)(&x_2-&y_2));
	/* Q2_F */

	/* Q3_S */
	int x_3;
	char arr_3[4]="abc"; //change arr_3[x] x=0 to 10
	int y_3;
	int *p_x_3;
	char *p_arr_3;
	int *p_y_3;
	p_x_3 = &x_3;
	p_arr_3 = arr_3; //&arr_3( incompatible pointer type)
	p_y_3 = &y_3;
	x_3 = 20;
	y_3 = 30;
	arr_3[4] = 10;

	printf("3. addresses\n"
			" about x(20)(then arr[4] = 10)\n"
			" value of x      = %d \n"
			" value of &x   = %p(%d) \n"
			" value of p_x   = %p(%d) \n"
			" value of *p_x  = %d \n"
			" value of &p_x = %p(%d)\n\n"
			,x_3, &x_3, &x_3,  p_x_3, p_x_3,  *p_x_3, &p_x_3, &p_x_3);
	printf(" about arr[4](abc)\n"
			" value of arr    = %s &arr    = %p(%d)\n"
			" value of arr[0] = %c, &arr[0] = %p(%d)\n"
			" value of arr[1] = %c, &arr[1] = %p(%d)\n"
			" value of arr[2] = %c, &arr[2] = %p(%d)\n"
			" value of arr[3] = %c, &arr[3] = %p(%d)\n"
			" value of arr[4] = %c, &arr[4] = %p(%d)\n"
			" value of p_arr    = %p(%d), &p_arr = %p(%d), *p_arr  = %c \n"
			" value of p_arr[0] = %c, &p_arr[0] = %p(%d), p_arr + 0 = %p(%d) \n"
			" value of p_arr[1] = %c, &p_arr[1] = %p(%d), p_arr + 1 = %p(%d)\n"
			" value of p_arr[2] = %c, &p_arr[2] = %p(%d), p_arr + 2 = %p(%d)\n"
			" value of p_arr[3] = %c, &p_arr[3] = %p(%d), p_arr + 3 = %p(%d)\n"
			" value of p_arr[4] = %c, &p_arr[4] = %p(%d), p_arr + 4 = %p(%c)\n\n"
			, arr_3, &arr_3, &arr_3
			, arr_3[0], &arr_3[0], &arr_3[0]
			, arr_3[1], &arr_3[1], &arr_3[1]
			, arr_3[2], &arr_3[2], &arr_3[2]
			, arr_3[3], &arr_3[3], &arr_3[3]
			, arr_3[4], &arr_3[4], &arr_3[4]
			, p_arr_3, p_arr_3, &p_arr_3, &p_arr_3, *p_arr_3
			, p_arr_3[0], &p_arr_3[0], &p_arr_3[0], p_arr_3+0, p_arr_3+0  // no *p_arr_3[0], &p_arr_3+0
			, p_arr_3[1], &p_arr_3[1], &p_arr_3[1], p_arr_3+1, p_arr_3+1
			, p_arr_3[2], &p_arr_3[2], &p_arr_3[2], p_arr_3+2, p_arr_3+2
			, p_arr_3[3], &p_arr_3[3], &p_arr_3[3], p_arr_3+3, p_arr_3+3
			, p_arr_3[4], &p_arr_3[4], &p_arr_3[4], p_arr_3+4, p_arr_3+4);
	printf(" about y(30)\n"
			" value of y      = %d \n"
			" value of &y    = %p(%d) \n"
			" value of p_y   = %p(%d) \n"
			" value of *p_y  = %d \n"
			" value of &p_y = %p(%d) \n\n"
			,y_3, &y_3, &y_3,  p_y_3, p_y_3,  *p_y_3, &p_y_3, &p_y_3);
	/* Q3_F */

	/* Q4_S */
	x_4 = 20;
	y_4 = 30;
	arr_4[4] = 10;

	printf("4. address of arr_4 as global variables\n"
			" value of &x      = %p(%d)\n"
			" value of &y      = %p(%d)\n"
			" value of arr      = %p(%d)\n"
			" value of &arr    = %p(%d)\n"
			" value of arr + 4 = %p(%d)\n"
			" value of &arr[4] = %p(%d)\n"
			" value of x(20)   = %d\n"
			" value of y(30)   = %d\n\n"
			, &x_4, &x_4, &y_4, &y_4, &arr_4, &arr_4, arr_4, arr_4, arr_4 + 4, arr_4 +4
			, &arr_4[4], &arr_4[4], x_4, y_4);
	/* Q4_F */

	/* Q5_S */
	printf("5. value of pointer p1 and p2\n");
	int *p1_5, *p2_5;

	printf( " value of &p1   = %p(%d)\n"
			" value of &p2   = %p(%d)\n"
			, &p1_5, &p1_5, &p2_5, &p2_5);

	{
		int q_5=10; p1_5 = &q_5;
		printf(" {value of &q = %p(%d) p1 -> q=10}\n", &q_5, &q_5);
	}

	{
		int r_5=20; p2_5 = &r_5;
		printf(" {value of &r = %p(%d) p2 -> r=20}\n", &r_5, &r_5);
	}

	int next_5, next2_5;

	printf( " value of p1    = %p(%d)\n"
			" value of p2    = %p(%d)\n"
			" value of *p1   = %d\n"
			" value of *p2   = %d\n"
			" value of &next  = %p(%d)\n"
			" value of &next2 = %p(%d)\n\n"
			, p1_5, p1_5, p2_5, p2_5, *p1_5, *p2_5, &next_5, &next_5, &next2_5, &next2_5);
	/* Q5_F */

	/* Q6_S */
	char *sp_6;
	printf("6. strings\n");
	sp_6 = local_str_6();
	printf(" sp         = %p(%s)\n", sp_6, sp_6);
	strcpy(sp_6, "XXXXXXX");
	printf(" sp  X'd   = %p(%s)\n\n", sp_6, sp_6);

	sp_6 = local_str_6();
	local_str2_6();
	printf(" sp         = %p(%s)\n", sp_6, sp_6);
	strcpy(sp_6, "XXXXXXX");
	printf(" sp  X'd   = %p(%s)\n\n", sp_6, sp_6);

	sp_6 = static_str_6();
	local_str2_6();
	printf(" sp         = %p(%s)\n", sp_6, sp_6);
	strcpy(sp_6, "XXXXXXX");
	printf(" sp  X'd   = %p(%s)\n\n", sp_6, sp_6);

	sp_6 = malloc_str_6();
	local_str2_6();
	printf(" sp         = %p(%s)\n", sp_6, sp_6);
	strcpy(sp_6, "XXXXXXX");
	printf(" sp  X'd   = %p(%s)\n\n", sp_6, sp_6);
	/* Q6_F */

	/* Q7_S */
	struct {
		char   my_char_7;
		short  my_short_7;
		int    my_int_7;
		long   my_long_7;
		float  my_float_7;
		double my_double_7;
	} my_struct_7;

	printf("7. struct\n &my_struct_7  = %p(%d)\n",&my_struct_7, &my_struct_7);
	printf(" offset:\n"
			" my_char    addr: %p(%d) diff: %ld\n"
			" my_short   addr: %p(%d) diff: %ld\n"
			" my_int      addr: %p(%d) diff: %ld\n"
			" my_long    addr: %p(%d) diff: %ld\n"
			" my_float    addr: %p(%d) diff: %ld\n"
			" my_double addr: %p(%d) diff: %ld\n\n"
			,&my_struct_7.my_char_7, &my_struct_7.my_char_7
			,(long)&my_struct_7 - (long)&my_struct_7.my_char_7
			,&my_struct_7.my_short_7, &my_struct_7.my_short_7
			,(long)&my_struct_7 - (long)&my_struct_7.my_short_7
			,&my_struct_7.my_int_7, &my_struct_7.my_int_7
			,(long)&my_struct_7 - (long)&my_struct_7.my_int_7
			,&my_struct_7.my_long_7, &my_struct_7.my_long_7
			,(long)&my_struct_7 - (long)&my_struct_7.my_long_7
			,&my_struct_7.my_float_7, &my_struct_7.my_float_7
			,(long)&my_struct_7 - (long)&my_struct_7.my_float_7
			,&my_struct_7.my_double_7, &my_struct_7.my_double_7
			,(long)&my_struct_7 - (long)&my_struct_7.my_double_7 );
	/* Q7_F */

	/* Q8_S */
	union {
		char   my_char_8;
		short  my_short_8;
		int    my_int_8;
		long   my_long_8;
		float  my_float_8;
		double my_double_8;
	} my_union_8;
	my_union_8.my_char_8   ='a';
	my_union_8.my_short_8  =  1;
	my_union_8.my_int_8    =  2;
	my_union_8.my_long_8   =  3;
	my_union_8.my_float_8  =4.1;
	my_union_8.my_double_8 =5.2;

	printf("8. union\n"
	        "  my_union_8.my_char_8     = 'a';\n"
			"  my_union_8.my_short_8    =   1;\n"
			"  my_union_8.my_int_8         =   2;\n"
			"  my_union_8.my_long_8      =   3;\n"
			"  my_union_8.my_float_8      = 4.1;\n"
			"  my_union_8.my_double_8  = 5.2;\n\n"
			" &my_union_8   = %p(%d)\n"
			, &my_union_8, &my_union_8);
	printf(" offset:\n"
			" my_char   addr: %p(%d)  diff: %ld value: %c \n"
			" my_short  addr: %p(%d)  diff: %ld value: %d \n"
			" my_int    addr: %p(%d)   diff: %ld value: %d \n"
			" my_long   addr: %p(%d)  diff: %ld value: %ld\n"
			" my_float  addr: %p(%d)   diff: %ld value: %f \n"
			" my_double addr: %p(%d) diff: %ld value: %lf\n\n"
			,&my_union_8.my_char_8, &my_union_8.my_char_8
			,(long)&my_union_8 - (long)&my_union_8.my_char_8
			,my_union_8.my_char_8
			,&my_union_8.my_short_8, &my_union_8.my_short_8
			,(long)&my_union_8 - (long)&my_union_8.my_short_8
			,my_union_8.my_short_8
			,&my_union_8.my_int_8, &my_union_8.my_int_8
			,(long)&my_union_8 - (long)&my_union_8.my_int_8
			,my_union_8.my_int_8
			,&my_union_8.my_long_8, &my_union_8.my_long_8
			,(long)&my_union_8 - (long)&my_union_8.my_long_8
			,my_union_8.my_long_8
			,&my_union_8.my_float_8, &my_union_8.my_float_8
			,(long)&my_union_8 - (long)&my_union_8.my_float_8
			,my_union_8.my_float_8
			,&my_union_8.my_double_8, &my_union_8.my_double_8
			,(long)&my_union_8 - (long)&my_union_8.my_double_8
			,my_union_8.my_double_8);
	/* Q8_F */

	/* Q9_S */
	char *sp1_9, *sp2_9, *sp3_9;
	sp1_9=(char*)malloc(10);
	int i;
	// sp1_9 <- {a b c d e f g h i j}
	for (i = 0; i < 10; i++){
		*(sp1_9 + i) = 97 + i;
	};
	// sp2_9 <- {k l m n o p q r s t}
	sp2_9=(char*)malloc(10);
	for (i = 0; i < 10; i++){
		*(sp2_9 + i) = 107 + i;
	};

	printf("9. malloc and free\n"
			" value of &sp1 = %p(%d)", sp1_9, sp1_9);
	for(i = 0; i < 10; i++){
		printf("%3c", *(sp1_9 + i));
	}
	printf("\n value of &sp2 = %p(%d)", sp2_9, sp2_9);
		for(i = 0; i < 10; i++){
			printf("%3c", *(sp2_9 + i));
	}

	// free sp1_9
   free( sp1_9 );
   	printf("\n free(sp1)\n"
   			" value of &sp1 = %p(%d)", sp1_9, sp1_9);
   	for(i = 0; i < 10; i++){
   		printf("%3c", *(sp1_9 + i));
   	}
   	printf("\n value of &sp2 = %p(%d)", sp2_9, sp2_9);
   		for(i = 0; i < 10; i++){
   			printf("%3c", *(sp2_9 + i));
	}

   // sp3_9 <- {u v w x y z}
	sp3_9=(char*)malloc(10);
	for (i = 0; i < 10; i++){
		*(sp3_9 + i) = 117 + i;
	};
	printf("\n define sp3 and put values in sp3\n"
			" value of &sp1 = %p(%d)", sp1_9, sp1_9);
	for(i = 0; i < 10; i++){
		printf("%3c", *(sp1_9 + i));
	}
	printf("\n value of &sp2 = %p(%d)", sp2_9, sp2_9);
		for(i = 0; i < 10; i++){
			printf("%3c", *(sp2_9 + i));
	}
	printf("\n value of &sp3 = %p(%d)", sp3_9, sp3_9);
		for(i = 0; i < 10; i++){
			printf("%3c", *(sp3_9 + i));
	}

	// sp1_9 <- {a b c d e f g h i j}
	for (i = 0; i < 10; i++){
		*(sp1_9 + i) = 97 + i;
	};
	printf("\n put values in sp1\n"
			" value of &sp1 = %p(%d)", sp1_9, sp1_9);
	for(i = 0; i < 10; i++){
		printf("%3c", *(sp1_9 + i));
	}
	printf("\n value of &sp2 = %p(%d)", sp2_9, sp2_9);
		for(i = 0; i < 10; i++){
			printf("%3c", *(sp2_9 + i));
	}
	printf("\n value of &sp3 = %p(%d)", sp3_9, sp3_9);
		for(i = 0; i < 10; i++){
			printf("%3c", *(sp3_9 + i));
	}
	printf("\n\n");
	/* Q9_F */

	/* Q10_S */
	printf("10. position of funcitons (using Q6)\n");
	printf(" local_str     = %p(%d)\n"
				" local_str2    = %p(%d)\n"
				" static_str    = %p(%d)\n"
				" malloc_str   = %p(%d)\n"
				, local_str_6, local_str_6
				, local_str2_6, local_str2_6
				, static_str_6, static_str_6
				, malloc_str_6, malloc_str_6);
	/* Q10_F */
	return 0;

}

Result

Vista

1.type pointers(bytes)
 size of int pointer     = 4
 size of string pointer = 4
 size of array pointer  = 4
 size of struct pointer = 4

 values(bytes)
 size of int      = 4
 size of long   = 4
 size of short  = 2
 size of float  = 4
 size of double = 8
 size of string(10 chars)     = 10
 size of array (5 ints)          = 20
 size of struct(int + double) = 16

2.address differences
 &x = 0x22cc5c(2280540)
 &y = 0x22cc58(2280536)
 input value arr[4] = 10
 address diff(long)&x-(long)&y) = 4
 ints    diff(long)(&x-&y)         = 1

3. addresses
 about x(20)(then arr[4] = 10)
 value of x      = 10 
 value of &x   = 0x22cc54(2280532) 
 value of p_x   = 0x22cc54(2280532) 
 value of *p_x  = 10 
 value of &p_x = 0x22cc48(2280520)

 about arr[4](abc)
 value of arr    = abc &arr    = 0x22cc50(2280528)
 value of arr[0] = a, &arr[0] = 0x22cc50(2280528)
 value of arr[1] = b, &arr[1] = 0x22cc51(2280529)
 value of arr[2] = c, &arr[2] = 0x22cc52(2280530)
 value of arr[3] =  , &arr[3] = 0x22cc53(2280531)
 value of arr[4] = 
, &arr[4] = 0x22cc54(2280532)
 value of p_arr    = 0x22cc50(2280528), &p_arr = 0x22cc44(2280516), *p_arr  = a 
 value of p_arr[0] = a, &p_arr[0] = 0x22cc50(2280528), p_arr + 0 = 0x22cc50(2280528) 
 value of p_arr[1] = b, &p_arr[1] = 0x22cc51(2280529), p_arr + 1 = 0x22cc51(2280529)
 value of p_arr[2] = c, &p_arr[2] = 0x22cc52(2280530), p_arr + 2 = 0x22cc52(2280530)
 value of p_arr[3] =  , &p_arr[3] = 0x22cc53(2280531), p_arr + 3 = 0x22cc53(2280531)
 value of p_arr[4] = 
, &p_arr[4] = 0x22cc54(2280532), p_arr + 4 = 0x22cc54(T)

 about y(30)
 value of y      = 30 
 value of &y    = 0x22cc4c(2280524) 
 value of p_y   = 0x22cc4c(2280524) 
 value of *p_y  = 30 
 value of &p_y = 0x22cc40(2280512) 

4. address of arr_4 as global variables
 value of &x      = 0x405040(4214848)
 value of &y      = 0x405020(4214816)
 value of arr      = 0x405030(4214832)
 value of &arr    = 0x405030(4214832)
 value of arr + 4 = 0x405034(4214836)
 value of &arr[4] = 0x405034(4214836)
 value of x(20)   = 20
 value of y(30)   = 30

5. value of pointer p1 and p2
 value of &p1   = 0x22cc3c(2280508)
 value of &p2   = 0x22cc38(2280504)
 {value of &q = 0x22cc34(2280500) p1 -> q=10}
 {value of &r = 0x22cc30(2280496) p2 -> r=20}
 value of p1    = 0x22cc34(2280500)
 value of p2    = 0x22cc30(2280496)
 value of *p1   = 10
 value of *p2   = 20
 value of &next  = 0x22cc2c(2280492)
 value of &next2 = 0x22cc28(2280488)

6. strings
  s(local)  = 0x22cae0(0123456)
 sp         = 0x22cae0(늻)
 sp  X'd   = 0x22cae0(봈@)

  s(local)  = 0x22cae0(0123456)
  s(local2) = 0x22cae0(abcdefg)
 sp         = 0x22cae0(늻)
 sp  X'd   = 0x22cae0(봈@)

  s(static) = 0x403000(tuvwxyz)
  s(local2) = 0x22cae0(abcdefg)
 sp         = 0x403000(tuvwxyz)
 sp  X'd   = 0x403000(XXXXXXX)

  s(malloc) = 0xde0158(hijklmn)
  s(local2) = 0x22cae0(abcdefg)
 sp         = 0xde0158(hijklmn)
 sp  X'd   = 0xde0158(XXXXXXX)

7. struct
 &my_struct_7  = 0x22cc00(2280448)
 offset:
 my_char    addr: 0x22cc00(2280448) diff: 0
 my_short   addr: 0x22cc02(2280450) diff: -2
 my_int      addr: 0x22cc04(2280452) diff: -4
 my_long    addr: 0x22cc08(2280456) diff: -8
 my_float    addr: 0x22cc0c(2280460) diff: -12
 my_double addr: 0x22cc10(2280464) diff: -16

8. union
  my_union_8.my_char_8     = 'a';
  my_union_8.my_short_8    =   1;
  my_union_8.my_int_8         =   2;
  my_union_8.my_long_8      =   3;
  my_union_8.my_float_8      = 4.1;
  my_union_8.my_double_8  = 5.2;

 &my_union_8   = 0x22cbf8(2280440)
 offset:
 my_char   addr: 0x22cbf8(2280440)  diff: 0 value: ?
 my_short  addr: 0x22cbf8(2280440)  diff: 0 value: -13107 
 my_int    addr: 0x22cbf8(2280440)   diff: 0 value: -858993459 
 my_long   addr: 0x22cbf8(2280440)  diff: 0 value: -858993459
 my_float  addr: 0x22cbf8(2280440)   diff: 0 value: -107374184.000000 
 my_double addr: 0x22cbf8(2280440) diff: 0 value: 5.200000

9. malloc and free
 value of &sp1 = 0xde01d0(14549456)  a  b  c  d  e  f  g  h  i  j
 value of &sp2 = 0xde01e0(14549472)  k  l  m  n  o  p  q  r  s  t
 free(sp1)
 value of &sp1 = 0xde01d0(14549456)  ? ? �  a  ? ? �  a  �   
 value of &sp2 = 0xde01e0(14549472)  k  l  m  n  o  p  q  r  s  t
 define sp3 and put values in sp3
 value of &sp1 = 0xde01d0(14549456)  u  v  w  x  y  z  {  |  }  ~
 value of &sp2 = 0xde01e0(14549472)  k  l  m  n  o  p  q  r  s  t
 value of &sp3 = 0xde01d0(14549456)  u  v  w  x  y  z  {  |  }  ~
 put values in sp1
 value of &sp1 = 0xde01d0(14549456)  a  b  c  d  e  f  g  h  i  j
 value of &sp2 = 0xde01e0(14549472)  k  l  m  n  o  p  q  r  s  t
 value of &sp3 = 0xde01d0(14549456)  a  b  c  d  e  f  g  h  i  j

10. position of funcitons (using Q6)
 local_str     = 0x401050(4198480)
 local_str2    = 0x401086(4198534)
 static_str    = 0x4010bc(4198588)
 malloc_str   = 0x4010e5(4198629)

Linux

1.type pointers(bytes)
 size of int pointer     = 4
 size of string pointer = 4
 size of array pointer  = 4
 size of struct pointer = 4

 values(bytes)
 size of int      = 4
 size of long   = 4
 size of short  = 2
 size of float  = 4
 size of double = 8
 size of string(10 chars)     = 10
 size of array (5 ints)          = 20
 size of struct(int + double) = 16

2.address differences
 &x = 0xffe94968(-1488536)
 &y = 0xffe94964(-1488540)
 input value arr[4] = 10
 address diff(long)&x-(long)&y) = 4
 ints    diff(long)(&x-&y)         = 1

3. addresses
 about x(20)(then arr[4] = 10)
 value of x      = 167772180 
 value of &x   = 0xffe94960(-1488544) 
 value of p_x   = 0xffe94960(-1488544) 
 value of *p_x  = 167772180 
 value of &p_x = 0xffe94954(-1488556)

 about arr[4](abc)
 value of arr    = abc &arr    = 0xffe9495c(-1488548)
 value of arr[0] = a, &arr[0] = 0xffe9495c(-1488548)
 value of arr[1] = b, &arr[1] = 0xffe9495d(-1488547)
 value of arr[2] = c, &arr[2] = 0xffe9495e(-1488546)
 value of arr[3] = ^@, &arr[3] = 0xffe9495f(-1488545)
 value of arr[4] = 
, &arr[4] = 0xffe94960(-1488544)
 value of p_arr    = 0xffe9495c(-1488548), &p_arr = 0xffe94950(-1488560), *p_arr  = a 
 value of p_arr[0] = a, &p_arr[0] = 0xffe9495c(-1488548), p_arr + 0 = 0xffe9495c(-1488548) 
 value of p_arr[1] = b, &p_arr[1] = 0xffe9495d(-1488547), p_arr + 1 = 0xffe9495d(-1488547)
 value of p_arr[2] = c, &p_arr[2] = 0xffe9495e(-1488546), p_arr + 2 = 0xffe9495e(-1488546)
 value of p_arr[3] = ^@, &p_arr[3] = 0xffe9495f(-1488545), p_arr + 3 = 0xffe9495f(-1488545)
 value of p_arr[4] = 
, &p_arr[4] = 0xffe94960(-1488544), p_arr + 4 = 0xffe94960(`)

 about y(30)
 value of y      = 30 
 value of &y    = 0xffe94958(-1488552) 
 value of p_y   = 0xffe94958(-1488552) 
 value of *p_y  = 30 
 value of &p_y = 0xffe9494c(-1488564) 

4. address of arr_4 as global variables
 value of &x      = 0x10012ac4(268511940)
 value of &y      = 0x10012ac0(268511936)
 value of arr      = 0x10012ac8(268511944)
 value of &arr    = 0x10012ac8(268511944)
 value of arr + 4 = 0x10012acc(268511948)
 value of &arr[4] = 0x10012acc(268511948)
 value of x(20)   = 20
 value of y(30)   = 30

5. value of pointer p1 and p2
 value of &p1   = 0xffe94948(-1488568)
 value of &p2   = 0xffe94944(-1488572)
 {value of &q = 0xffe94938(-1488584) p1 -> q=10}
 {value of &r = 0xffe94938(-1488584) p2 -> r=20}
 value of p1    = 0xffe94938(-1488584)
 value of p2    = 0xffe94938(-1488584)
 value of *p1   = 20
 value of *p2   = 20
 value of &next  = 0xffe94940(-1488576)
 value of &next2 = 0xffe9493c(-1488580)

6. strings
  s(local)  = 0xffe947c4(0123456)
 sp         = 0xffe947c4(0123456)
 sp  X'd   = 0xffe947c4(XXXXXXX)

  s(local)  = 0xffe947c4(0123456)
  s(local2) = 0xffe947c4(abcdefg)
 sp         = 0xffe947c4(abcdefg)
 sp  X'd   = 0xffe947c4(XXXXXXX)

  s(static) = 0x10012ab8(tuvwxyz)
  s(local2) = 0xffe947c4(abcdefg)
 sp         = 0x10012ab8(tuvwxyz)
 sp  X'd   = 0x10012ab8(XXXXXXX)

  s(malloc) = 0x10013008(hijklmn)
  s(local2) = 0xffe947c4(abcdefg)
 sp         = 0x10013008(hijklmn)
 sp  X'd   = 0x10013008(XXXXXXX)

7. struct
 &my_struct_7  = 0xffe948e8(-1488664)
 offset:
 my_char    addr: 0xffe948e8(-1488664) diff: 0
 my_short   addr: 0xffe948ea(-1488662) diff: -2
 my_int      addr: 0xffe948ec(-1488660) diff: -4
 my_long    addr: 0xffe948f0(-1488656) diff: -8
 my_float    addr: 0xffe948f4(-1488652) diff: -12
 my_double addr: 0xffe948f8(-1488648) diff: -16

8. union
  my_union_8.my_char_8     = 'a';
  my_union_8.my_short_8    =   1;
  my_union_8.my_int_8         =   2;
  my_union_8.my_long_8      =   3;
  my_union_8.my_float_8      = 4.1;
  my_union_8.my_double_8  = 5.2;

 &my_union_8   = 0xffe94928(-1488600)
 offset:
 my_char   addr: 0xffe94928(-1488600)  diff: 0 value: @ 
 my_short  addr: 0xffe94928(-1488600)  diff: 0 value: 16404 
 my_int    addr: 0xffe94928(-1488600)   diff: 0 value: 1075104972 
 my_long   addr: 0xffe94928(-1488600)  diff: 0 value: 1075104972
 my_float  addr: 0xffe94928(-1488600)   diff: 0 value: 2.325000 
 my_double addr: 0xffe94928(-1488600) diff: 0 value: 5.200000

9. malloc and free
 value of &sp1 = 0x10013018(268513304)  a  b  c  d  e  f  g  h  i  j
 value of &sp2 = 0x10013028(268513320)  k  l  m  n  o  p  q  r  s  t
 free(sp1)
 value of &sp1 = 0x10013018(268513304)  ^@ ^@ ^@ ^@ e  f  g  h  i  j
 value of &sp2 = 0x10013028(268513320)  k  l  m  n  o  p  q  r  s  t
 define sp3 and put values in sp3
 value of &sp1 = 0x10013018(268513304)  u  v  w  x  y  z  {  |  }  ~
 value of &sp2 = 0x10013028(268513320)  k  l  m  n  o  p  q  r  s  t
 value of &sp3 = 0x10013018(268513304)  u  v  w  x  y  z  {  |  }  ~
 put values in sp1
 value of &sp1 = 0x10013018(268513304)  a  b  c  d  e  f  g  h  i  j
 value of &sp2 = 0x10013028(268513320)  k  l  m  n  o  p  q  r  s  t
 value of &sp3 = 0x10013018(268513304)  a  b  c  d  e  f  g  h  i  j

10. position of funcitons (using Q6)
 local_str     = 0x1000052c(268436780)
 local_str2    = 0x100005bc(268436924)
 static_str    = 0x1000064c(268437068)
 malloc_str   = 0x100006a4(268437156)

Discussion

task1

  • All pointer has a same size.
  • Size of each type (int=4, long=4, short=2, float=4, double=8)(bytes)
  • Structure has more space than the space of values in the structure. structure(16 bytes), values in the sturcture(int(4 bytes) + double(8 bytes) = 12 bytes)

task2

  • &x-&y shows the distance between &x and &y by the size of type(int-4 bytes)
  • Address can be a minus value
  • Hexadecimal in Linux = 0x, visual studio = 00

task3

  • array
arr    : %s     all chars in the array
&arr   : %p(%d) the starting address of the array
&arr[4]: %p(%d) 5th char's address(=arr+4)
arr+4  : %p(%d) &arr + 4(4 chars)
  • pointer array
p_arr    : %p(%d) same as &arr
&p_arr   : %p(%d) the address of the pointer
p_arr+4  : %p(%d) same as arr+4
*p_arr   : %c     the first char of the array
*p_arr   : %s     not working
*p_arr[1]: %s     not working
*p_arr+1 : %s     not working
It seems array pointer can not use with %s
  • Visual studio show error when arr[0] is used but not with 'gcc'
actually arr[0] is not make any sense
  • x, arr[4], y saved in stack from the bottom and next to each other
in memory
y      (4 bytes)
arr[0] (1 byte)
arr[1]
arr[2]
arr[3]
x      (same starting address as arr[4])

Therefore arr[4]=10 also change x to 10 !! But it changed depending on the size of the array (more report at the bottom)

task4

  • Vista
global values are saved in statics area at every 16 bytes from the top (addresses always finish with 0 e.g. 0xDcho040 17:39, 15 March 2008 (NZDT)0) so x or y would not be changed unless input the value over 16th position of array 'arr'.
  • Linux
X and y keep the value after input arr[4] to 10 but it changed depending on the size of the array (more report at the bottom).

task5

  • Vista
values are saved in stack area from the bottom and next to each others even new values are in '{ }'
values defined in '{ }' are deleted after '{ }' but actual data are still remained in the memory.
Therefore, p1 still indicates 10, and p2 still indicates 20.
  • Linux
values defined in { } saved different place from values defined in main{ }
After { } for q is finished, r in { } saved in same place.
Therefore, both p1 and p2 indicate 20.

task6

  • Vista
Values in a local array defined in a function is deleted when the function is finished.
Therefore, there is nothing in the address of the local array so strcpy function also doesn't work.
After function is finished, then if new function starts, a local array in the new function use same memory as the previous array.
However, static and malloc arrays do no use same memory with local array and keep the values even the function is finished.
  • Linux
Linux works the same as Vista except that a local array in a function keeps the values after the function is finished.

task7

  • Vista
As expected, structure save all the information.
ps) 'char' take 2 bytes.
  • Linux
Linux works the same as Vista.

task8

  • Vista
All values in a union starts at same address
Only last value inputed in the union keeps the value, but the values inputed before are not
  • Linux
Linux works the same way as Vista

task9

  • Vista
Malloc() uses malloc area which is saved from the top
Free(name of pointer using malloc area) deletes all values in that malloc area but the pointer still indicates that address
After using free(name of point using malloc area), the malloc area deleted from 'free' would be used next time.
  • Linux
Linux works the same way as Vista except that when free is used only first 4 bytes in malloc area are deleted.

task10

  • Vista
Functions saved in the memory near global values saved.
  • Linux
Functions saved the first part of the memory.

Eddtion test

  • Sometimes 'arr[4]=10' change the value x, but sometiems not. Here is the reason.

Memory allocation of values defined in main()

File:Memory allocation.jpg

  • Vista saves the values from the bottom of memory.
  • Linux saves the values to both way in memory.
  • The same size of the arry shows the same results in every time.
  • The arry allocated with at least 4 bytes(4, 8, 12 bytes).
  • Hard to guess the allocations but the allocations make diffrences when the array has a size (*4 bytes(4, 8, 12 .. bytes))

Memory allocation of global values

File:Memory allocation 2.jpg

  • The allocation is not concerned by the order of values defined
  • Vista saves global values every 16 bytes (All global values end with 0(ox------0))
  • In Linux, an array which has over 8 bytes and less than 8 bytes are saved in different position.