1/19
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Byte Addressable Memory
Computer memory is usually byte addressable
That is to say memory can be thought of as an array of bytes, and each memory “cell” has a unique address
You can think of this as a character array, where the address is the index of the element (memory cell)
Words
size can differ from machine to machine
we are assuming a word is 32 bits (4 bytes) long
integer values are usually word size
Memory in terms of Words
Here we are looking at memory in terms of “words” (or 32-bit values, which occupy 4 bytes)
The addresses go up in steps of 4
Words Mapping to Bytes
This is little-endian format – least significant byte stored at first memory address
Variables
three aspects:
a symbolic name
a value it contains
an address where it resides in memory
Variable in Memory
Assignment Statements
val = val + 2;
The way we interpret the variable “val” is different depending on which side of the assignment statement it appears
Fetch and Store
Source and Destination
Pointers
let programmers pass memory addresses to the objects as values
char val;
char* addr;
val = 3;
addr = &val;
printf("val = %d, addr = %x\n", val, addr);
*addr = *addr + 2;
printf("val = %d, addr = %x\n", val, addr);
/* val = 3, addr = 4063e8
* val = 5, addr = 4063e8
*/
&
returns the address of its operand
*
returns the value stored at the address contained within its operand
used to indicate that a variable is a pointer
Levels of Indirection
Notation
int* x;
int *x;
int * x;
All of the above are equivalent (C ignores the whitespace)
Can be read as “x is a pointer to an int”
Records
A compound item
Unlike an array (which is homogeneous), it is heterogeneous – i.e., components of various types:
components are called fields
each field is identified using a name (not an index)
Holds various different properties of a single entity
Defining Records in C
#define MAXSIZE 20
//type define a structure named Student
typedef struct student {
char name[MAXSIZE];
int age;
char gender;
int entryYear;
char subject[MAXSIZE];
char maritalStatus;
} Student; //name of the type
Allocating Space for a Record
Student* newStudent()
//allocates space for a new Student record
//returns pointer to allocated space
{
Student* pt = malloc(sizeof(Student));
return pt;
}
creates the space for a new Student record, and returns a pointer to that space
malloc and sizeof Functions
dynamically allocates memory from an area within your program’s space called the heap
Student* pt = malloc(sizeof(Student));
use sizeof function to tell us how many bytes a Student record occupies
use malloc to actually allocate the space we require, returning the base address of the area of memory allocated
The Arrow (->) Operator
selects an attribute or field of the record
Student* stu = newStudent();
strcpy(stu->name, "James T. Kirk");
stu->age = 19; //(*stu).age = 19
stu->gender = 'M';
stu->entryYear = 2252;
strcpy(stu->subject, "Space Command");
stu->maritalStatus = 'm';
The Dot (.) Operator
used to access fields when a variable is declared directly
Student stu2;
strcpy(stu2.name, "Nyota Uhura");
stu2.age = 18;
stu2.gender = 'F';
stu2.entryYear = 2257;
strcpy(stu2.subject, "Communications");
stu2.maritalStatus = 's';