473,785 Members | 2,807 Online
Bytes | Software Development & Data Engineering Community
+ 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****@c onnect.to
http://mathforum.org/library/view/10978.html
=============== =============== =======

Jul 19 '05 #1
7 2753
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=F 6nitz?= <po*****@gmx.ne t> 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*********@ea rthlink.net> wrote:
=?iso-8859-1?Q?Andr=E9_P=F 6nitz?= <po*****@gmx.ne t> 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*********@ea rthlink.net> wrote in message news:ue******** *******@earthli nk.net...
=?iso-8859-1?Q?Andr=E9_P=F 6nitz?= <po*****@gmx.ne t> 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 indistinguisabl e from

i++;

Jul 19 '05 #5
Paul Pluzhnikov wrote in news:ue******** *******@earthli nk.net:
=?iso-8859-1?Q?Andr=E9_P=F 6nitz?= <po*****@gmx.ne t> 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.l earn.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
2248
by: pembed2003 | last post by:
Hi coders, I have the following: void f1(char* &s){ *s = 'a'; } void f2(char* s){ *s = 'b'; }
3
4194
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 the screen I get "Hello, World!!". The following is the code (please ignore all wxWidgets classes): void SystemBackupFrm::compress() { using namespace std;
10
293
by: pembed2003 | last post by:
Hi coders, I have the following: void f1(char* &s){ *s = 'a'; } void f2(char* s){ *s = 'b'; }
5
3980
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 variable **var make it an array of pointers? I realize that 'char **var' is a pointer to a pointer of type char (I hope). And I realize that with var, var is actually a memory address (or at
3
1937
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 operand after ''&''prot='' operator. Description: An unhandled exception occurred during the execution of the
6
2246
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 = doc.GetElementsByTagName("co_name") textbox1.text = co_name(0).innertext Now I'm getting company names that have ampersands in them. I was not aware that was not allowed in xml and had no method of dealing with it.
5
2232
by: arnuld | last post by:
this is the code: ------------------------------------------------------------------ #include <iostream> void g(char&){}; void h(const char&) {}; int main() { char c;
3
2299
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 and the result was that Regex is either as fast or two times slower than method which used methods from classes
6
5591
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 normal functions, not operators. I keep changing these to just have the plain old type, which is more efficient (I'm using embedded systems) and less obtuse. I'm puzzled why this one programmer insisted on odd style everywhere. Maybe he's just...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10336
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10095
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8978
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6741
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3655
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.