Skip to content

Instantly share code, notes, and snippets.

@jki127
Last active November 5, 2019 16:18
Show Gist options
  • Save jki127/09a4fa6f89b4c374fdb2fa85967a46db to your computer and use it in GitHub Desktop.
Save jki127/09a4fa6f89b4c374fdb2fa85967a46db to your computer and use it in GitHub Desktop.

Morphs & Memory Management - Programming Languages - Oct 29th, 2019

Table of Contents

Morphs

The class of things that has a graphical representation on the screen is called Morph.

Every morph knows how to draw itself on the screen.

Here's how we create a window.

Morph new openInWindow.

Here's what we see when look at this in the Inspector

bounds: (0@0) corner: (50@40)

Here's an example hierarchy of morphs:

  • Window
    • Title Bar
    • TextArea
    • Buttons
      • Text
|mymorph|
mymorph color. "Returns the current color of the window (default: blue)"
mymorph color: Color green.

"more ways to set color"
mymorph color: (Color fromRgbTriplet: #(0.7, 0.5, 0.1)).
mymorph color: yellow darker. 

"adjust the size of the morph to 400x400 pixels"
mymorph extent: 400@400.

mymorph := Morph openInWindow.

A cool subclass of morph is AthensFlakeDemo.

We can also access the window of mymorph and edit the title bar text.

mymorph window setLabel: 'my morph window'.

Let's make a circle inside mymorph.

|mymorph|
mymorph color: yellow darker. 
mymorph extent: 400@400.

mymorph addMorph: (CircleMorph new bounds:
    (mymorph bounds insetBy: 10)).
mymorph := Morph openInWindow.
mymorph window setLabel: 'my morph window'.

Quick Note about "loops" in Smalltalk

do:

#(1 2 3 9 20) do: [:val |
    Transcript show: val; cr.]

withIndexDo:

#(1 2 3 9 20) withIndexDo: [:c :val |
    Transcript show: c; show ','; show: val; cr.]

It's probably not useful to work with morphs just in the Playground so let's make a subclass of morph

Morph subclass: #MyTest
    instance

We can change how a morph is displayed by overwriting the drawOn method:

drawOn: aCanvas
    |colors|
    super drawOn: aCanvas.
    colors := Color wheel: 7. "returns an array of 7 colors"
    colors withIndexDo: [:c :i |
        aCanvas fillOval: (self bounds insetBy: 4 * i) color: c.
    ]

Let's make sure that the size of MyTest is bigger by default

initialize
    super initialize.
    self extend 400@400.

Let's make it respond to mouse clicks. First we need to

handlesMouseDown: anEvent
    ^true.

For now, let's send a message to the user that their click was received

mouseDown: anEvent
    self inform: 'You clicked me at ', anEvent position printString.

For Homework:

You'll be making a class called Bouncy

The step method is called every 50ms automatically and defines how everything on the page changes

Tries

Tries are a data structure for storing sequences

Note: The "terminal" nodes of a Trie are usually drawn with a double circle

Here's an example of a Trie with the following words in it:

  • Foobar
  • Fool
  • Foob

Trie

Memory Management

Regions of Memory

  • OS Kernel Space
  • Stack ⬇️
  • Heap ⬆️
  • BSS
  • Data
  • Text

Text, BSS, and Data memory are reserved when a program starts

Typically the stack grows downward and the heap grow upward.

// allocates 3 bytes
char *p1 = malloc(3);

char *p2 = malloc(1);
char *p3 = malloc(4);

// the memory no longer belongs to me
free(p2);

// This will look for the first contiguous region of 6 btyes
char *p4 = malloc(6);

free(p3);
char *p5 = malloc(2);
free(p1);
free(p4);
free(p5);

Dangling Pointer

A pointer that does not point to memory you own or something you should no longer be accessing.

Heap Fragmentation

There will be holes in your memory after things are deallocated. This is partially because malloc will find the first fit in memory for amount of bytes it needs.

Memory Leak

Memory that is no longer needed is not released

Double Free

Calling free twice on the same region of memory leads to unexpected behavior

Why don't all languages use Garbage Collectors?

  • Speed
  • Linguistic Attributes
int *foo = new int;
int foo2 = (int) foo;
- C++ has to decide how to convert a memory address into an integer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment