1. | Welcome | ToC | FAQ | Resources | Courses | Projects | Mail Lists | Members | Misc |
2. | Fundamentals | Languages | Tools | Net | Core | Advanced |
3. | Binary | Hardware | Software | Mac Programming |
4. | Lessons |
5. | Binary | Memory | CPU | Data | Files | Programming |


FMPC - Data

Data: Types and Variables

It is up to the programmer to instruct the computer what size of memory to allocate for each variable in a program. The programmer makes that choice based on the different number of states that a specific variable will possibly take on during the execution of the program. A high level language such as C provides built in standard 'data types' for all of the most commonly used types of data that a program would normally try to process. By specifying that a variable is to be considered of some specific pre-defined type that the compiler understands, the programmer may subsequently use that variable name throughout the program and know that the compiler will produce the correct instructions to deal with a piece of data of that type and will know how many bytes of memory are allocated to storing it.

Most of the commonly supported data types are 1, 2, or 4 bytes in length, although support for longer types and variable length types is also provided. In general, the compiler will do the best it can when the programmer tries to assign a variable of one type to the value of the variable of a different type. If the underlying types are compatible, in general the compiler will be able to work with them simultaneously. However, attempting to put a value that cannot be represented in the smaller into a variable of that smaller size will usually result in undesired results. I say usually because programmers WILL sometimes do this to truncate a value, but in most cases valuable information is lost in these types of moves, so it's important to remember what type of data the variable is that you are using when you do operations.

For the standard types available in C, please see the "Essence of C".

Structures, Unions, and Arrays

Many things that a programmer thinks of as a single entity are more complex than can be described with a single variable of any of the predefined types. C allows the programmer to define new types by defining 'structures' (some languages refer to structures as 'records'). A structure defines the association of an ordered list of variables of pre-existing types, thus creating a new type. Once a structure is defined, it may be used as a type in subsequent structures. When a variable is declared of the type so defined, the compiler allocates sufficient memory to hold each of the sub-variables that compose the type.

Sometimes the programmer wishes to use a specific collection of bytes as one type of variable in some sections of their code, but then wants to use that same exact set of bytes as if it were a different type of variable in another section. In COBOL, this was accomplished by using the "Redefines" command. In Pascal, by the use of "variant records". C provides the ability to define a given set of bytes as the 'union' of multiple different types.

Many times a program will process a list of entities that share common properties. To simplify the creation, management and processing of such lists of entities, most high level languages allow for defining and processing lists of items of the same type, or "arrays". A variable declared as an array is defined to be of some predefined type just as a non-array variable is defined, but the programmer also specifies how many instances of that variable are to be created to compose the array. In C, array items are numbered from 0 to <one-less-than-the-number-of-elements>.

Details on how structures, unions, and arrays are defined and used may be found in "The Essence of C".

Pointers and Handles

Each program is allocated a specific segment of the available RAM of the computer. Most programmers use "dynamic allocation" of memory to maximize use of the available pool of RAM. Dynamic allocation means that the programmer reserves memory for variables while they are in use, but then releases that memory when the variables aren't needed any longer.

The Mac OS has routines built in that allow the programmer to allocate memory on the fly. These routines allow the programmer to allocate a fixed size block of memory and return a value that is the memory address of where the variable resides in the computer's memory, also called a 'pointer' to the value. The C language incorporates syntax that allows the programmer to refer to a variable using pointers instead of the variable itself.

To give the OS more flexibility in managing the program's available memory, it is better to define dynamic variables as relocatable rather than as fixed. To make management of relocatable variables easier, the Mac OS allows dynamic allocation of memory to return a "handle" to the memory block instead of returning a pointer. A handle is a pointer to a pointer. The second pointer actually points to the memory block itself. The first pointer (the handle) points to the memory location that holds the second pointer. Although this may seem needlessly indirect, C supports this syntax well and by using this additional level of indirection the OS gains great flexibility in memory management. The specifics of how this works in the Mac OS are defined in greater detail in chapter 1 of "Mac C".


Copyright © 1996, 1997, 1998. Last Update to This Page: 1998/10/22
This Page Maintained by: radar pangaean * * * Original Author: radar pangaean
The MOST web site is built and maintained by the voluntary efforts/donations of our members.