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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:55:46+00:00 2026-05-31T18:55:46+00:00

So recently i was thinking about strcpy and back to K&R where they show

  • 0

So recently i was thinking about strcpy and back to K&R where they show the implementation as

while (*dst++ = *src++) ;

However I mistakenly transcribed it as:

while (*dst = *src)
{
    src++; //technically could be ++src on these lines
    dst++; 
}

In any case that got me thinking about whether the compiler would actually produce different code for these two. My initial thought is they should be near identical, since src and dst are being incremented but never used I thought the compiler would know not to try to acually preserve them as “variables” in the produced machine code.

Using windows7 with VS 2010 C++ SP1 building in 32 bit Release mode (/O2), I got the dis-assembly code for both of the above incarnations. To prevent the function itself from referencing the input directly and being inlined i made a dll with each of the functions. I have omitted the prologue and epilogue of the produced ASM.

    while (*dst++ = *src++)
6EBB1003 8B 55 08             mov         edx,dword ptr [src]     
6EBB1006 8B 45 0C             mov         eax,dword ptr [dst]     
6EBB1009 2B D0                sub         edx,eax                //prepare edx so that edx + eax always points to src     
6EBB100B EB 03                jmp         docopy+10h (6EBB1010h)  
6EBB100D 8D 49 00             lea         ecx,[ecx]              //looks like align padding, never hit this line
6EBB1010 8A 0C 02             mov         cl,byte ptr [edx+eax]  //ptr [edx+ eax] points to char in src  :loop begin
6EBB1013 88 08                mov         byte ptr [eax],cl      //copy char to dst
6EBB1015 40                   inc         eax                    //inc src ptr
6EBB1016 84 C9                test        cl,cl                  // check for 0 (null terminator)
6EBB1018 75 F6                jne         docopy+10h (6EBB1010h)  //if not goto :loop begin
        ;

Above I have annotated the code, essentially a single loop , only 1 check for null and 1 memory copy.

Now lets look at my mistake version:

    while (*dst = *src)
6EBB1003 8B 55 08             mov         edx,dword ptr [src]  
6EBB1006 8A 0A                mov         cl,byte ptr [edx]  
6EBB1008 8B 45 0C             mov         eax,dword ptr [dst]  
6EBB100B 88 08                mov         byte ptr [eax],cl       //copy 0th char to dst
6EBB100D 84 C9                test        cl,cl                   //check for 0
6EBB100F 74 0D                je          docopy+1Eh (6EBB101Eh)  // return if we encounter null terminator
6EBB1011 2B D0                sub         edx,eax  
6EBB1013 8A 4C 02 01          mov         cl,byte ptr [edx+eax+1]  //get +1th char  :loop begin
    {
        src++;
        dst++;
6EBB1017 40                   inc         eax                   
6EBB1018 88 08                mov         byte ptr [eax],cl        //copy above char to dst
6EBB101A 84 C9                test        cl,cl                    //check for 0
6EBB101C 75 F5                jne         docopy+13h (6EBB1013h)   // if not goto :loop begin
    }

In my version, I see that it first copies the 0th char to the destination, then checks for null , and then finally enters the loop where it checks for null again. So the loop remains largely the same but now it handles the 0th character before the loop. This of course is going to be sub-optimal compared with the first case.

I am wondering if anyone knows why the compiler is being prevented from making the same (or near same) code as the first example. Is this a ms compiler specific issue or possibly with my compiler/linker settings?


here is the full code, 2 files (1 function replaces the other).

// in first dll project
__declspec(dllexport) void docopy(const char* src, char* dst)
{
    while (*dst++ = *src++);
}

__declspec(dllexport) void docopy(const char* src, char* dst)
{
    while (*dst = *src)
    {
        ++src;
        ++dst;
    }
}


//seprate main.cpp file calls docopy
void docopy(const char* src, char* dst);
char* source ="source";
char destination[100];
int main()
{

    docopy(source, destination);
}
  • 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-31T18:55:47+00:00Added an answer on May 31, 2026 at 6:55 pm

    Because in the first example, the post-increment happens always, even if src starts out pointing to a null character. In the same starting situation, the second example would not increment the pointers.

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

Sidebar

Related Questions

I have recently been thinking about the difference between the two ways of defining
Recently I've been thinking about finite state machines (FSMs), and how I would implement
I am recently thinking about writing self-modifying programs , I think it may be
Recently I've been thinking about performance difference between class field members and method variables.
I've been thinking about this recently and I don't know a really nice and
So recently I started thinking about a way of improving speed (vs cpu usage)
I was thinking recently about what projects I could start that would be of
I've been thinking about this for a while and its got to a point
Recently I've been thinking about securing some of my code. I'm curious how one
We have been thinking recently about integrating our J2EE system with other applications written

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.