Solved

memory question

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now