CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2017
    Posts
    3

    in this really allowed function or we have undefined behavior

    for swap function we have two choice: ref-style:

    Code:
     void swap (int &a, int &b)
        {
          int temp;
    
          temp = b;
         b   = a;
         a   = temp;   
       }
    and pointer-style:

    Code:
      void swap (int *a, int *b)
        {
         int temp;
    
        temp = *b;
        *b   = *a;
        *a   = temp;   
        }
    ref-style absolutely legal but pointer-style have some issue, when we try to use this function variables pass by value not by reference-even they are pointers- in fact we try to use memory of a local variable outside its function, and may by in some day in some machine we have undefined behavior ,

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: in this really allowed function or we have undefined behavior

    variables pass by value not by reference-even they are pointers- in fact we try to use memory of a local variable outside its function, and may by in some day in some machine we have undefined behavior ,
    No. You have a misunderstanding. The pointer is passed by value. So the contents of the memory pointed to by a are swapped with the contents of the memory pointed to by b. The variable temp is not used outside of the scope of the function and all is well.

    Code:
    void swap (int *a, int *b)
    This means that a and b are pointers to memory that contain a type of int.

    Code:
    temp = *b;
    This means that temp becomes the value of the int stored at the memory location pointed to by b - not the address of the memory location itself.

    *a and *b mean different things depending upon their context.

    Cheers.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jan 2017
    Posts
    3

    Re: in this really allowed function or we have undefined behavior

    Let me explain more,
    In this code:

    Code:
    main()
    {
        //
        {
            int i=12;
            int *j=&i;
        }
        //in this area, there is not i,j, but phisically threre is  unsafe-relationship between &i and what point to (12) , any more logic according to this assumtion may be work but not safe
    }
    Last edited by 2kaud; January 22nd, 2017 at 09:24 AM.

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: in this really allowed function or we have undefined behavior

    i is of type int and is initialised with value 12.

    j is of type pointer to int and is initialised with the address of the variable i. So dereferencing of j will in the above code give a value of 12.

    Code:
    main()
    {
        int i=12;
        int *j=&i;
    
        swap(&i, j);
    }
    This is still valid - although pointless. It swaps the value at memory location address of i with the value at memory location pointed to by j. In this example the memory addresses are the same but it is still correct code.

    there is unsafe-relationship between &i and what point to (12)
    The address to which j points can be changed as j is not const.

    If then
    Code:
    *j = 20;
    then the value of i will have changed to be 20. This is valid in c/c++. So I'm not really sure I'm understanding the point you are trying to make?
    Last edited by 2kaud; January 22nd, 2017 at 09:41 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Jan 2017
    Posts
    3

    Re: in this really allowed function or we have undefined behavior

    second post more general then swap function and my example code very simple and not include swap line

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: in this really allowed function or we have undefined behavior

    Let's refer back to your code in post #3:
    Quote Originally Posted by dt128 View Post
    Code:
    main()
    {
        {
            int i=12;
            int *j=&i;
        }
        //in this area, there is not i,j, but phisically threre is  unsafe-relationship between &i and what point to (12) , any more logic according to this assumtion may be work but not safe
    }
    You are right to say that at the point of the code marked by your comment, i and j no longer exist. Therefore there is no relationship between i and j: neither exist, so there is no "unsafe-relationship". Perhaps what you have in mind is something like this:
    Code:
    int main()
    {
        int* j = nullptr;
        {
            int i = 12;
            j = &i;
        }
        // #1
    }
    Now, at point #1, i no longer exists, but j exists and continues to point to i. There would thus be undefined behaviour if at point #1 j was dereferenced.

    Going back to your code in post #1:
    Code:
    void swap(int *a, int *b)
    {
        int temp;
    
        temp = *b;
        *b   = *a;
        *a   = temp;
    }
    We see that like your code in post #3, temp, a, and b all exist in the same scope, i.e., the body of the swap function. Therefore after the function returns to the caller, temp, a, and b all cease to exist. Hence there is no undefined behaviour as long as the correct arguments were passed to swap in the first place.

    Referring back to your code in post #3, the code in the swap function is akin to this:
    Code:
    int main()
    {
        int i = 12;
        {
            int* j = &i;
            // #1
        }
        // #2
    }
    The initialisation of j with &i would be analogous to the swap function call. Point #1 would be analogous to where the swapping actually happens: notice that both i and j are in scope (though in the swap function, the variable from the caller will not be in scope). Point #2 would be analogous to where the swap function returns to the caller: notice that while j no longer exists, i still exists, so there is no problem.
    Last edited by laserlight; January 22nd, 2017 at 11:03 AM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured