Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

simple c++ language question

Posted on 2006-07-17
11
280 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
ID: 17122955
i know that szSurname will be allocated only once
but what about szName?
0
 
LVL 86

Expert Comment

by:jkr
ID: 17123217
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
ID: 17123226
szName will be allocated 10 times, ZeroMemory will clear the block each time though.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 11

Author Comment

by:Agarici
ID: 17123293
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
ID: 17123425
>>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
 
LVL 11

Author Comment

by:Agarici
ID: 17123515
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
ID: 17123559
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
ID: 17123560
>>>> 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
ID: 17123583
ok
thank you all for the replys
A.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 17123645
> 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
ID: 17123653
s/when the function exists/when the function exits/
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

840 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