473,465 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

The same assembler code for foo(char&) and foo(char*)


==========================================
Windows 2000 Professional
CYGWIN_NT-5.0 1.5.4(0.94/3/2)
GNU g++ version 3.2 20020927 (prerelease)
GNU objdump 2.14.90 20030901
==========================================
We can see that the same assembly code is generated for
* foo2 (char& )
and
* foo3 (char* ).

Does it mean that after parsing
a compiler "sees"/implements foo(char&) and foo(char*)
as identical function?

In other words, on the assembly stage there is no difference
between char& and char* (?).
====== C++ code : BEGIN ======
// File t1.cpp

static int sink = 0;

static void foo1 (char var) { sink += var; }
static void foo2 (char& var) { sink += var; }
static void foo3 (char* ptr) { sink += *ptr; }
static void foo4 () { sink += 1; }

====== C++ code : END ========

====== Compilation : BEGIN ======

$ g++ -c t1.cpp

====== Compilation : END ========

====== objdump : BEGIN ======

$ objdump -CSD t1.o

t1.o: file format pe-i386

Disassembly of section .text:

00000000 <foo1(char)>:
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 83 ec 04 sub $0x4,%esp
6: 8b 45 08 mov 0x8(%ebp),%eax
9: 88 45 ff mov %al,0xffffffff(%ebp)
c: 0f be 45 ff movsbl 0xffffffff(%ebp),%eax
10: 01 05 00 00 00 00 add %eax,0x0
16: c9 leave
17: c3 ret

00000018 <foo2(char&)>:
18: 55 push %ebp
19: 89 e5 mov %esp,%ebp
1b: 8b 45 08 mov 0x8(%ebp),%eax
1e: 0f be 00 movsbl (%eax),%eax
21: 01 05 00 00 00 00 add %eax,0x0
27: 5d pop %ebp
28: c3 ret
29: 90 nop

0000002a <foo3(char*)>:
2a: 55 push %ebp
2b: 89 e5 mov %esp,%ebp
2d: 8b 45 08 mov 0x8(%ebp),%eax
30: 0f be 00 movsbl (%eax),%eax
33: 01 05 00 00 00 00 add %eax,0x0
39: 5d pop %ebp
3a: c3 ret
3b: 90 nop

0000003c <foo4()>:
3c: 55 push %ebp
3d: 89 e5 mov %esp,%ebp
3f: ff 05 00 00 00 00 incl 0x0
45: 5d pop %ebp
46: c3 ret
47: 90 nop
48: 90 nop
49: 90 nop
4a: 90 nop
4b: 90 nop
4c: 90 nop
4d: 90 nop
4e: 90 nop
4f: 90 nop
Disassembly of section .data:

00000000 <sink>:
...

====== objdump : END ========
=====================================
Alex Vinokur
mailto:al****@connect.to
http://mathforum.org/library/view/10978.html
=====================================

Jul 19 '05 #1
7 2730
In comp.lang.c++ Alex Vinokur <al****@bigfoot.com> wrote:
GNU g++ version 3.2 20020927 (prerelease)
GNU objdump 2.14.90 20030901
[...]
We can see that the same assembly code is generated for
* foo2 (char& )
and
* foo3 (char* ).

Does it mean that after parsing
a compiler "sees"/implements foo(char&) and foo(char*)
as identical function?


That's an implemtation detail. Implementing references as pointers is
certainly feasible and - as you have shown - done.

It is not required by the language, though.

Andre'
Jul 19 '05 #2
=?iso-8859-1?Q?Andr=E9_P=F6nitz?= <po*****@gmx.net> writes:
Implementing references as pointers is
certainly feasible and - as you have shown - done.


How *else* could you implement references?

Cheers,
--
In order to understand recursion you must first understand recursion.
Jul 19 '05 #3
In gnu.g++.help Paul Pluzhnikov <pp*********@earthlink.net> wrote:
=?iso-8859-1?Q?Andr=E9_P=F6nitz?= <po*****@gmx.net> writes:
Implementing references as pointers is
certainly feasible and - as you have shown - done.


How *else* could you implement references?


I don't know...

Andre'
Jul 19 '05 #4

"Paul Pluzhnikov" <pp*********@earthlink.net> wrote in message news:ue***************@earthlink.net...
=?iso-8859-1?Q?Andr=E9_P=F6nitz?= <po*****@gmx.net> writes:
Implementing references as pointers is
certainly feasible and - as you have shown - done.


How *else* could you implement references?


Except in a few cases where they need to actually be stored
(like you put a reference member in a class), the compiler just
inserts whatever it would have needed to access the original
object. A trivial example;

int i = 5;
int& r = i;

r++;

I know of no compiler that would actually allocate any storage for r
(as a matter of fact, the code generated is indistinguisable from

i++;

Jul 19 '05 #5
Paul Pluzhnikov wrote in news:ue***************@earthlink.net:
=?iso-8859-1?Q?Andr=E9_P=F6nitz?= <po*****@gmx.net> writes:
Implementing references as pointers is
certainly feasible and - as you have shown - done.


How *else* could you implement references?


When you need to pass or store a reference then they do need
to be pointers, but not nessaseraly the same *type* of pointer
as a regular C++ ponters, references arn't allowed by the
language to be NULL for instance, so there maybe some machines
out there for which a reference can be stored in a more efficient
way than a pointer.
Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #6
On Thu, 18 Sep 2003 13:13:57 +0300, "Alex Vinokur"
<al****@bigfoot.com> wrote in comp.lang.c++:

==========================================
Windows 2000 Professional
Windows 2000 Professional is off-topic in comp.lang.c++.
CYGWIN_NT-5.0 1.5.4(0.94/3/2)
The version of your platform's infrastructure is off-topic in
comp.lang.c++.
GNU g++ version 3.2 20020927 (prerelease)
The version of your compiler is off-topic in comp.lang.c++.
GNU objdump 2.14.90 20030901
The version of some utility program you use is off-topic in
comp.lang.c++.
==========================================
We can see that the same assembly code is generated for


Assembly language generated by any compiler is off-topic in
comp.lang.c++.

Stop cross-posting off-topic crap to comp.lang.c++.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 19 '05 #7
In article <bk************@ID-79865.news.uni-berlin.de>,
al****@bigfoot.com says...

[ ... ]
We can see that the same assembly code is generated for
* foo2 (char& )
and
* foo3 (char* ).

Does it mean that after parsing
a compiler "sees"/implements foo(char&) and foo(char*)
as identical function?


At most, it means that one port of one compiler produces similar code
for the implementation. Certainly code generation comes after parsing,
but that's certainly not ALL that happens after parsing, and there's no
question that throughout most of the compilation process, the two _must_
be treated distinctly from each other.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: pembed2003 | last post by:
Hi coders, I have the following: void f1(char* &s){ *s = 'a'; } void f2(char* s){ *s = 'b'; }
3
by: kieran | last post by:
Hi, I'm using fstream.get to read in a character from a file, then print it on the screen. I have a file called test.log that contains "Hello, World!", but when I try and print the contents out on...
10
by: pembed2003 | last post by:
Hi coders, I have the following: void f1(char* &s){ *s = 'a'; } void f2(char* s){ *s = 'b'; }
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
3
by: david | last post by:
I do not how to include char & in a string. In C, use \&. But I do not know how in VB.NET? any one give me hand? I have the following error message. David ...... Syntax error: Missing...
6
by: cj | last post by:
I'm receiving an xml formatted string that I pull data from by reading it into an xml document like this: Dim doc As New Xml.XmlDocument doc.LoadXml(respstr) Dim co_name As Xml.XmlNodeList =...
5
by: arnuld | last post by:
this is the code: ------------------------------------------------------------------ #include <iostream> void g(char&){}; void h(const char&) {}; int main() { char c;
3
by: ommail | last post by:
Hi I wonder if regular expressions are in general sower than using classes like String and Char when used for validating/parsing text data? I've done some simple test (using IsMatch()) method...
6
by: Darin Johnson | last post by:
I keep running across that I'm maintaining that likes to define function parameters as "const char &" or "const int &", etc. Ie, constant reference parameters to a primitive type. This is for...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.