Solved

memory question

Posted on 1998-11-27
2
286 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
[X]
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
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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…
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.

705 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