Solved

memory question

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
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.

828 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