CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Dec 2010
    Posts
    32

    Unhappy Need help on this code...(Segmentation Fault)

    Hi all,
    When I was compiling this code in Linux with g++, I got the Segmentation Fault. I guess I might have
    misused the pointer in my function. Could you help me figure out what's wrong with my code?
    What this code should do is swapping the values of int a and int b with function swap...

    Code:
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    
    void swap(int *p, int *q)
    {
      int *temp;
      *temp=*p;
      *p=*q;
      *q=*temp;
    }
    
    int main()
    {
      
      int a=5,b=10;
      int *p=&a;
      int *q=&b;
      swap(p,q);
      cout<<"a="<<a<<endl;
      cout<<"b="<<b<<endl;
      return 0;
    }

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Need help on this code...(Segmentation Fault)

    You have created a pointer, temp, pointing to nothing in particular. You then attempt to write to the location it points to (nowhere in particular).

  3. #3
    Join Date
    Dec 2010
    Posts
    32

    Re: Need help on this code...(Segmentation Fault)

    Oh, yea...
    I changed the code to something like this, then it works. Thank you Lindley.
    Code:
    void swap(int *p, int *q)
    {
      int temp;
      temp=*p;
      * p= *q;
      * q= temp;
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    118

    Re: Need help on this code...(Segmentation Fault)

    Alternatively, to get rid of that pesky temp!

    Code:
    void swap(int *p, int *q)
    {
       *p = *p - *q;
       *q = *q + *p;
       *p = -(*p - *q);
    }
    Shush your complaints! I just like it.

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Need help on this code...(Segmentation Fault)

    Quote Originally Posted by Moschops View Post
    Alternatively, to get rid of that pesky temp!

    [...]


    Where did you find that? Obfuscated C Contest?

    No, seriously, aside from saving the stack space for temp it doesn't seem to be more efficient.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Need help on this code...(Segmentation Fault)

    While a neat mathematical trick, it seems more likely to be less efficient in most cases. Assuming, of course, that the types being swapped even support the operation.

  7. #7
    Join Date
    Aug 2008
    Posts
    902

    Re: Need help on this code...(Segmentation Fault)

    nobody is going to mention std::swap?

    http://www.cplusplus.com/reference/algorithm/swap/

  8. #8
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Need help on this code...(Segmentation Fault)

    The swap obfuscated swap I've always been taught is:

    Code:
    template<typename POD_t>
    void swap(POD_t& lhs, POD_t& rhs)
    {
       lhs^=rhs;
       rhs^=lhs;
       lhs^=rhs;
    }
    It works for the same reasons it works with operator-. This has the advantage of working on ANY pod type. On non POD types there are no guarantees (ie assume it doesn't work).

    In the real world, this is not very efficient, as pretty much every processor now has a single instruction swap built in (explicitly or implicitly).

    If you want more C++ bitwise "tricks", you can always go to http://www-graphics.stanford.edu/~seander/bithacks.html

    Back to the topic at hand, this is how swap is usually implemented.

    Code:
    template <class T>
    void swap(T& a, T& b)
    {
      T c(a);
      a=b;
      b=c;
    }
    Notice the complete lack of pointers. Pointers are evil.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Need help on this code...(Segmentation Fault)

    With C++0x, I suspect it would instead be:

    Code:
    template <class T>
    void swap(T& a, T& b)
    {
      T c(move(a));
      a=move(b);
      b=move(c);
    }

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