Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,205,413 members, 7,992,372 topics. Date: Sunday, 03 November 2024 at 02:17 AM

Storage Bindings Of Variables - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Storage Bindings Of Variables (5431 Views)

Naming Variables / Variables In Programming / Learn Javascript Variables - For Pidgin (2) (3) (4)

(1) (2) (Reply) (Go Down)

Storage Bindings Of Variables by Javanian: 3:07pm On Sep 04, 2016
Hi,

This post is meant to describe variables into four different categories based on their Storage Bindings. The Binding categories will be

- Static Variables
- Stack Dynamic variables
- Explicit Heap Dynamic variables
- Implicit Heap Dynamic Variables

I will describe each category in detail, list its strengths and weaknesses and implementation in languages like Java, Python, C, C++ and Javascript.

http://solomonubani..com.ng/2016/09/storage-bindings-for-variables.html
http://solomonubani.com/2016/09/storage-bindings-for-variables.html

2 Likes

Re: Storage Bindings Of Variables by Javanian: 3:18pm On Sep 04, 2016
Static Variables

Static variables are bound to memory cells sometime before execution and remain bound to the same memory cells until program execution terminates. This means values are assigned to the memory cells of a variable before execution of a program begins. Global variables are usually static variables although a lot of languages allow subprograms to have static variables called "local static variables". Class variables in a lot of language implementations are usually static variables. Class Variables are created sometime before program execution begins.

Strengths

-Static Variables are fast. They can be addressed directly unlike other variables that require indirect addressing which is usually slower.
-Static Variables are history sensitive. They always remember.

Weaknesses

Static variables reduce programming flexibility. They do not support recursion.

In Java


class StaticVariableEg
{
static int count=0; //count is a static variable
public void increment()
{
count++;
}
public static void main(String args[])
{
StaticVariableEg ob=new StaticVariableEg();
ob.increment();
System.out.println(ob.count);
}
}


The Implementation of static class variables in Java is similar to that of C# and C++.

C and C++ have a static specifier that can be used in variable definitions in a function to make the variable static.

http://solomonubani..com.ng/2016/09/storage-bindings-for-variables.html
http://solomonubani.com/2016/09/storage-bindings-for-variables.html
Re: Storage Bindings Of Variables by Javanian: 3:43pm On Sep 04, 2016
Stack Dynamic Variables

In stack dynamic variables, storage bindings are created during elaboration of a variable, Not when execution starts but when execution gets to the variable(elaboration). Stack Dynamic variables are created at run time when execution has begun. Like static variables deallocation cannot be done until executions is complete. In stack dynamic variables both allocation and dealloaction is done from the run time stack

Strengths

Stack Dynamic variables are more flexible than static variables. They can be used in recursive subprograms.

Weaknesses

Stack Dynamic variables are not history sensitive
Stack Dynamic variables are slower than static variables because of the incurred overhead in allocation and deallocation of variables and aslo in requires indirect addressing.

In Java, C++ and C#, variables defined in methods are by default Stack dynamic


class StackDynamicEg
{
void counter()
{
int count = 0; //Stack Dynamic variable
}
}


http://solomonubani..com.ng/2016/09/storage-bindings-for-variables.html
http://solomonubani.com/2016/09/storage-bindings-for-variables.html
Re: Storage Bindings Of Variables by Javanian: 4:01pm On Sep 04, 2016
Explicit Heap Dynamic Variables

In Explicit Heap Dynamic variables allocation and deallocation are done at run time by explicit instructions written by the programmer. Allocation and Deallocation of explicit Heap Dynamic variables is done from the heap. They are created with Pointers and Reference variables.

Strengths

They are very memory prudent.
They can be used to create dynamic data structures like Linked Lists and Trees that can grow and shrink during execution

Weaknesses

-They require pointers which are very complex to implement
-They are slower than static variables becasue of the incured overhead in allocation and deallocation of variables

In C++


int *intnode; // Create a pointer
intnode = new int; // Create the explicit heap-dynamic variable

delete intnode; // Deallocate the heap-dynamic variable


In Java all objects are explicit heap dynamic variables and are accessed through reference variables. In contrast to C++, Java does not allow explicit deallocation of variables, deallocation is done implicitly by the garbage collector.

http://solomonubani..com.ng/2016/09/storage-bindings-for-variables.html
http://solomonubani.com/2016/09/storage-bindings-for-variables.html
Re: Storage Bindings Of Variables by Javanian: 4:13pm On Sep 04, 2016
Implicit Heap Dynamic Variables

In Implicit Heap Dynamic Variables, allocation and deallocation are done only when the variable is assigned a value at runtime. Allocation and deallocation are done at runtime from the Heap.

Strengths

-They are very flexible
-They make optimum use of memory

Weaknesses

-They are very slow
-They are not history sensitive

In Python


#!/usr/bin/python
str = ["Java", "Python", "C++", "Delphi", "Fortran"] #implicit heap dynamic variable 'str' is created at runtime
print str
str = 5 #implicit heap dynamic variable which was previously a list of length 5 is now an integer
print str


http://solomonubani..com.ng/2016/09/storage-bindings-for-variables.html
http://solomonubani.com/2016/09/storage-bindings-for-variables.html
Re: Storage Bindings Of Variables by Javanian: 4:22pm On Sep 04, 2016
Please this topic is open for discussion. Let us talk more on implementation in other languages I didn't mention or challenges encountered in implementation. I recommend Concepts of Programming Languages (10th Edition) 10th Edition by Robert W. Sebesta to anyone who wants elaborate explanation of the topic. It is a really great book.

http://solomonubani..com.ng/2016/09/storage-bindings-for-variables.html
http://solomonubani.com/2016/09/storage-bindings-for-variables.html
Re: Storage Bindings Of Variables by danvery2k6(m): 4:24pm On Sep 04, 2016
Finally, someone has decided to come back from recess. Nice articles.

1 Like

Re: Storage Bindings Of Variables by Javanian: 4:47pm On Sep 04, 2016
Re: Storage Bindings Of Variables by Javanian: 4:50pm On Sep 04, 2016
danvery2k6:
Finally, someone has decided to come back from recess. Nice articles.

Many Thanks.
Re: Storage Bindings Of Variables by Javanian: 10:53am On Sep 05, 2016
So? No Takers? I don't want to believe this thread is very esoteric.

1 Like

Re: Storage Bindings Of Variables by danvery2k6(m): 11:16am On Sep 05, 2016
Javanian:
Explicit Heap Dynamic Variables


Strengths

They are very memory prudent.

On this, I do understand that the heap does not have size restrictions on variable size, apart from the physical limitations of your computer. Heap memory is also slightly slower to be read from and written to because one has to use pointers to access memory on the heap.

So when you say that Heap Dynamic Variables are memory prudent, what does that really mean? Or better still, how/why are they considered "Memory prudent"?
Re: Storage Bindings Of Variables by Javanian: 12:30pm On Sep 05, 2016
danvery2k6:


On this, I do understand that the heap does not have size restrictions on variable size, apart from the physical limitations of your computer. Heap memory is also slightly slower to be read from and written to because one has to use pointers to access memory on the heap.

You are very right.


So when you say that Heap Dynamic Variables are memory prudent, what does that really mean? Or better still, how/why are they considered "Memory prudent"?

Speed and Memory efficiency are actually two different things. So you are right Heap Dynamic variables both explicit and implicit are slow relative to static and stack dynamic variables. They are memory prudent because the value of a memory slot can be changed at runtime when they are no more needed. Those memory slots can then be replaced with values that are then needed. The example I gave in Python:



#!/usr/bin/python
str = ["Java", "Python", "C++", "Delphi", "Fortran"] #implicit heap dynamic variable 'str' is created at runtime
print str
str = 5 #implicit heap dynamic variable which was previously a list of length 5 is now an integer
print str


The variable str holds a list of 5 strings. In Python 2.7 a string is represented by 32 bits in memory. So the list holds 5 strings which would be 160 bits in memory. As execution continues str now holds an integer with value of 5. An int in Python 2.7 is just 12 bits. So the variable str would have saved a 160-12 bits of memory by being able to do this. This is not the case in static variables and stack dynamic variables. So basically heap dynamic variables sacrifice speed for memory prudence and writability.

2 Likes

Re: Storage Bindings Of Variables by CodeHouse: 12:53pm On Sep 05, 2016
Javanian:
So? No Takers? I don't want to believe this thread is very esoteric.

I guess people have the same book you read and make more use of the internet these days, this forum hasn't been "moderator driven" for years, most people are getting their facts from whatsapp group these days...nice to know you are back, right?
Re: Storage Bindings Of Variables by Javanian: 1:06pm On Sep 05, 2016
CodeHouse:


I guess people have the same book you read and make more use of the internet these days, this forum hasn't been "moderator driven" for years, most people are getting their facts from whatsapp group these days...nice to know you are back, right?

Yeah, I will try to be more active.

1 Like

Re: Storage Bindings Of Variables by danvery2k6(m): 2:02pm On Sep 05, 2016
Javanian:

http://solomonubani..com.ng/2016/09/storage-bindings-for-variables.html

I've been trying to understand why you did the things you did in this image. Maybe a little more context to the image might help us understand better. Why do some lines run from the beginning of execution to the end while some start from somewhere in the middle? Is that trying to tell us how long the variables are available in the execution context or am I getting it wrong?
Re: Storage Bindings Of Variables by Javanian: 2:11pm On Sep 05, 2016
danvery2k6:


I've been trying to understand why you did the things you did in this image. Maybe a little more context to the image might help us understand better. Why do some lines run from the beginning of execution to the end while some start from somewhere in the middle? Is that trying to tell us how long the variables are available in the execution context or am I getting it wrong?

Yes, you are right. The image is simply an anecdote of the various storage bindings. Static variables are bound to memory sometime before execution begins and are not deallocated until after execution. Stack dynamic variables are bound during elaboration of the variable (when execution gets to it) and are not unbounded until execution is complete while Heap dynamic variables can be allocated and deallocated as often as possible during execution.

1 Like

Re: Storage Bindings Of Variables by danvery2k6(m): 2:36pm On Sep 05, 2016
Javanian:
Implicit Heap Dynamic Variables

In Implicit Heap Dynamic Variables, allocation and deallocation are done only when the variable is assigned a value at runtime. Allocation and deallocation are done at runtime from the Heap.

Strengths

-They are very flexible
-They make optimum use of memory

Weaknesses

-They are very slow
-They are not history sensitive

In Python


#!/usr/bin/python
str = ["Java", "Python", "C++", "Delphi", "Fortran"] #implicit heap dynamic variable 'str' is created at runtime
print str
str = 5 #implicit heap dynamic variable which was previously a list of length 5 is now an integer
print str


http://solomonubani..com.ng/2016/09/storage-bindings-for-variables.html

I'm guessing one weakness here will be a probable loss of error detection capabilities by the compiler or interpreter?
Re: Storage Bindings Of Variables by Javanian: 2:47pm On Sep 05, 2016
danvery2k6:


I'm guessing one weakness here will be a probable loss of error detection capabilities by the compiler or interpreter?

+10

A weakness of most weakly typed languages, Sacrificing Reliability and Readability for Writability.

1 Like

Re: Storage Bindings Of Variables by CodeHouse: 4:06pm On Sep 05, 2016
Javanian:


Yeah, I will try to be more active.

glad..
Re: Storage Bindings Of Variables by Javanian: 4:28pm On Sep 07, 2016
In C


char *x;

x = "Seun Osewa";
x = "Seun";


I just saw this interesting example online where the variable x is said to be implicitly heap dynamic probably because the value of x changes at runtime. I never thought until now that a strongly type language could be "implicitly" heap dynamic though it can be argued that x is explicitly a String.
Re: Storage Bindings Of Variables by danvery2k6(m): 1:51pm On Sep 08, 2016
If we are to go by the definition of Implicit Heap-dynamic Variables, which says "Variables for which storage is not allocated until the variable is assigned to". Would we still consider pointers in C to be Implicit Heap-dynamic variables?

I also think that pointers in C don't change storage location, but simply values stored in the location, which seems to violate the second definition point of Implicit Heap-dynamic variables: " the storage associated with the variable can change with every assignment to it".

How is it possible that the above shows an example of Implicit Heap-dynamic variable?
Re: Storage Bindings Of Variables by Javanian: 3:29pm On Sep 08, 2016
danvery2k6:
If we are to go by the definition of Implicit Heap-dynamic Variables, which says "Variables for which storage is not allocated until the variable is assigned to". Would we still consider pointers in C to be Implicit Heap-dynamic variables?

I also think that pointers in C don't change storage location, but simply values stored in the location, which seems to violate the second definition point of Implicit Heap-dynamic variables: " the storage associated with the variable can change with every assignment to it".

How is it possible that the above shows an example of Implicit Heap-dynamic variable?


I am also very skeptical about that example, it breaks too many rules as you have rightly stated. I just came across the example here

1 Like

Re: Storage Bindings Of Variables by ugwum007(m): 8:34am On Sep 27, 2016
Javanian:
Static Variables



In Java


class StaticVariableEg
{
static int count=0; //count is a static variable
public void increment()
{
count++;
}
public static void main(String args[])
{
StaticVariableEg ob=new StaticVariableEg();
ob.increment();
System.out.println(obj.count);
}
}



Are you sure that expression will print anything?
Re: Storage Bindings Of Variables by ugwum007(m): 8:40am On Sep 27, 2016
Javanian:
Stack Dynamic Variables


class StackDynamicEg
{
void counter()
{
int count = 0; //Stack Dynamic variable
}
}



brother Javanian, I thought variables created in a method is destroyed after the method is executed.

what of these:

int counter(){

int count = 0;

return count;
}

I am still a learner, please clarify me.
Re: Storage Bindings Of Variables by Javanian: 9:47am On Sep 27, 2016
ugwum007:

Are you sure that expression will print anything?
Typo. Thanks for bringing it to my attention.

1 Like

Re: Storage Bindings Of Variables by Javanian: 9:51am On Sep 27, 2016
ugwum007:


brother Javanian, I thought variables created in a method is destroyed after the method is executed.

what of these:

int counter(){

int count = 0;

return count;
}

I am still a learner, please clarify me.

A method is a subprogram. Technically, Subprograms are Programs.
Re: Storage Bindings Of Variables by ugwum007(m): 9:57am On Sep 27, 2016
Javanian:


A method is a subprogram. Technically, Subprograms are Programs.

I see. u be bros you are really good like they say.

1 Like

Re: Storage Bindings Of Variables by Javanian: 11:15pm On Aug 11, 2017
bump tongue
Re: Storage Bindings Of Variables by melodyogonna(m): 10:50pm On Aug 12, 2017
do u mean to revive this thread?? oga javanian
Re: Storage Bindings Of Variables by Javanian: 11:29am On Aug 13, 2017
melodyogonna:
do u mean to revive this thread?? oga javanian

Kinda. I was thinking of creating another one similar to this, just looking for some motivation and I don't know if anyone will be interested. I'm not an oga o tongue
Re: Storage Bindings Of Variables by melodyogonna(m): 1:12pm On Aug 13, 2017
Javanian:


Kinda. I was thinking of creating another one similar to this, just looking for some motivation and I don't know if anyone will be interested. I'm not an oga o tongue
do you know i started learning programming from your thread, last year, just when i was searching for the best language to learn, i found your thread - so you are oga javanian **winks**.
Your thread helped me
Re: Storage Bindings Of Variables by Javanian: 2:23pm On Aug 13, 2017
melodyogonna:
do you know i started learning programming from your thread, last year, just when i was searching for the best language to learn, i found your thread - so you are oga javanian **winks**.
Your thread helped me

Glad to hear that thread has been useful to someone cheesy

1 Like

(1) (2) (Reply)

Online Shopping System In Php Documentation Help / Macbook Pro Is Overrated. / How To Transfer Ownership If Buy A Used Autel Scanner?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 59
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.