Solved

memory question

Posted on 1998-11-27
2
276 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

861 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