Week 5 - Complex Data Types & Memory Flashcards
What is a primitive data type?
The basic types of data which come default with the programming language.
Eg: boolean, float/real, int, string etc.
What’s the difference between an array and a class/record?
A class/record is used to define the blueprint of a general things. This doesn’t contain any actual values.
However, an array is used for storing multiple variables and values.
Pass by Value
Takes a copy of the variable and passes it to the called function or procedure.
Passes only the value of the variable.
Like downloading Word Doc to edit on your own. Changes made are independent of the main doc.
Pass by Reference
Takes a copy of the link to the content passes it to the called function or procedure.
Passes the actual variable and value.
Like emailing a link to a Google Docs which allows for everyone to make changes to the main document.
What are the main parts of memory?
The code area, global variables, the stack and the heap.
What are pointers and references used for?
Pointers/references are found on the stack, and link to a value or variable on the heap.
Can you assign values to fields once you define a type?
No. Because you need to first initialise the values.
This is where you have the placeholder values, so that you can then pass values into that class/function.
What are the main ways of using complex and custom data types in Ruby?
Classes/records, structs & modules/enumerations.
The Stack
A part of the computer memory responsible for storing function calls and local variables.
The Heap
It’s a large pool of memory where the running program can request chunks of memory.
This is typically used alongside pointers and references on the stack to point to memory on the heap.
This is typically called dynamic allocation.
Code Area
Where class definitions go and main code goes.
This area of memory is fixed.
Global Variables
Where all global variables and constants accessible to all functions are stored in the computer memory.
This area of memory is fixed.
What’s the difference between the stack and the heap?
The stack is a linear data structure, whereas the heap is a hierarchical data structure.
The stack is fixed, whereas the heap is dynamic and can change throughout the runtime of a program.
Enumerations
Modules define an enumeration which is a custom data type.
They are not not like classes or structs because they are created as a set of constants.
Attributes
Used to define data/special characteristics of a class/record. They make up the blueprint of our class.
Methods
Functions/procedures used to handle the behaviour.
Instance/Object
Is a concrete occurrence of any object. This is the same thing as a token.
Eg: a program may have a class/object named Animal, but there could be many instances of Animal, such as lion, cat, and dog.
What’s the difference between instance and class?
Class is the blueprint for an object, whereas the instance is a specific token of that class.
If our class is animals, then an instance could be dog.
How do we access enumerations?
Access by using ‘Class name here’::’value’
How do we add values onto the end of an array?
Use: Array «_space;value
Stack Overflow
Occurs when the space on the stack has exceeded and the operating system will stop and complain to fix this issue.
What does .new() do?
Allows for a programmer to allocate memory on the heap.
Objects
An abstraction used to represent objects in the real world such as cars, birds, trees etc.
Objects have 2 features which are data & behaviour.
Eg: colour, name, speed
Eg: drive, stop, accelerate
Why do we need to use the heap?
It allows for programmers to have more control over the computer memory being used.
What’s the difference between a pointer and a reference?
A pointer has the freedom to be assigned to the location of a variable.
References are directly put onto a variable, meaning that you can now call the variable two ways, by the variable name or the reference name.