Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6846667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:39:06+00:00 2026-05-27T00:39:06+00:00

I am confused between these two functions: void Swap_byPointer1(int *x, int *y){ int *temp=new

  • 0

I am confused between these two functions:

void Swap_byPointer1(int *x, int *y){
    int *temp=new int;
    temp=x;
    x=y;
    y=temp;
}

void Swap_byPointer2(int *x, int *y){
    int *temp=new int;
    *temp=*x;
    *x=*y;
    *y=*temp;
}

Why Swap_byPointer2 succeeds to swap between x and y, and Swap_byPointer1does not?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:39:06+00:00Added an answer on May 27, 2026 at 12:39 am

    The first snippet swaps the memory addresses which are the values of the pointers. Since the pointers are local copies, this has no effect for the caller.

    Rewritten without a memory leak:

    void Swap_byPointer1(int *x, int *y){
        //e.g x = 0xDEADBEEF and y = 0xCAFEBABE;
        int *temp=x;
        x=y;
        y=temp;
        //now x = 0xCAFEBABE and y = 0xDEADBEEF
    }
    

    The second swaps the pointees (objects that the pointers point to).

    Rewritten without a memory leak:

    void Swap_byPointer2(int *x, int *y){
        //e.g *x = 100 and *y = 200
        int temp =*x;
        *x=*y;
        *y=temp;
        //now *x = 200 and *y = 100
        //there are now different values at the original memory locations
    }
    

    (Pointers can point to dynamically allocated objects, but don’t have to. Using a pointer does not mean there has to be a new-allocation. Pointers can point to objects with automatic lifetime as well.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im quite confused that what is difference between these two initializations: int (*p)[10]; and
I am confused on the differences between these two code blocks: $(#someButton).click(function() { var
I am very confused between these two consistency models. Please give some timeline examples
I'm confused about the differences between these two special folders. Here's a code snippet
I'm confused about the difference between these two things: $env:path And function path {$env:path}
What is the difference between these two lines of code? int *ptr = &x;
What is difference between the two, i am just confused so much on these
I am confused as to the difference between the two. In my application I
So, I know that there is a difference between these two tidbits of code:
whats the difference between these two access methods? nodes->at(235).push_back(NavigationNode(NULL,0,0)); nodes[235].push_back(NavigationNode(NULL,0,0)); second one gives this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.