Solved

memory question

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 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.

920 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

16 Experts available now in Live!

Get 1:1 Help Now