Memory, Pointers, Records

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/19

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

20 Terms

1
New cards

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)

<p><span>Computer memory is usually byte addressable</span></p><p><span>That is to say memory can be thought of as an array of bytes, and each memory “cell” has a unique address</span></p><p><span>You can think of this as a character array, where the address is the index of the element (memory cell)</span></p>
2
New cards

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

3
New cards

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

<p><span>Here we are looking at memory in terms of “words” (or 32-bit values, which occupy 4 bytes)</span></p><p><span>The addresses go up in steps of 4</span></p>
4
New cards

Words Mapping to Bytes

This is little-endian format – least significant byte stored at first memory address

<p>This is little-endian format – least significant byte stored at first memory address</p>
5
New cards

Variables

three aspects:

  • a symbolic name 

  • a value it contains

  • an address where it resides in memory

6
New cards

Variable in Memory

<p></p>
7
New cards

Assignment Statements

val = val + 2;

The way we interpret the variable “val” is different depending on which side of the assignment statement it appears

8
New cards

Fetch and Store

<p></p>
9
New cards

Source and Destination

<p></p>
10
New cards

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
*/

11
New cards

&

returns the address of its operand

12
New cards

*

returns the value stored at the address contained within its operand

used to indicate that a variable is a pointer

13
New cards

Levels of Indirection

<p></p>
14
New cards

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” 

15
New cards

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

16
New cards

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

17
New cards

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

18
New cards

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

19
New cards

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';

20
New cards

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';