Solved

memory question

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

810 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