-4

In inline functions, the compiler is suggested to copy a piece of code at compile time into the caller program.

#include<iostream>
inline void swap(int *a, int *b){
    a=a^b;
    b=a^b;
    a=a^b;
}
int main(){
    int a=2,b=3;
        swap(a,b);
    std::cout<<a<<b;
    return 0;
}

Here, the swap function is call by value. So, suppose that the above code is inlined by the compiler, then will the actual variable a and b will be passed so that it works perfectly or the part that has an inline function has some other scope?

Because, if it is inlined, the code is directly copied into main here, and since no parameter passing will take place, the code have the access to a and b.

ps:The question has been edited because I was not able to precisely put in words my doubt so it has so may downvotes.

Aditya Gaddhyan
  • 354
  • 1
  • 14
  • 3
    Notice that `inline` keyword and inlining are unrelated. – Jarod42 Aug 23 '19 at 16:58
  • 2
    Your swap does xor on pointers. Do instead: inline void swap(int *a, int *b){ *a=*a^*b; *b=*a^*b; *a=*a^*b; } call as swap(&a, &b). – 0kcats Aug 23 '19 at 17:00
  • While it's not a very good question, I don't understand why it has so many downvotes. There is a misunderstanding, quite natural at that. –  Aug 23 '19 at 17:02
  • 1
    The [inline specifier](https://en.cppreference.com/w/cpp/language/inline) does not mean what you think it does. It doesn't *actually* have much (if *anything*) to do with whether code is actually inlined. Rather it is related to the [One Definition Rule](https://en.cppreference.com/w/cpp/language/definition). – Jesper Juhl Aug 23 '19 at 17:03
  • @dyukha Maybe to get it removed so it does not pop up in the search? – 0kcats Aug 23 '19 at 17:03
  • @0kcats, with this logic I can suggest hundreds questions which should be downvoted, but wasn't. –  Aug 23 '19 at 17:04
  • 1
    `inline void swap(int *a, int *b){ a=a^b; b=a^b; a=a^b; }` - That's just *wrong* and not what you intended. You want to operate on what the pointers *point to*, not the pointer values themselves. – Jesper Juhl Aug 23 '19 at 17:06
  • Recommended reading: [When should I write the keyword 'inline' for a function/method?](https://stackoverflow.com/questions/1759300/when-should-i-write-the-keyword-inline-for-a-function-method) – user4581301 Aug 23 '19 at 17:07
  • "In inline functions, the piece of code at compile time is copied into the caller program." - No. Not correct. It *may* be. But it also *may not*. `inline` has little to do with that. – Jesper Juhl Aug 23 '19 at 17:11
  • "caller program" -- do you mean "calling function"? Because there is only one program being compiled, and the inline function is already part of it by virtue of it being defined. No copying is required. – JaMiT Aug 23 '19 at 17:20
  • This doesn't address the question, but this XOR approach to swapping is not a good idea. There are too many ways it can go wrong. For example, if you try to swap an integer with itself you end up setting its value to 0. – Pete Becker Aug 23 '19 at 17:20
  • your first snippet is wrong. The function takes pointers but you pass `int`s. Once you fix that your manual inlining should be ok – 463035818_is_not_an_ai Aug 23 '19 at 17:32
  • You should see compiler warnings. You are passing variables to a function that takes pointers. Prefix the variables in your function with "p_" or something similar and you should have compiler warnings or errors pop up. – Thomas Matthews Aug 23 '19 at 20:19

3 Answers3

1

Why is it that the first code fails to work until a pointer to a and b are used?

Because swap takes pointers. You can't call it without pointers, even it it would be inlined. The program still needs to be syntactically correct even if the semantics change due to optimizations.

Also note that inline is just a suggestion to the compiler to inline the function. It's actual purpose is to prevent multiple definition errors if you define the function in multiple translation units.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • suppose that the optimization finally inlines the code. Then will it work? – Aditya Gaddhyan Aug 23 '19 at 17:10
  • 2
    No, because optimization does not change the meaning of correct code. – john Aug 23 '19 at 17:11
  • 1
    @AdityaGaddhyan Worth noting that even though the function compiles with pointers, it doesn't work. it swaps the pointers around rather than the pointed-at values. Since the pointers are passed by value, there are no observable side effects and the compiler will probably discard the function entirely. Hard to beat the execution time, but not what you want. Consider `void swap(int &a, int &b)` as a replacement. – user4581301 Aug 23 '19 at 17:20
  • 1
    @AdityaGaddhyan The code has to be syntactically correct first. `swap(a,b);` is not correct since `swap` takes pointers. If you do `swap(&a,&b);` then the code is correct, and the compiler can then choose to instead inline the code instead of calling a function and dealing with pointers. – NathanOliver Aug 23 '19 at 17:21
  • @AdityaGaddhyan Your problem has nothing to do with inlining. – eesiraed Aug 24 '19 at 04:52
0

In inline functions, the piece of code at compile time is copied into the caller program.

That's completely incorrect. Making a function inline has no effect on what your program does. If your function does not work without inline then it's not going to start working when you make it inline, and vice versa.

So the premise of the question is wrong.

john
  • 85,011
  • 4
  • 57
  • 81
  • "If your function does not work without inline then it's not going to start working when you make it inline" - It *might*, if the reason it didn't work was that it was violating ODR without the `inline`. – Jesper Juhl Aug 23 '19 at 17:08
0

Your inline function and usage are wrong. You are passing variables to a function that requires pointers.

In main, your function call should be:
swap(&a, &b);

If you don't want to mess with pointers, I recommend a version using references:

#include <iostream>
using std::cout;

inline void my_swap(int& a, int& b)
{
  int temp = a;
  a = b;
  b = a;
}

int main()
{
  int p = 42, q = 1;
  cout << "p: " << p << ", q: " << q << "\n";
  swap(p, q);
  cout << "p: " << p << ", q: " << q << "\n";
  return 0;
}

The parameters of swap are passed by reference which means that the function will modify the variables passed to swap.

Take the above main function and print out the assembly language. There should be no branch or call instructions to the swap function. Increment the optimization levels as necessary (some compilers won't inline the code at the lowest optimization levels, to make debugging easier). A very intelligent compiler may replace the entire function with an assembly language instruction for swapping.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154