SE250:March 10

From Marks Wiki
Jump to navigation Jump to search

Demotivator for the Day

File:Pointer(dog).jpg
POINTERS

They can bite you in the ass.


Announcement

Agenda

  • Lab 2 overview (Tuesday 11 March)
  • There is an option called 'Emacs Psychiatrist' under the 'Help' Menu in Emacs. It's very interesting to see the way it manipulates what the user types into a question. Perhaps a discussion on how this works.
  • Is using emacs compulsory or any other editor can be used, for example, Visual Studio, Eclipse etc?

Mgha023 01:26, 9 March 2008 (NZDT)

  • Whats good about using pointers? Why use pointers? Who in their right mind would use pointers?

Minutes

Minute taker: llay008


Answers to Questions in the Agenda

Emacs psychotherapist is a very simple program created in the 1960/70’s. The Psychotherapist takes fragments of your answer and echoes it back, most often using questions.

Emacs is not compulsory and Visual Studio and Eclipse may be used, although Visual Studio is limited as it is tied into the Windows system.

Pointers are necessary for a good program.

Revision of Friday's Lecture

On a PC the memory of the computer is separated five different zones.

•	The top half of the memory is read only and is dedicated to the program code.
•	The next block stores global variables.
•	Below it is the spaced reserved for malloc.
•	The fourth space is unallocated. 
•	The final zone is where the local variables are stored.  
       When the third or fifth zones need more memory it expands into the fourth zone.

The value 0 is neither read nor written. It can be used as a null pointer.

Different compilers use memory differently.

Logging onto Linux

The quickest way is to log into run bash shell. ssh (secure shell) You will need to enter: yourusername@login.cs.auckland.ac.nz. It will ask you for your password. This method enables you to edit locally as well as flip back between the two servers.

When working on emacs your files will be saved as a filename.exe. On Linux there is NO .exe extension. The two are NOT compatible so you will need to be consistent which program is run on which server.

Question: what does ./ mean? ./ is the path (.) = current directory (/) = file

Local & Global Variables and Addresses

You can use %p to print the addresses of variables.

Comparing the addresses of local and global variables, we see that local and global variables are stored in very different parts of the memory. We can also see that there is a larger space in the memory between the global variables than between the local variables.

When the addresses of x and y are subtracted and cast as a long, printing the difference between the addresses of local variables x and y gives a difference of 1. Printing the difference between the addresses of global variables g_x and g_y gives a difference of -4.

However, cast the variables separately ((long)&x – (long)&y as opposed to (long)&x -&y) and the output is 4 for the local variables and -16 for the global variables.

Pointers behave like integers, but when doing arithmetic they are scaled by type.

Question: Why use a long type? The int type may not be long enough to hold all the addresses available so a long is used.

Question: What happens when there is something, like an array, between variables x and y?

Malloc, Functions and Addresses

int *ip = malloc (10);
strcpy(ip, “012345678”);
printf(“ip = %p(%s)\n”, ip, ip);

This code creates a pointer to an array located in the malloc memory and copies the string “012345678” into the array. The address where the array is stored in memory is printed and the string stored in the array is also printed. The address is unlike any we have seen so far, which is to be expected as this is the first address we have looked at that is stored in the malloc part of the memory.

char * foo 1( ) 
{ 
	char *ip = malloc(10);
	strcpy(ip, “012345678”);
	return ip;
}
char* foo2 ( )
{
	static char ip[10];
	strcpy(ip, “012345678”);
	return ip;
}
*below is an example of bad programming.
char* foo3 ( )
	char ip [10];
	strcpy(ip, “012345678”);
	return ip;
}

Above are three different functions that will store the string “012345678” in memory. The last, however, will give you a warning when compiling and when printf is used to look at the address in memory and the string stored there, will give you a nonsense value for ip and you will find that the address is located near the local variables.

Question: Why is this? What has happened?

Structures in Memory

struct {
       char my_char;
       short my_short;
       int my_int;
} my_struct;
printf(“sizeof(my_struct) = %d\nsizeof(my_char) + sizeof(my_short)  + sizeof(my_int) = %d\n”,
sizeof(my_struct), sizeof(my_char) + sizeof(my_short)  + sizeof(my_int));

The above code finds the size of the structure (= 8) and the size of a char + the size of a short + the size of the int (= 7). What is interesting is that in this case the structure is allocated more space than is required. This may be due to odd boundaries. The computer often retrieves several amounts of data at once, so adding an extra byte may actually speed up the process. When the order of the types was rearranged within the structure, in one case it became twelve bytes long.

Looking at addresses inside the struct:

printf(“&my_struct = %p\n”, &my_struct);
printf(“offset  of my_struct = %ld\n”, (long)(&my_struct) – (long)(&my_struct.my_int));

This gives a value of -4. Using the char type gives a value of -2. Using union instead of type gives an offset of 0.