Solved

memory question

Posted on 1998-11-27
2
247 Views
Last Modified: 2010-04-01
Is there a difference between:
MyFunc()
{
 if(...)
  return;
 int i;
 ....
}
and:
MyFunc()
{
 int i;
 if(...)
  return;
  ....
}
concerning the stack/memory ??
0
Comment
Question by:nil_dib
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 1178748
No, there's _no_ differnce (except in the notation), as the stack is always set up when the function is entered. I.e. the stack space for all local varaibles is allocated before the first code statement is executed.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1178749
I would have to dissagree somewhat.  To the best of my knowledge that is entirely implimentation defined.  That is, it is up to the designer of the compiler.    In fact, I believe (again I'm not sure) that it is common practice in many implimentations to set up multiple stack frames  in a function that has nested scrope, thus a function like

MyFunc()
{
    {
       int i;
    }
}

would actually set up and destroy a second stack frame in some cases.  There is no telling how such a compiler would handle the case you presented.

I think you can be sure of this.  There is goign to be no significant difference in execution speed between the two.  This is because operations required to provide space for the integer are insignificant.  This would not be true if the variable in question had a constructor that took considerable time to run, for example, if the constructor read from a file.  In that case, the first version would be faster.

You have this quesions 2 times.  You might want to delete the other.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

746 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

10 Experts available now in Live!

Get 1:1 Help Now