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

  • Home
  • SEARCH
  • 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 8366055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:48:51+00:00 2026-06-09T12:48:51+00:00

I recently had an interview question where I had to implement memcpy. I’ve used

  • 0

I recently had an interview question where I had to implement memcpy. I’ve used memcpy plenty in my experience so it didn’t seem like a tough problem.

So, I started implementing a loop to copy one address at a time from pointer to pointer, something like this:

void memcpy(void* dest, void* src, int size){
    for(int index = 0; index < size; index++){
        dest[index] = src[index];
    }
}

However the interviewers interrupted noting that the man page for memcpy says it “copies n bytes from src to dest” (which I confirmed later) and then wanted me to iterate instead by size/4 and then pick up the remaining with another loop of index < size%4 (I guess assuming it was a 32 bit system?)

Well, this seemed strange seeing as how I’ve used memcpy for years with no problem without having to give it a *4 modifier). When I got home I fired up gdb and copied a small string “hello” and was careful to input the size both with strlen() and constants to see where it starts and stops.

    char* src = "hello";
    char* dest = calloc(16, sizeof(char));
    int len = strlen(src);
    memcpy(dest, src, len); // both my version and official version

Now I carefully examined src and dest with gdb which both contained “hello\0”.

So my question is: what am I not understanding about using the number 4, (or “size in bytes”)? And why does the documentation say “n bytes” when that’s not really the behavior? What am I not seeing clearly here?

  • 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-09T12:48:53+00:00Added an answer on June 9, 2026 at 12:48 pm

    They were asking you to optimize your implementation and have it copy a 32-bit word at a time inside the loop vs. a byte at a time. This would necessitate some careful checking to handle the boundary cases, such as size not being a multiple of 4, or dest or src not being aligned on a 4-byte boundary.

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

Sidebar

Related Questions

This is one of an interview question which I had recently. I would like
I recently had this question in an interview and this is what I came
Recently I had a question on the interview - I was asked to compare
Recently i had an interview in which the question was asked as How would
I recently had this question in an interview to write test cases to test
I recently had to work around the different default sizes used for enumerations in
This task has already been asked/answered, but I recently had a job interview that
I had an interview recently and he asked me about Singleton Design Patterns about
I recently had a problem with two threads sticking in deadlock because they weren't
I recently had a little problem using a jquery plugin with coffeescript (within the

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.