April 26, 2024, 05:46:30 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


How variables are stored in Memory

Started by billhsln, September 23, 2017, 01:48:29 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

billhsln

September 23, 2017, 01:48:29 PM Last Edit: September 23, 2017, 08:17:20 PM by billhsln
If you have 2 ISTRING variables defined as var1[10], var2[10].  How much space is used in memory.  Would it be only a total of 20 chars for both or is there a default size for an ISTRING so that no matter how much you want, you get a default size, like I am asking for 10 but the default actually is 32, so my 2 variables end up taking 64 chars of space in memory.  Thinking kind of like how storage on the HD works.  A single byte file takes 1K of storage or more depending on the size of the HD.

Thanks,
Bill

Tried:

OPENCONSOLE

DEF i:INT
DEF v1[10],v2[10],v3[10]:ISTRING
DEF p1,p2,p3,p4:POINTER

p1=v1
p2=v2
p3=v3
p4=i

PRINT p1, p2, p3, p4

PRINT "Done"
INPUT i
CLOSECONSOLE
END


Changed values from 10, 20, 30, 40 and 50.  It seems that it is in multiples of 4.  10 is 12, 20 is 20, 30 is 32, 40 is 40 and 50 is 52.  So, I think I found my own answer.
When all else fails, get a bigger hammer.

Andy

Bill,

Don't forget ISTRINGS start at index 0 (zero), so if you declare an ISTRING as:

istring v1[10], you actually have space for 11 characters.

If space is an issue, use pointers instead - you can always increase them as you need to.

Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

LarryMc

You were blowing my mind with "10 is 12, 20 is 20, 30 is 32, 40 is 40 and 50 is 52" this as a length??????? until it dawned on me what you were doing and where you were going wrong.

On 32 bit processors you can think of a memory address as a UINT type which is 4 bytes.
A POINTER is the number or the numeric value of a memory address; again in 4 bytes.
The POINTER is always a 4 byte value that points to a variable REGARDLESS of the variable type or the size of the variable.

Your first mistake was to print the pointer values (which were only the 4 byte addresses of the variables) and assume the variables were stored in memory in the same order that you subtracted them to find the length.  You don't ever know where the compiler and linker are going to put things.
Now, here is the first "duh" moment.  You know what size the ISTRINGS are and how much memory they take up because you declared them. If you declare it as 10,50 or 10,000 then that is how much memory it takes up.  The only thing you have to remember is that the arrays are indexed from 0 to 1 less than what you sized them like what Andy said.
Now if you want to know how long a string is in an ISTRING during run time that's a different thing.
Say you have
DEF V1[500]:ISTRING
and somewhere in your program you want to make sure you don't add sometthing to V1 and overfill it
so you need to see how full V1 is; all you have to do is
X=Len(V1)

Now different HD have different ways of storing data on HDs.  What I was talking about applies to memory except for a few TYPE/ENDTYPE structures that are memory aligned and have a 2.4. or 8 after the structure name I've only come across a couple of them.


Hope that helps
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Brian

The Master speaks! Great explanation, Larry

Brian

billhsln

Close to what I am looking for but not 100%.  I am wondering if you define an ISTRING as v1[10], you say it takes 11 bytes, is the 12th byte used?  I would guess that the answer is no due to the 4 byte boundary on a 32bit machine.  Would that mean that on a 64bit machine it would take 8 bytes, so bytes 13-16 are not used?

Bill
When all else fails, get a bigger hammer.

LarryMc

My machine is a 64-bit machine.
I played with your program a little and can offer you a little more insight  for you to see what is going on.
You have this line in your program:
DEF v1[10],v2[10],v3[10]:ISTRING
I suggest you start increasing just the size of just v2
I say v2 because if you notice the memory address show you that the assignments have been made in the reverse order from you defining the variables.
So by increasing the length of v2 we can watch and see what it does to v1 without being also impacted by simultaneous changes to v3 and i.
I started with all of them 1 and got the following memory addresses
v2[1]
4259884  4259880 42598876 4259856
v2[2]-v2[4]
4259884  4259880 42598872 4259856
v2[5]-v2[8]
4259892  4259880 42598872 4259856

From the above you can see that memory is being allocated in 4byte chunks for ISTRINGS
Now, your other question was is all 4 bytes being used each time.
Yes and No.
If you use a normal string command like  PRINT then no
For example
DEF v1[4]:ISTRING
v1="Larr"
PRINT v1
gives
Lar
that's because a NULL is placed at the end of the string or at the end element of of the definition, whichever is less.
So, with with a DEF v1[4] remember  that's v[0]-v1[3] then v[3] will have a  null when you use a PRINT statement.
So anything after what you DEF doesn't matter because you can't get to it because of the NULL character.
That NULL character always acts as a safety device to keep you from overwriting memory WHEN you are using STRING type commands
------------------------------
Now when you're NOT using normal STRING type commands and you're reading/writing memory it's a whole new ballgame.
See the section in the Usersguide on Language/Pointers and Typecasting
This is where you can read and write to the same memory address as different memory types of different memory  sizes.
Doing this you always run the risk of over writing memory and corrupting it and causing crashes that can be extremely hard to find at times..
Then there are the commands where you simply read and write to memory directly.  There you're simply on your on with no limits on what you do.

Now, does that help a little more?  If it don't you're SOL  LOL! I'm tired of typing.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

billhsln

Thanks Larry.  That does give the answer I was looking for.  It seems that 4 is the answer I was looking for even on 64 bit machines.

Bill
When all else fails, get a bigger hammer.