Solved

simple c++ language question

Posted on 2006-07-17
11
274 Views
Last Modified: 2010-04-01
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.
0
Comment
Question by:Agarici
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 11

Author Comment

by:Agarici
Comment Utility
i know that szSurname will be allocated only once
but what about szName?
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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.
0
 
LVL 2

Assisted Solution

by:Linky
Linky earned 50 total points
Comment Utility
szName will be allocated 10 times, ZeroMemory will clear the block each time though.
0
 
LVL 11

Author Comment

by:Agarici
Comment Utility
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.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>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).
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 11

Author Comment

by:Agarici
Comment Utility
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?
0
 
LVL 86

Accepted Solution

by:
jkr earned 400 total points
Comment Utility
Yes and no. Most modern compilers will move the declaration out of the loop body when set to perform any optimizations in release builds, but when compiling without any optimizations enabled, you're right, it will be allocated 100 times.
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 50 total points
Comment Utility
>>>> For 'f()', that's true, but since 'f()' is called 10 times in the loop...

A good compiler won't make any allocation cause the call to f() might get a subject of optimization cause it doesn't change anything in the calling main().

In Debug mode all optimizations were switched off and you easily can verify that the storage was created 10 times (on the stack so it may get the same address any time). If you replace " TCHAR szName[200];"  by a class object, e. g. "CString strName;" or "std::string strName" you can step into the constructor and destructor any time f() was called.

Regards, Alex

0
 
LVL 11

Author Comment

by:Agarici
Comment Utility
ok
thank you all for the replys
A.
0
 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
> 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.
0
 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
s/when the function exists/when the function exits/
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now