SE250:lab-2:sbas046

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

Task 1

This task demonstrated that all pointers are 4 bytes irrespective of what they are pointing to.

Code

#include <stdio.h>

int main(void)
{
double *ip;
printf("The size of the pointer is = %d", sizeof(ip));
return 0;
}

Windows Output:

The size of the pointer is = 4

Linux Output:

The size of the pointer is = 4

Task 2

When using long predefined for each variable the answers are as expected. When using a single long predefinition for the substraction however you get the expected answers divided by 4.

Code

#include <stdio.h>

int main(void)
{
    int x;
    int y;
    printf("The adress of x = %p\nThe adress of y = %p\nThe"
	   " difference of the adresses are = %ld\n", &x, &y, (long)&x- (long)&y);
    printf("The adress of x = %p\nThe adress of y = %p\nThe"
	   " difference of the adresses are = %ld\n", &x, &y, (long)(&x- &y));

    return 0;
}

Windows Output:

The adress of x = 0x22ccc4
The adress of y = 0x22ccbc
The difference of the adresses are = 4
The difference of the adresses are = 1

Linux Output:

The adress of x = 0xff81072c
 The adress of y = 0xff810728
The difference of the adresses are = 4
The difference of the adresses are = 1

Task 3

This task demonstrates that if you initialise a value to the (n+x)th position of an array, where the array is n long and x is any integer greater than one, the value initialised will be written outside the array and may overwrite other variables in memory.

Code

#include <stdio.h>

int main(void)
{

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

    x=0;
    y=0;
    arr[4] = 10;
    printf("The adress of x = %p\nThe adress of y = %p\nThe"
	   " difference of the adresses are = %ld\n", &x, &y, (long)&x- (long)&y);
    printf("adress of arr = %p\n"
	   "adress of arr[4] = %p\n"
	   "value of arr+4 = %d\n",
	   &arr, &arr[4], arr[4]);
    printf("x =%d\ny= %d", x, y);
    return 0;
}

Windows Output:

The adress of x = 0x22ccc4
The adress of y = 0x22ccbc
The difference of the adresses are = 8
adress of arr = 0x22ccc0
adress of arr[4] = 0x22ccc4
value of arr+4 = 10
x =10
y= 0

Linux Output:

The adress of x = 0x10010b64
The adress of y = 0x10010b68
The difference of the adresses are = -4
adress of arr = 0xffc4073c
adress of arr[4] = 0xffc40740
value of arr+4 = 10
x = 0
y = 0

Task 4

This time the memory adresses are in a different sectors altogether and hence have vastly different values. Also making x and y global variables enables you to change the character array without it overwriting the x or y variables, this is because these variables are in different memory sectors.


Task 5

p1 is the adress of q p2 is the adress of r


Task 6

This task demonstrates the different sectors of memory used under different situations.

Code

#include <stdio.h> 


char *local_str(){
    char s[8] = "1234567";
    return s;
}

char* local_str2(){
    char s[8] = "abcdefg";
    return s;
}

char *malloc_str(){
    char *s = malloc(8);
    strcpy(s, "hijklmn");
    return s;
}

char *static_str(){
    static char s[8] = "tuvwxyz";
    return s;
}

int main (void)
{
    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;

}

Windows Output:

sp = 0x22cc90(567)
sp = 0x22cc90(efg)
sp = 0x402000(tuvwxyz)
sp = 0x10601a0(hijklmn)

Linux Output:

sp = 0xffb0f704( þîÐ►)
sp = 0xffb0f704(abcdefg)
sp = 0x10010cf8(tuvwxyz)
sp = 0x10011008(hijklmn)
local_str = 0x100004dc

Task 7

This task demonstrates the adresses the variables are stored within a structure. It also shows that the structure itself has a NULL adress. This is because its not actually storing anything, its just C's way of representing an structure and its not really a physical memory structure.

Code

#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 (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;
}

Windows Output:

&my_struct = 0x0
offsets:
my_char: 0
my_short: -2
my_int: -4
my_long: -8
my_float: -12
my_double: -16

Linux Output:

&my_struct = 0xff917710
offsets:
my_char: 0
my_short: -2
my_int: -4
my_long: -8
my_float: -12
my_double: -16

Task 8

An union stacks the variables on top of each other. All the variables within the structure share the same adress


Task 9

This task demonstrates that if you free a malloced pointer it still points at the adress it was pointing to before but the memory is now unalocated.

Code

#include<stdio.h>

int main (void)
{
    char *sp1, *sp2, *sp3;

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

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

    printf("sp1 = %p\n"
	   "sp2 = %p\n"
	   "sp3 = %p\n\n",
	   sp1, sp2, sp3);
    
	
	return 0;
}

Windows Output:

gcc malloc.c -o malloc.exe && ./malloc.exe
sp1 = 0x1060198
sp2 = 0x611021a0
sp3 = 0x0

sp1 = 0x1060198
sp2 = 0x10701b0
sp3 = 0x0

sp1 = 0x1060198
sp2 = 0x10701b0
sp3 = 0x0

sp1 = 0x1060198
sp2 = 0x10701b0
sp3 = 0x1060198

Linux Output:

malloc.c: In function 'main':
malloc.c:7: warning: incompatible implicit declaration of built-in function 'mal
loc'
sp1 = 0x10011008
sp2 = 0xfffe39c4
sp3 = 0x1

sp1 = 0x10011008
sp2 = 0x10011018
sp3 = 0x1

sp1 = 0x10011008
sp2 = 0x10011018
sp3 = 0x1

sp1 = 0x10011008
sp2 = 0x10011018
sp3 = 0x10011008

Task 10

The functions are stored near the global variables in the actual code sector.

Code

#include <stdio.h>


char *local_str(){
    char s[8] = "1234567";
    return s;
}

char* local_str2(){
    char s[8] = "abcdefg";
    return s;
}

char *malloc_str(){
    char *s = malloc(8);
    strcpy(s, "hijklmn");
    return s;
}

char *static_str(){
    static char s[8] = "tuvwxyz";
    return s;
}

int main (void)
{
    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);

    printf("local_str = %p\n\n", local_str);
    
    return 0;
}

Windows Output:

sp = 0x22cc90(567)
sp = 0x22cc90(efg)
sp = 0x402000(tuvwxyz)
sp = 0x10201a0(hijklmn)
local_str = 0x401050

Linux Output:

sp = 0xffb0f704( þîÐ►)
sp = 0xffb0f704(abcdefg)
sp = 0x10010cf8(tuvwxyz)
sp = 0x10011008(hijklmn)
local_str = 0x100004dc