Hypothetical question

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
OK, I was playing around with Visual Basic and found a function called VarPtr, which returns the address of a variable (as far as I understand it). So I thought I could use this to determine the length of variables like the following code. My question is, could this method of determination ever fail, could these variables ever not be right next to each other in system memory? For example, for a 16-bit integer, if I declare a and b, would a's address ever be any more than 2 from b's address? Perhaps a more specific question is, could another program have its time slice between the declaration of two variables in my program? If a was in system memory and b was in the paging file, would the addresses vary significantly, or is paging implemented so low level as for this not to occur? Does memory ever get rearranged (thus causing a change in address)?
 

Thyme

Platinum Member
Nov 30, 2000
2,330
0
0
I'm not too familiar with how VB handles memory, so this could not be relevent, but generally, there are two different areas of memory: computer managed and user managed. If you declare a variable, it's contents are put somewhere in computer managed memory. If you declare a pointer and allocate it, it puts it in user managed area. If you use a function to get a pointer of a computer-managed variable, you can't really guarentee anything as the system can do whatever it feels is best.
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Originally posted by: Thyme
I'm not too familiar with how VB handles memory, so this could not be relevent, but generally, there are two different areas of memory: computer managed and user managed. If you declare a variable, it's contents are put somewhere in computer managed memory. If you declare a pointer and allocate it, it puts it in user managed area. If you use a function to get a pointer of a computer-managed variable, you can't really guarentee anything as the system can do whatever it feels is best.

Thanks for the input. So in other words this is by no means a reliable method to get sizes of variables? I don't think I'd ever do it, but it was one of those boring theoretical questions. I suppose I should just use something like VB's LenB() instead.

One interesting thing I observed was that the later-declared variable b had an address before that of a's. So if (int16) b had an address of 6, a had an address of 8, and I found consistent results with this. This means they are stored in Big-Endian order in memory? Or does Big-Endian signify the order of variable data and not the order of addressing? (Wrong term to use at this level?)

What I tried is looping through dimensioning variables and returning addresses, and ending the program if the difference was more than 2 (in case of int16). I looped through it a 1000000 and my program never ended.

By using this code, I also determined that VB's byte is actually 2 bytes. Does that sound right?