rayskelton
asked on
Allocating static buffer in processing loop
Are there any pitfalls to allocating memory in a processing loop. I am not a fan of this concept, but is there a problem doing this? Exactly what is occuring by doing this? What will be gained, or lost by allocating outside the loop and clearing the beffer before each use?
for(ll->rewind(); ll->data() ; (*ll)++ )
{
char temp[80];
sprintf(temp, (char *)" %s", (char *)ll->data());
msg_list.append(ll_strdup( temp));
}
for(ll->rewind(); ll->data() ; (*ll)++ )
{
char temp[80];
sprintf(temp, (char *)" %s", (char *)ll->data());
msg_list.append(ll_strdup(
}
The buffer is allocated in stack as usual.
void func1(void)
{
char buff[80];
for(int i=1; i<3; i++)
{
buff[i] = i;
}
}
void func2(void)
{
for(int i=1; i<3; i++)
{
char buff[80];
buff[i] = i;
}
}
484: void func1(void)
485: {
00402050 55 push ebp
00402051 8B EC mov ebp,esp
00402053 81 EC 94 00 00 00 sub esp,94h
00402059 53 push ebx
0040205A 56 push esi
0040205B 57 push edi
0040205C 8D BD 6C FF FF FF lea edi,[ebp-94h]
00402062 B9 25 00 00 00 mov ecx,25h
00402067 B8 CC CC CC CC mov eax,0CCCCCCCCh
0040206C F3 AB rep stos dword ptr [edi]
486: char buff[80];
487:
488: for(int i=1; i<3; i++)
0040206E C7 45 AC 01 00 00 00 mov dword ptr [ebp-54h],1
00402075 EB 09 jmp func1+30h (00402080)
00402077 8B 45 AC mov eax,dword ptr [ebp-54h]
0040207A 83 C0 01 add eax,1
0040207D 89 45 AC mov dword ptr [ebp-54h],eax
00402080 83 7D AC 03 cmp dword ptr [ebp-54h],3
00402084 7D 0C jge func1+42h (00402092)
489: {
490: buff[i] = i;
00402086 8B 4D AC mov ecx,dword ptr [ebp-54h]
00402089 8A 55 AC mov dl,byte ptr [ebp-54h]
0040208C 88 54 0D B0 mov byte ptr [ebp+ecx-50h],dl
491: }
00402090 EB E5 jmp func1+27h (00402077)
492: }
494: void func2(void)
495: {
00401520 55 push ebp
00401521 8B EC mov ebp,esp
00401523 81 EC 94 00 00 00 sub esp,94h
00401529 53 push ebx
0040152A 56 push esi
0040152B 57 push edi
0040152C 8D BD 6C FF FF FF lea edi,[ebp-94h]
00401532 B9 25 00 00 00 mov ecx,25h
00401537 B8 CC CC CC CC mov eax,0CCCCCCCCh
0040153C F3 AB rep stos dword ptr [edi]
496: for(int i=1; i<3; i++)
0040153E C7 45 FC 01 00 00 00 mov dword ptr [ebp-4],1
00401545 EB 09 jmp func2+30h (00401550)
00401547 8B 45 FC mov eax,dword ptr [ebp-4]
0040154A 83 C0 01 add eax,1
0040154D 89 45 FC mov dword ptr [ebp-4],eax
00401550 83 7D FC 03 cmp dword ptr [ebp-4],3
00401554 7D 0C jge func2+42h (00401562)
497: {
498: char buff[80];
499: buff[i] = i;
00401556 8B 4D FC mov ecx,dword ptr [ebp-4]
00401559 8A 55 FC mov dl,byte ptr [ebp-4]
0040155C 88 54 0D AC mov byte ptr buff[ecx],dl
500: }
00401560 EB E5 jmp func2+27h (00401547)
501: }
{
char buff[80];
for(int i=1; i<3; i++)
{
buff[i] = i;
}
}
void func2(void)
{
for(int i=1; i<3; i++)
{
char buff[80];
buff[i] = i;
}
}
484: void func1(void)
485: {
00402050 55 push ebp
00402051 8B EC mov ebp,esp
00402053 81 EC 94 00 00 00 sub esp,94h
00402059 53 push ebx
0040205A 56 push esi
0040205B 57 push edi
0040205C 8D BD 6C FF FF FF lea edi,[ebp-94h]
00402062 B9 25 00 00 00 mov ecx,25h
00402067 B8 CC CC CC CC mov eax,0CCCCCCCCh
0040206C F3 AB rep stos dword ptr [edi]
486: char buff[80];
487:
488: for(int i=1; i<3; i++)
0040206E C7 45 AC 01 00 00 00 mov dword ptr [ebp-54h],1
00402075 EB 09 jmp func1+30h (00402080)
00402077 8B 45 AC mov eax,dword ptr [ebp-54h]
0040207A 83 C0 01 add eax,1
0040207D 89 45 AC mov dword ptr [ebp-54h],eax
00402080 83 7D AC 03 cmp dword ptr [ebp-54h],3
00402084 7D 0C jge func1+42h (00402092)
489: {
490: buff[i] = i;
00402086 8B 4D AC mov ecx,dword ptr [ebp-54h]
00402089 8A 55 AC mov dl,byte ptr [ebp-54h]
0040208C 88 54 0D B0 mov byte ptr [ebp+ecx-50h],dl
491: }
00402090 EB E5 jmp func1+27h (00402077)
492: }
494: void func2(void)
495: {
00401520 55 push ebp
00401521 8B EC mov ebp,esp
00401523 81 EC 94 00 00 00 sub esp,94h
00401529 53 push ebx
0040152A 56 push esi
0040152B 57 push edi
0040152C 8D BD 6C FF FF FF lea edi,[ebp-94h]
00401532 B9 25 00 00 00 mov ecx,25h
00401537 B8 CC CC CC CC mov eax,0CCCCCCCCh
0040153C F3 AB rep stos dword ptr [edi]
496: for(int i=1; i<3; i++)
0040153E C7 45 FC 01 00 00 00 mov dword ptr [ebp-4],1
00401545 EB 09 jmp func2+30h (00401550)
00401547 8B 45 FC mov eax,dword ptr [ebp-4]
0040154A 83 C0 01 add eax,1
0040154D 89 45 FC mov dword ptr [ebp-4],eax
00401550 83 7D FC 03 cmp dword ptr [ebp-4],3
00401554 7D 0C jge func2+42h (00401562)
497: {
498: char buff[80];
499: buff[i] = i;
00401556 8B 4D FC mov ecx,dword ptr [ebp-4]
00401559 8A 55 FC mov dl,byte ptr [ebp-4]
0040155C 88 54 0D AC mov byte ptr buff[ecx],dl
500: }
00401560 EB E5 jmp func2+27h (00401547)
501: }
As you can see above the func1() and func2 are identical.
ASKER
So there is no reallocation each iteration through the loop?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.