Re: copymemory basic question

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Thanks, I take that back. I recompiled with debug info and used VC++ 6.0 to
see the assembly source and this is how VB6 translated it in this case(Copy
to Notepad to see it clearly):

64: temp = s1
00401D57 mov edx,dword ptr [s1]
00401D5A add esp,14h
00401D5D lea ecx,[temp]
00401D60 mov dword ptr [esi+3Ch],eax
00401D63 call dword ptr [__imp____vbaStrCopy (0040106c)]

66: s1 = s2
00401DE0 mov edx,dword ptr [s2]
00401DE3 add esp,24h
00401DE6 lea ecx,[s1]
00401DE9 call dword ptr [__imp____vbaStrCopy (0040106c)]

68: s2 = temp
00401E66 mov edx,dword ptr [temp]
00401E69 add esp,24h
00401E6C lea ecx,[s2]
00401E6F call dword ptr [__imp____vbaStrCopy (0040106c)]

It's calling vbaStrCopy() in the runtime library for each string assignment.


"Tony Proctor" <tony_proctor@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:%23DAL7IZrFHA.1032@xxxxxxxxxxxxxxxxxxxxxxx
>I think you might be misinterpreting your own results here. The VB
>Optimiser
> isn't doing much at all (as usual), and certainly not tracking String
> addresses. The String data is held in BSTR memory packets. These, in turn,
> reside in an OLE string pool. They may be deallocated/allocated using
> oleaut32 APIs such as SysFreeString and SysAllocString. VB simply uses
> these
> APIs. Also, VB allocates new packets each time a string is assigned from
> one
> variable to another (except in the special case mentioned by Thorsten).
> What
> you're seeing is the result of the string pool returning previously-used,
> but now free, memory addresses. There's a high chance of re-using the same
> memory addresses in your code because the various string variables hold
> the
> same value, and so will require the same amount of memory.
>
> Tony Proctor
>
> "Someone" <nobody@xxxxxxx> wrote in message
> news:oc0Re.6919$ct5.2257@xxxxxxxxxxxxx
>> On a second thought, it seems that the optimizer in VB copies the
>> pointers
>> to strings instead of swapping the contents. I didn't know that. It seems
>> that it also keeps track of the last few strings used, even if you
> assigned
>> a different value. However, I don't think that it's a good idea to rely
>> on
>> whether the optimizer can do it or not, it's best to test it with a
>> timing
>> routine to see which method is faster. Even with this optimization, it
> might
>> be slower than "Swapping arrays of Longs representing the Indexes of
>> strings", because it seems that VB uses internal memory management
> functions
>> when it does this during runtime.
>>
>>
>> Here is a test sample I did and its output:
>>
>> Private Sub Form_Click()
>> Dim s1 As String
>> Dim s2 As String
>> Dim temp As String
>>
>> s1 = "abc"
>> s2 = "def"
>> temp = ""
>>
>> ' Set some form properties so the output can be seen clearly when in
>> EXE.
>> Me.AutoRedraw = True
>> Me.FontName = "Courier"
>>
>> Debug.Print " s1", , " s2", , " temp"
>> Debug.Print "Before: "; VarPtr(s1); StrPtr(s1), VarPtr(s2);
>> StrPtr(s2), VarPtr(temp); StrPtr(temp)
>>
>> temp = s1
>> Debug.Print "After temp = s1: "; VarPtr(s1); StrPtr(s1), VarPtr(s2);
>> StrPtr(s2), VarPtr(temp); StrPtr(temp)
>> s1 = s2
>> Debug.Print "After s1 = s2: "; VarPtr(s1); StrPtr(s1), VarPtr(s2);
>> StrPtr(s2), VarPtr(temp); StrPtr(temp)
>> s2 = temp
>>
>> Debug.Print "After s2 = temp: "; VarPtr(s1); StrPtr(s1), VarPtr(s2);
>> StrPtr(s2), VarPtr(temp); StrPtr(temp)
>>
>> End Sub
>>
>>
>>
>> OUTPUT(Copy to Notepad to see it clearly):
>>
>>
>> s1 s2
> temp
>> Before: 1308792 1530892 1308788 2193116
>> 1308784 1530932
>> After temp = s1: 1308792 1530892 1308788 2193116
>> 1308784 2217996
>> After s1 = s2: 1308792 1530932 1308788 2193116
>> 1308784 2217996
>> After s2 = temp: 1308792 1530932 1308788 1530892
>> 1308784 2217996
>>
>> My environment: Windows XP with SP2, and VB6 with SP5.
>>
>>
>
>


.



Relevant Pages

  • RE: FileSearch to locate the latest (last saved) file
    ... Dim sReport As Workbook, sDashboard As Workbook ... Dim fLdr As String, Fil As String, FPath As String, x As String, _ ... FileDates= temp ... For i = 1 To NewestFile ...
    (microsoft.public.excel.programming)
  • Re: locating strings approximately
    ... > I'd like to see if a string exists, even approximately, in another. ... > have looked at edit distance, but that isn't a good choice for finding ... if temp < dist: dist = temp ... dprev, dcurr = dcurr, dprev ...
    (comp.lang.python)
  • Re: Unmanaged code(dll) function: int myfunc (char* temp)
    ... //Assigning an value to temp ... I am using char* not TCHAR* ... Can you help me how to get the value of temp when using string builder ... int myfunc ...
    (microsoft.public.dotnet.framework.compactframework)
  • Help with a program
    ... program.cpp: see declaration of 'movie' ... string title; ... void getline ... {int temp; ...
    (alt.comp.lang.learn.c-cpp)
  • Re: How Globals is handle in C.
    ... > "Hello World" string will be stored in the Initialized data segment. ... I think> temp is stored in stack if you have not declared it as global. ... The C language says nothing about an "initialized data segment" or "read only memory". ...
    (comp.lang.c)