Visit our sponsor Five Planet Juices  
Home
Microsoft Interview Process
Microsoft HR Questions
Technical Questions
Puzzles/Riddles
Resume Tips and Template
Discuss
Question to Interviewer
Interview Tips
Term Of Use
Site Feedback


Implement and test a memcpy function
The function copies memory block pointed by src into memory block pointed by dst. 1. Request the types of arguments and return value (thus determing the real declaration: void memcpy( void* src, void* dst, size_t size ); 2. Request the units of size. 3. Mention the case of overlapping src and dst blocks. Be very careful about this case. void memcpy( void* src, void* dst, size_t size ) { char *pc_src = src, *pc_dst = dst; //in C++ this will require an explicit cast if(pc_dst <= p_src ) for( char* it_src = pc_src, *it_dst = pc_dst; it_src < pc_src + size_t; it_src++, it_dst++ ) *it_dst = *it_src; else for( char* it_src = pc_src + size-1, *it_dst = pc_dst + size-1; it_src >= pc_src; it_src--, it_dst-- ) *it_dst = *it_src; } Now I'm too lazy to write the tests, but do mention the cases: 1. A usual case. Also check that the memory around src and dst doesn't change. 2. A case of overlapping blocks: src before and beyond dst. Also src and dst at the same address. 3. size = 1 and size = 0 cases. 4. You may be asked to write a case when memory is copied between different data types (e.g. unsidned int and char[4])

 

Comments:

elegant solution
By jagatsastry on Thursday, August 30, 2007 (UMST)
void* memcpy(void* src, const void* dest, size_t size) { char*a=(char*)src; char*b=(char*)dest; if(a<b) { if(a>b+size) exit(OVERLAP); for(int i=0;i<size;i++) *(b+i)=*(a+i); } else { if(b>a+size) exit(OVERLAP); it i; for(int i=0;i<size;i++) *(b+i)=*(a+i); } }

Reply to this Comment
 

Does not take care ofoverlap
By mjeewani on Saturday, September 27, 2008 (UMST)
Memcpy does not take care of overlap, If the buffer overlaps you should use memmove.

Reply to this Comment
 

Add Your Comment

New Articles
  • Phone screen from MICROSOFT Denmark for SDET
    Technical Phone screen after Escreen

  • Please post some non-tech qs for User Experience and Business Analyst job

    I am a CS Grad applied in non-tech position.Please help!


  • Combinations in a character array.
    Write a program that takes input a char array and outputs all the combinations of the characters in the character array.
    Example: consier char array {'a','b','c'}
    the output shouold be abc,cab,bac,acb,cba,bca that is all the combinations of characters 'a','b','c'.


  •  

    Most Popular Articles
  • Crazy Guy On The Airplane
    A line of 100 airline passengers is waiting to board a plane. they each hold a ticket to one of the 100 seats on that flight.

  • What are your greatest weaknesses
    Beware - this is an eliminator question, designed to shorten the candidate list. Any admission of a weakness or fault will earn you an “A” for honesty, but an “F” for the interview.

  • Question: Tell me about yourself.
    Many candidates, unprepared for the question, skewer themselves by rambling, recapping their life story, delving into ancient work history or personal matters.

  •  

    New Posts

  • uu jewelry
    Posted by nemoAnonusy on Thursday, November 20, 2008 (UMST)

  • Philip hypocritic opened the uspecting the change.
    Posted by Fektshho on Thursday, November 20, 2008 (UMST)

  • English army popular one ntelligent individual mobile.
    Posted by Ulvpjtlt on Wednesday, November 19, 2008 (UMST)



  •