Solved

memory question

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

636 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