Abstractions of memory

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

1/25

flashcard set

Earn XP

Description and Tags

1. Variables as an abstraction 2. Arrays in C 3. Strings in C 4. Passing into and returning from functions

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

26 Terms

1
New cards

c It provides direct access to:

hardware and, importantly, virtual memory e.g. RAM

2
New cards

Every byte of memory on your computer

has a unique memory address

3
New cards

A variable

a way of storing information in your computer's memory

4
New cards

Rather than loading data in and out of memory, and the programmer having
to keep track as to what is stored where

programming languages introduce
an abstraction: a variable.

5
New cards

int h2g2 = 42; int words

give me an address in memory for an integer (typically 4
bytes), and write the value 42 to it

6
New cards

h2g2 later in your code, this is compiled to in words:

go to the address where this variable stores its value, and fetch it for me

7
New cards

Declarations

tell the compiler the
name and type of something

8
New cards

To define a variable:
int h2g2;

You can define and initialise:
int h2g2 = 42;

9
New cards

A definition

tell the compiler the name and type of something but also tells the compiler
where it is stored in memory

10
New cards

find the location of where this is stored in memory

using the ampersand unary operator (&variable)

11
New cards

&h2g2 gives?

gives us the location in memory

12
New cards

printf("The value %d is at %p\n", h2g2, &h2g2); will print what with location as x

The value 42 is at x

13
New cards

Why would we want a variables address?

In C, we can only pass by value and return by value and If your code is passing around big
chunks of data, this is inefficient

14
New cards

Pointer variables

are just variables that store memory addresses

15
New cards

use of pointers

to pass around and store memory addresses in our programs

16
New cards

how do you refer to a pointer and give an example with and int

by the type of the thing they point to, followed by an asterisk int *

17
New cards
18
New cards

int h2g2 = 42;
int *p = &h2g2;

printf("The value %d is at %p\n", *p, p); what is this code doing

using a pointer The value 42 is at x to print the location

19
New cards

what is this code doing:

int i;
int ptr_to_i = &i;
int *ptr_to_ptr_to_i = &ptr_to_i;

a pointer to a pointer

20
New cards

write hoe to declare an array of length 9

#define ARRAY_LENGTH 9
int an_array[ARRAY_LENGTH];

21
New cards

how do arrays allocate memory

Arrays allocate to consecutive
points in memory, based on the
size of the type of array

22
New cards

how can you cast an_array to pointer

int ptr = (int) an_array;

23
New cards

how is this code saying printf("value at index 2 is %d\n", *(ptr + 2));

Take my address and offset 2 int’s worth of memory ahead

24
New cards

What happens if I lie to the compiler about the
type of variable being pointed to by a pointer? for example using a char for an array of ints

It can point to the wrong location as chars are a different size than int

25
New cards

what are chars in c

a single ASCII or UTF-8 character in C

26
New cards

what are used instead of strings in C

contiguous blocks of chars in memory,
terminated by a null character (\0)