Agarici
asked on
simple c++ language question
may be this will sound realy dumb but...
when you have this code:
void f()
{
TCHAR szName[200];
ZeroMemory( szName, 200 );
}
void g()
{
static TCHAR szSurname[200];
ZeroMemory( szSurname, 200 );
}
void main()
{
for( int i = 0; i < 10 ; i++ )
{
f();
g();
}
}
how many time will szName be allocated?
thanks.
A.
when you have this code:
void f()
{
TCHAR szName[200];
ZeroMemory( szName, 200 );
}
void g()
{
static TCHAR szSurname[200];
ZeroMemory( szSurname, 200 );
}
void main()
{
for( int i = 0; i < 10 ; i++ )
{
f();
g();
}
}
how many time will szName be allocated?
thanks.
A.
Since 'szName()' is non-static, it'll be allocated on the stack ten times, that's what the loop
for( int i = 0; i < 10 ; i++ )
does for i ranging from 0...9.
for( int i = 0; i < 10 ; i++ )
does for i ranging from 0...9.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
well... that is what i also know...
i asked this because i had a dispute with a collegue of mine... and he was saying that szName will only be alocated once on the stack... with such a strong belief that made me wory...
thanks
A.
i asked this because i had a dispute with a collegue of mine... and he was saying that szName will only be alocated once on the stack... with such a strong belief that made me wory...
thanks
A.
>>he was saying that szName will only be alocated once on the stack
For 'f()', that's true, but since 'f()' is called 10 times in the loop...
The key difference here is the 'static' keyword on the other local variable, which specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends).
For 'f()', that's true, but since 'f()' is called 10 times in the loop...
The key difference here is the 'static' keyword on the other local variable, which specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends).
ASKER
what if i have:
void f()
{
for( int i = 0 ; i < 10 ; i++)
{
TCHAR szName[200];
ZeroMemory( szName, 200 );
}
}
void main()
{
for( int i = 0; i < 10 ; i++ )
{
f();
}
}
there will be 100 allocations, corect?
void f()
{
for( int i = 0 ; i < 10 ; i++)
{
TCHAR szName[200];
ZeroMemory( szName, 200 );
}
}
void main()
{
for( int i = 0; i < 10 ; i++ )
{
f();
}
}
there will be 100 allocations, corect?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok
thank you all for the replys
A.
thank you all for the replys
A.
> he was saying that szName will only be alocated once on the stack...
It gets allocated on the stack each time the function is called when it is automatic, but what your colleague may have been thinking is that the memory is released when the function exists, so the same chunk of memory will be typically be allocated each time, when you call the function in your loop.
It gets allocated on the stack each time the function is called when it is automatic, but what your colleague may have been thinking is that the memory is released when the function exists, so the same chunk of memory will be typically be allocated each time, when you call the function in your loop.
s/when the function exists/when the function exits/
ASKER
but what about szName?