Results 1 to 3 of 3

Thread: Type Variables and memory organization?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    Type Variables and memory organization?

    How is PC memory organized? It is accessed byte by byte or are there larger access units?

    I have an application with an array of user type variables. I noticed that when a user Type variable is written to a file, all the subordinate variables are packed with no filler. How do these subordinate variables appear in memory?

    On a mainframe, the order of the variables in an input/output record affects the amount of memory required. Does it have an effect for a VB program?

    Suppose I have Type variable with a subordinate 9-character fixed string and some subordinate Doubles. If The string is first, is there unused space between it and the first double? Will defining the subordinate doubles first save some memory space?
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

  2. #2
    Fanatic Member JPicasso's Avatar
    Join Date
    Aug 2001
    Location
    Kalamazoo, MI
    Posts
    843
    You can currently purchase 1Gig of RAM for under $200 (US).

    I am not dismissing your question, but I don't believe Visual basic has that kind of
    control over how items are loaded. The VBcompiler smashes data as close as it can.
    I do not belive there is any one-to-one way to know what values are stored in memory vs how an output
    file is ordered.

    I would think VB has many other performance pitfalls than re-ordering your type.
    Merry Christmas

  3. #3
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    I have always had to be aware of alignment on every system that I have ever worked on. Mostly because of the fact that the data is being passed between apps that were coded in different languages. Some languages like COBOL and VB do not make it apparent that they are aligning the variables in a structure to the correct alignment.

    I put the following code in an empty form
    VB Code:
    1. Private Type dclTest
    2.              strText As String * 9
    3.              dblOne  As Double
    4.              dblTwo  As Double
    5.         End Type
    6. Private Declare Sub CopyMemory Lib "KERNEL32" _
    7.                     Alias "RtlMoveMemory" (hpvDest As Any, _
    8.                                            hpvSource As Any, _
    9.                                            ByVal cbCopy As Long)
    10.  
    11. Private Sub Form_Load()
    12.   Dim testUDT As dclTest
    13.   Dim b       As Byte
    14.   Dim i       As Integer
    15.  
    16.   With testUDT
    17.        .strText = "str len 9"
    18.        .dblOne = 200
    19.        .dblTwo = 5.5
    20.   End With
    21.   Debug.Print "UDT is " & LenB(testUDT) & " bytes long"
    22.   For i = 0 To LenB(testUDT) - 1
    23.       CopyMemory b, ByVal VarPtr(testUDT) + i, 1
    24.       Debug.Print b;
    25.   Next i
    26.  
    27. End Sub
    and got the following output:

    UDT is 36 bytes long
    115 0 116 0 114 0 32 0 108 0 101 0 110 0 32 0 57 0 0 0 0 0 0 0 0 0 105 64 0 0 0 0 0 0 22 64


    This would indicate that 2 pad bytes were inserted after the String to properly align the Doubles.
    strText = 18 bytes
    padding = 2 bytes
    dblOne = 8 bytes
    dblTwo = 8 bytes

    This is a rather round-about way of getting the individual bytes, but I'm not the most knowledgable VB programmer and don't know if there is a better way to do it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width