Reply to topic  [ 3 posts ] 
 Variables: Pointers and Allocated Memory 
Author Message
Level 38
Level 38
User avatar

Cash on hand:
435.45

Bank:
2,750,364.30
Posts: 10364
Joined: Sun Oct 26, 2008 5:47 am
Group: Dev Team
 Variables: Pointers and Allocated Memory
Greetings.
Today's tutorial does not teach you any new cool features or anything. It does not focus on any particular version of action script either, or any particular language for that matter. Also, this is pretty high level, so I wont get into too many details. I want you all to get a basic view of what is happening when you create a variable.

Today I am going to teach you what the inner workings of variables really are.

What is a variable?
To explain this I am going to have to go down a few levels in the system.

When you create a variable, what happens is you open a spot in the memory with a particular size.
The memory is basically a long array of bytes. like this

[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

let this represent the our memory for now.
When you create a variable, you or the compiler needs to specify a type for the variable. Is it a number, a string, an array, a class?
let us say our variable is a number, a 4 bytes long integer. our variable will need to allocate 4 bytes of the memory, and that it does.

[x][x][x][x][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]


the spots marked x have been allocated for this variable. the spots are still empty (or rather, still has it's old values left from whoever used it before us)
We're basically marking this spot so that no other program starts using it, for that would be catastrophic.







so, looking at our memory in terms of content, it basically looks like this right now

[0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0]

(actually, I'm pretty sure it'd be full of a bunch of data, but to make it more noticeable of what parts we're actually using, We're going to pretend every other spot in the memory has never been used before.
I'm also sure there is a bunch of header data when creating a number in most languages, but let us ignore that for now.)

Let us say we want the variable to store the number 4354354 (ignoring any header data that also needs to be stored). That number is converted to bytes. A byte can store 256, so if the number is over 256, then another byte is required. The number variable we created already specified that it is 4 bytes long, so that's how much we're going to use. When a number exceeds 255, we add 1 to the second byte, so the number 255 would be stored like this in bytes: 0,0,0,255, and the number 256 would be stored like this: 0,0,1,0 and then we start over again, and so next time we get to 255 on the far right byte, we add another one on the second byte. In the end the number we're storing is converted to bytes and it'll look like this:

[0][66][245][229][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][0]


As you can see, only 3 of them are needed to store our number, but size of the allocated memory is decided with the datatype, and in case we increase the number, we could still need that 4th spot. But anyway, that's what happens when you create and allocate a number into memory. So that's basically raw number data storage in the memory.








When you create a variable, you also create a so called pointer.

A pointer is basically a shortcut to the memory allocated. In action script as well as languages such as C# and java, all variables you create are actually just pointers to the memory that the compiler/interpreter allocated for you when it built. (converted your code to machine code or some other mid-level code)

So when you create a variable and delete it in for example flash. You just delete a shortcut to it. The actual data is still there until the automatic garbage collection decides to de-allocate it to make room for something else.

Here is how this matters in flash, java or c#

When you create two class objects (for example movieclips)
and set one of the movieclips to be equal to the other.
Code:
var desu:Movieclip;
var dasu:Movieclip;
dasu = desu;


what actually happens isn't completely copying the data from desu to dasu.
The pointer for dasu which points to it's allocated memory address, is changed to simply point to desu's memory address instead.

BEFORE
Image
AFTER


So, in conclusion to all this...
Whenever you create a variable and point it to an object, you do not create an exacty copy of the object. You just set the variable to be a shortcut to that object
And what happens when you decide to change anything inside desu and dasu after this?
Here's what. Anything you change in desu will be changed in dasu, and anything changed in dasu will also be changed in desu.

[color=crimson]However, keep in mind that this does not apply on number datatypes (Number, integer, float, double, long) in most languages.
It does, on the other hand apply on Strings, arrays, and any class object.







Okay, I said I wouldn't teach you any cool features or anything, but eh, what the heck.


Isn't it annoying that when you're performing a hitTest or need any kind of access to a movieclip several levels down other movieclips?
I'm talking for example, _root.level1.map.aliveObjects.player.feet
this is especially annoying when you just decided that the player actually needs to be moved to _root.dynamicObjects
You'd have to rewrite all the 90000 lines where you go _root.level1.map.aliveObjects.player.feet.hitTest(this)
to _root.dynamicObjects.player.feet.hitTest(this)
and that's a major pain in the fucking ass!

So what do you do?
You create a variable in root to represent your player instead!

var playerP:MovieClip;

Now, inside your player movieclip or on it's Load function, do this:
_root.playerP = this;

and voile!
you now have a global pointer to your player and you can always access it from anywhere using _root.playerP, even if you have moved your player into another movieclip.
also notice that it might not be a good idea to give them the same name. Especially if they're both in the same movieclip or in root at the same time. That's why I called the pointer playerP, but it's all up to you what you call your variables.

_________________
My Pixiv
Image
Spoiler: show
OLD VERSION, BITCHES!
Image


Sun Dec 16, 2012 10:24 pm
Profile E-mail
ℱᒪ૪ᓰﬡᘐ ᖘ⋒ᖇᖰᒪᙓ ᖘᙓﬡᓮᔕ
ℱᒪ૪ᓰﬡᘐ ᖘ⋒ᖇᖰᒪᙓ ᖘᙓﬡᓮᔕ
User avatar

Cash on hand:
258,935,827.73

Bank:
7,777,777.77
Posts: 19745
Joined: Fri Mar 04, 2011 9:57 pm
Location: ЇИ УОЦЯ MЇЙD FЦCКЇЙG ЇT ЇЙTО ОBLЇVЇОЙ
Group: Їи$aиїту
Country: Nepal (np)
Post Re: Variables: Pointers and Allocated Memory
0gknew most of this before hand because teh pirates were using the ttrick of having one program write to anothers memory to bypass some security trigger, still, usefull, because i didnt know that you write to memory in bytes

_________________
ImageImage
Image
?
Їи$aиїту Group! | Ultimate Fh Tribute!
© 2010 -2099 Odin Anarkis. All Rights Reserved.

Quotes
Spoiler: show
Image
who149 wrote:
I'm trying i'm trying~ i'm making I'll try too slowly up my posting. At least once a day for a bit. Then I'll up that too twice, then four, then 8 and so on.
Until eventually I wake up one morning and find out that I am actually an Idiot hero.
On some quest too cheat on his gf or raise affection of 5 women who conveniently live in my the same dorm as me.
In which I only have 100 days to seduce them all.

Remon wrote:
Now we can dominate the porn industry, camera industry, AND the world!
YomToxic wrote:
YOU BETTER STAY ALIVE OR ELSE I WILL HUNT YOU DOWN AND RAPE YOU DEAD.

_________________
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
2 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.


Sun Dec 16, 2012 10:35 pm
Profile E-mail WWW
Level 38
Level 38
User avatar

Cash on hand:
435.45

Bank:
2,750,364.30
Posts: 10364
Joined: Sun Oct 26, 2008 5:47 am
Group: Dev Team
Post Re: Variables: Pointers and Allocated Memory
well, there are several levels when it comes to memory or any storage allocation. segments, blocks, holding bytes. It all depends on disk and memory format.

_________________
My Pixiv
Image
Spoiler: show
OLD VERSION, BITCHES!
Image


Sun Dec 16, 2012 10:48 pm
Profile E-mail
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 
 

Similar topics

 
In memory of Hitler
Forum: ./General Spam
Author: Zob
Replies: 11
Suntory Beer's "A Penguin's Memory - A Tale of Happiness"?!!
Forum: ./Anime & Manga
Author: RV-007
Replies: 1
Walk down memory lane.
Forum: ./General Spam
Author: Pantsman
Replies: 36
Felt like walking down memory lane...
Forum: ./General Spam
Author: Desmond
Replies: 3
Top


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Mods Database :: Imprint :: Crawler Feeds :: Reset blocks
Designed by STSoftware for PTF.

Portal XL 5.0 ~ Premod 0.3 phpBB SEO