SE250:lab-2:mabd065

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

Lab2


1. running printf("%d\n",sizeof(ip)) where ip is a pointer of type int gives the value 4 (i.e. 4 bytes).

Other types:

On the PC:

Short: 4
Float: 4
Double: 4
char: 4

On Linux:

Short: 4
Float: 4
Double: 4
char: 4

Looking at the results, all types above have the same size!!

I have tried to use %lg in the printf instead of %d and got some strange results (1.97626e-323 for Char!).



2. Executing (long) (&x - &y) gives:

PC:

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

Linux:

&x = 0xffb8c71c, &y = 0xffb8c718, diff = 1

Executing (long)&x - (long)&y gives:

PC:

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

Linux:

&x = 0xffbb371c, &y = 0xffbb3718, diff = 4



3.

Size of array on the PC & on linux is: 4

PC:

&arr = 0x22ccc0
arr+4 = 0x22ccc4
&arr[4] = 0x22ccc4

Linux:

&arr = 0xffdc9714
arr+4 = 0xffaa8718
&arr[4] = 0xff945718


CODE:

#include <stdio.h>

int main ()
{
    int x;
    char arr[10];
    int y;
    printf("&x = %p, &y = %p, diff = %ld \n", &x, &y, (long)(&x - &y));
    return 0;

}


Difference between x & y when arr is zero:

Arr = 0:

On PC:
&x = 0x22ccbc, &y = 0x22cc9c, diff = 8 
On linux:
&x = 0xffb6871c, &y = 0xffb68718, diff = 1

Arr = 1:

PC:
&x = 0x22ccc4, &y = 0x22ccbc, diff = 2 
Linux:
&x = 0xff883718, &y = 0xff883714, diff = 1

Arr = 2:

PC:
&x = 0x22ccc4, &y = 0x22ccbc, diff = 2 
Linux:
&x = 0xff9c9718, &y = 0xff9c9714, diff = 1

Arr = 3:

PC:
&x = 0x22ccbc, &y = 0x22cc9c, diff = 8
Linux:
&x = 0xff8f9718, &y = 0xff8f9714, diff = 1

Arr = 4:

PC:
&x = 0x22ccc4, &y = 0x22ccbc, diff = 2
Linux:
&x = 0xff96371c, &y = 0xff963714, diff = 2

Arr = 5:

PC:
&x = 0x22ccbc, &y = 0x22cc9c, diff = 8
Linux:
&x = 0xffc0871c, &y = 0xffc08718, diff = 1

Arr = 6:

PC:
&x = 0x22ccbc, &y = 0x22cc9c, diff = 8 
Linux:
&x = 0xffba171c, &y = 0xffba1718, diff = 1

Arr = 7:

PC:
&x = 0x22ccbc, &y = 0x22cc9c, diff = 8 
Linux:
&x = 0xffd2a71c, &y = 0xffd2a718, diff = 1

Arr = 8:

PC:
&x = 0x22ccc4, &y = 0x22ccb4, diff = 4
Linux:
&x = 0xffe98710, &y = 0xffe9870c, diff = 1

Arr = 9:

PC:
&x = 0x22ccbc, &y = 0x22cc9c, diff = 8 
Linux:
&x = 0xffc0670c, &y = 0xffc06708, diff = 1

Arr = 10:

PC:
&x = 0x22ccbc, &y = 0x22cc9c, diff = 8
Linux:
&x = 0xfff0770c, &y = 0xfff07708, diff = 1


After reverting Arr to size 4,

CODE:

#include <stdio.h>

int main ()
{
    int x = 0;
    char arr[4];
    arr[4] = 10;
    int y = 0;
     
    printf("&x = %p, &y = %p, diff = %ld \n", &x, &y, (long)(&x - &y));
    return 0;

}

I got the following:

PC:
x = 10, y = 0 
Linux:
x = 167772160, y = 0



4.

Repeat of task2:

CODE:

#include <stdio.h>

int x;
int y;

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

Executing (long) (&x - &y) gives:

PC:
&x = 0x403030, &y = 0x403020, diff = 4
Linux:
&x = 0x10010a64, &y = 0x10010a68, diff = -1

Executing (long)&x - (long)&y gives:

PC:
&x = 0x403030, &y = 0x403020, diff = 16  
Linux:
&x = 0x10010a64, &y = 0x10010a68, diff = -4


Repeat of task3:

Size of array on the PC & on linux is: 4

PC:

&arr = 0x403030
arr+4 = 0x403034
&arr[4] = 0x403034

Linux:

&arr = 0x10010a44
arr+4 = 0x10010a48
&arr[4] = 0x10010a48


CODE:

#include <stdio.h>

int x;
char arr[4];
int y;

int main ()
{
    printf("size of size of &arr = %p \n", &arr[4]);
    return 0;
}


Difference between x & y when arr is zero:

Arr = 0:

On PC:
&x = 0x403040, &y = 0x403020, diff = 8 
On linux:
&x = 0x10010a64, &y = 0x10010a68, diff = -1

Arr = 1:

PC:
&x = 0x403040, &y = 0x403020, diff = 8 
Linux:
&x = 0x403040, &y = 0x403020, diff = 8

Arr = 2:

PC:
&x = 0x403040, &y = 0x403020, diff = 8
Linux:
&x = 0x10010a64, &y = 0x10010a6c, diff = -2

Arr = 3:

PC:
&x = 0x403040, &y = 0x403020, diff = 8
Linux:
&x = 0x10010a64, &y = 0x10010a6c, diff = -2

Arr = 4:

PC:
&x = 0x403040, &y = 0x403020, diff = 8
Linux:
&x = 0x10010a64, &y = 0x10010a6c, diff = -2

Arr = 5:

PC:
&x = 0x403040, &y = 0x403020, diff = 8
Linux:
&x = 0x10010a64, &y = 0x10010a70, diff = -3

Arr = 6:

PC:
&x = 0x403040, &y = 0x403020, diff = 8
Linux:
&x = 0x10010a64, &y = 0x10010a70, diff = -3

Arr = 7:

PC:
&x = 0x403040, &y = 0x403020, diff = 8
Linux:
&x = 0x10010a64, &y = 0x10010a70, diff = -3

Arr = 8:

PC:
&x = 0x403040, &y = 0x403020, diff = 8
Linux:
&x = 0x10010a64, &y = 0x10010a70, diff = -3

Arr = 9:

PC:
&x = 0x403040, &y = 0x403020, diff = 8
Linux:
&x = 0x10010a64, &y = 0x10010a68, diff = -1

Arr = 10:

PC:
&x = 0x403040, &y = 0x403020, diff = 8
Linux:
&x = 0x10010a64, &y = 0x10010a68, diff = -1


After reverting Arr to size 4,

CODE:

#include <stdio.h>

int main ()
{
    int x = 0;
    char arr[4];
    arr[4] = 10;
    int y = 0;
     
    printf("&x = %p, &y = %p, diff = %ld \n", &x, &y, (long)(&x - &y));
    return 0;

}

I got the following:

PC:
x = 0, y = 0 
Linux:
x = 0, y = 0


CODE:

#include <stdio.h>

int x=0;
char arr[4];
int y=0; 
int main ()
{
    arr[4] = 10;
    printf("x = %d, y = %d\n", x, y);
    return 0;
}

5.

p1 = 2280636 (0x22ccc4), p2 = 2280632 (0x22ccc0) q = 0 (0x22ccbc), r = 1628704768 (0x22ccb8)

CODE:

#include <stdio.h>

int main ()
{
    int *p1,*p2;
    int q, r;
    p1 = &q;
    p2 = &r;

    printf("p1 = %d (%p), p2 = %d (%p)\n", p1, &p1, p2, &p2);
    printf("q = %d (%p), r = %d (%p)\n", q, &q, r, &r);
    return 0;
}

6. Results:

sp = 0x22cc90(456)
sp = 0x22cc90(efg)
sp = 0x22cc90(tuvwxyz)
sp = 0xda01a0(hijklmn)

CODE:

#include <stdio.h>
#include <string.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 ()
{
    char *sp;
    sp = local_str();
    printf("sp = %p(%s)\n", sp, sp);

    sp = local_str();
    local_str2();
    printf("sp = %p(%s)\n", sp, sp);

    sp = static_str();
    local_str2();
    printf("sp = %p(%s)\n", sp, sp);

    sp = malloc_str();
    local_str2();
    printf("sp = %p(%s)\n", sp, sp);

    return 0;
}


After adding the extra code, the results are:

sp = 0x22cc90(456)
sp X'd = 0x22cc90(5 @)
sp = 0x22cc90(efg)
sp X'd = 0x22cc90(5 @)
sp = 0x22cc90(tuvwxyz)
sp X'd = 0x22cc90(XXXXXXX)
sp = 0x10101a0(hijklmn)
sp X'd = 0x10101a0(XXXXXXX)
#include <stdio.h>
#include <string.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 ()
{
    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;
}

--Mabd065 13:35, 11 March 2008 (NZDT)