C++ function parameters: use a reference or a pointer (and then dereference)?

后端 未结 11 2215
臣服心动
臣服心动 2021-02-05 10:49

I was given some code in which some of the parameters are pointers, and then the pointers are dereferenced to provide values. I was concerned that the pointer dereferencing wou

11条回答
  •  孤独总比滥情好
    2021-02-05 11:08

    Here's the difference in the generated assembly with g++. a.cpp is pointers, b.cpp is references.

    $ g++ -S a.cpp
    
    $ g++ -S b.cpp
    
    $ diff a.S b.S
    1c1
    <       .file   "a.cpp"
    ---
    >       .file   "b.cpp"
    4,6c4,6
    < .globl __Z7MyFunc1PiS_
    <       .def    __Z7MyFunc1PiS_;        .scl    2;      .type   32;     .endef
    < __Z7MyFunc1PiS_:
    ---
    > .globl __Z7MyFunc1RiS_
    >       .def    __Z7MyFunc1RiS_;        .scl    2;      .type   32;     .endef
    > __Z7MyFunc1RiS_:
    

    Just the function name is slightly different; the contents are identical. I had identical results when I used g++ -O3.

提交回复
热议问题