Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

memory question

Posted on 1998-11-27
2
Medium Priority
?
274 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
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 1178750
No, there's no difference. The stack for the local variable is usually set up before the first machine instruction is executed.
For example:
// compile: cl stack.cpp /link /entry:main
void main( void)
{
      __asm{ nop};
}
void MyFunc1()
{
      __asm{ nop};
      __asm{ nop};
if(1)
return;
int i;
}
void MyFunc2()
{
      __asm{ nop};
      __asm{ nop};
      __asm{ nop};
int i;
if(1)
return;
}
The 'nops' are only inserted to distinguish between the functions in the following disassembly (created with: 'dumpbin /disasm stack.exe > stack.asm'):
icrosoft (R) COFF Binary File Dumper Version 5.10.7303
Copyright (C) Microsoft Corp 1992-1997. All rights reserved.


Dump of file stack.exe

File Type: EXECUTABLE IMAGE

  00401000: 55                 push        ebp     ; start of 'main()'
  00401001: 8B EC              mov         ebp,esp
  00401003: 53                 push        ebx
  00401004: 56                 push        esi
  00401005: 57                 push        edi
  00401006: 90                 nop
  00401007: 5F                 pop         edi
  00401008: 5E                 pop         esi
  00401009: 5B                 pop         ebx
  0040100A: 5D                 pop         ebp
  0040100B: C3                 ret
  0040100C: 55                 push        ebp     ; start of 'MyFunc1()'
  0040100D: 8B EC              mov         ebp,esp
  0040100F: 51                 push        ecx
  00401010: 53                 push        ebx
  00401011: 56                 push        esi
  00401012: 57                 push        edi
  00401013: 90                 nop
  00401014: 90                 nop
  00401015: 5F                 pop         edi
  00401016: 5E                 pop         esi
  00401017: 5B                 pop         ebx
  00401018: 8B E5              mov         esp,ebp
  0040101A: 5D                 pop         ebp
  0040101B: C3                 ret
  0040101C: 55                 push        ebp     ; start of 'MyFunc2()'
  0040101D: 8B EC              mov         ebp,esp
  0040101F: 51                 push        ecx
  00401020: 53                 push        ebx
  00401021: 56                 push        esi
  00401022: 57                 push        edi
  00401023: 90                 nop
  00401024: 90                 nop
  00401025: 90                 nop
  00401026: 5F                 pop         edi
  00401027: 5E                 pop         esi
  00401028: 5B                 pop         ebx
  00401029: 8B E5              mov         esp,ebp
  0040102B: 5D                 pop         ebp
  0040102C: C3                 ret

  Summary

        1000 .text

As you can see: NO difference ;-)

0
 
LVL 86

Expert Comment

by:jkr
ID: 1178751
Sorry for eventually having answered this question twice - but EE behaves a bit strange today...

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 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.

730 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