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 595955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:07:09+00:00 2026-05-13T16:07:09+00:00

I’m trying to swap objects for a homework problem that uses void pointers to

  • 0

I’m trying to swap objects for a homework problem that uses void pointers to swap objects. The declaration of my function has to be:

void swap(void *a, void *b, size_t size);

I’m not looking for the exact code how to do it so I can figure it out by myself, but I’m not sure if I understand it correctly. I found that one problem is by doing:

void *temp;
temp = a;
a = b;
b = temp;

only changes what the pointers point to. Is that correct? If it is correct, why doesn’t swapping pointers actually change the contents between *a and *b. Because if your pointer points to something different, couldn’t you dereference it and the objects would now be different?

Similarly, just switching the values like:

void *temp;
*temp = *a;
*a = *b;
*b = *temp;

Is not correct either, which I’m not sure why. Because again, it seems to me that the content is switched.

Does swapping objects mean complete swapping of memory and value of what a pointer points to?

So it seems like I have to use malloc to allocate enough space for my swap. If I allocate enough memory for one object, assuming they are the same size, I don’t really see how it is different than the other two methods above.

void *temp = malloc(sizeof(pa));
// check for null pointer
temp = a;
// do something I'm not sure of since I don't quite get how allocating space is any 
// different than the two above methods???

Thanks!

  • 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-13T16:07:09+00:00Added an answer on May 13, 2026 at 4:07 pm

    Swapping pointers does not change the pointed-to values. If it did, that would be like swapping address labels on envelopes moving me into your house and you into mine.

    You were nearly there:

    void swap(void *a, void *b, size_t size) {
      char temp[size]; // C99, use malloc otherwise
      // char serves as the type for "generic" byte arrays
    
      memcpy(temp, b,    size);
      memcpy(b,    a,    size);
      memcpy(a,    temp, size);
    }
    

    The memcpy function copies memory, which is the definition of objects in C. (Called POD or plain ol’ data in C++, to compare.) In this way, memcpy is how you do assignment without caring about the type of the object, and you could even write other assignments as memcpy instead:

    int a = 42, b = 3, temp;
    
    temp = b;
    b    = a;
    a    = temp;
    // same as:
    memcpy(&temp, &b,    sizeof a);
    memcpy(&b,    &a,    sizeof a);
    memcpy(&a,    &temp, sizeof a);
    

    This is exactly what the above function does, since you cannot use assignment when you do not know the type of the object, and void is the type that stands in for “unknown”. (It also means “nothing” when used as function return type.)


    As a curiosity, another version which avoids malloc in common cases and doesn’t use C99’s VLAs:

    void swap(void *a, void *b, size_t size) {
      enum { threshold = 100 };
      if (size <= threshold) {
        char temp[threshold];
    
        memcpy(temp, b,    size);
        memcpy(b,    a,    size);
        memcpy(a,    temp, size);
      }
      else {
        void* temp = malloc(size);
        assert(temp); // better error checking desired in non-example code
    
        memcpy(temp, b,    size);
        memcpy(b,    a,    size);
        memcpy(a,    temp, size);
    
        free(temp);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 400k
  • Answers 400k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The new socket is an application-level concept introduced because each… May 15, 2026 at 4:19 am
  • Editorial Team
    Editorial Team added an answer Did you see the link to http://aperiodic.net/phil/archives/Geekery/notes-on-lisp-testing-frameworks.html off the Test… May 15, 2026 at 4:19 am
  • Editorial Team
    Editorial Team added an answer Have a look at the XAML in this answer and… May 15, 2026 at 4:19 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.