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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:13:19+00:00 2026-06-12T09:13:19+00:00

Ok, I was trying to implement memmove just as a programming exercise, and I

  • 0

Ok, I was trying to implement memmove just as a programming exercise, and I get a memory access violation in the memmove function when I try to use malloc. Here is the function:

//Start

void* MYmemmove (void* destination, const void* source, size_t num) { 

    int* midbuf = (int *) malloc(num); // This is where the access violation happens.
    int* refdes = (int *) destination; // A pointer to destination, except it is casted to int*
    int* refsrc = (int *) source; // Same, except with source
    for (int i = 0;num >= i;i++) { 
        midbuf[i] = *(refsrc + i); // Copy source to midbuf
    }
    for (int i = 0;num >= i;i++) { 
        refdes[i] = *(midbuf + i); // Copy midbuf to destination
    } 
    free(midbuf); // free midbuf 
    refdes = NULL; // Make refdes not point to destination anymore
    refsrc = NULL; // Make refsrc not point to source anymore
    return destination;
}

By the way, I am sort of a newbie to pointers, so don’t be suprised if there is some mistakes.
What am I doing wrong?

  • 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-06-12T09:13:19+00:00Added an answer on June 12, 2026 at 9:13 am

    Please be careful with the other suggestions! The answer depends on how your memmove will be used. The other answers state you should change your malloc call to account for the size of an int. However, if your memmove function will be used to mean “move this number of bytes” then the implementation would be wrong. I would instead go about using char* as that takes care of several problems in one go.

    Also, an int is typically 4 bytes, and char is typically 1 byte. If the void* address you receive is not word aligned (not a multiple of 4 bytes), you will have a problem: To copy an int that is not word aligned you will have to do more than one read and expensive bit masking. This is inefficient.

    Finally, the memory access violation happened because you were incrementing your midbuf int pointer each time, and moving forward 4 bytes at a time. However, you only allocated num bytes, and thus would eventually try to access past the end of the allocated region.

    /** Moves num bytes(!) from source to destination */
    void* MYmemmove (void* destination, const void* source, size_t num) { 
    
        // transfer buffer to account for memory aliasing
        // http://en.wikipedia.org/wiki/Aliasing_%28computing%29
        char * midbuf = (char *) malloc(num); // malloc allocates in bytes(!)
        char * refdes = (char *) destination;
        char * refsrc = (char *) source;
    
        for (int i = 0; i < num; i++) { 
            midbuf[i] = *(refsrc + i); // Copy source to midbuf
        }
    
        for (int i = 0; i < num; i++) { 
            refdes[i] = *(midbuf + i); // Copy midbuf to destination
        } 
    
        free(midbuf); // free midbuf
        // no need to set the pointers to NULL here.
        return destination;
    }
    

    By copying byte by byte, we avoid alignment issues, and cases where num itself could be not a multiple of 4 bytes (e.g. 3, so an int is too large for that move).

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

Sidebar

Related Questions

Trying to implement a search similar to here .This searches properties based on city,locality,property
Trying to implement 3-layer (not: tier, I just want to separate my project logically,
Ok well I'm trying implement something similar to the 'undo' function in many image
Trying to implement simple error handling without adding buku try / except statements to
Trying to implement the excellent jQuery bidirectional infite scroll as explained here: http://www.bennadel.com/blog/1803-Creating-A-Bidirectional-Infinite-Scroll-Page-With-jQuery-And-ColdFusion.htm For
Trying to implement a function that will return a list of ints the represent
Trying to implement the following structure from c to use NSArray in objective-c: In
Trying to implement AVAudioplayer and get some metering data of the played music, but
Trying to implement the example from here. Facebook Share passes along the URL of
trying to implement the following but its not working <script> function loadmap(lat,lng) { //alert(lat,lang);

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.