Help, Im trying to implement my own strcpy

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2005
Posts: 15,406
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help, Im trying to implement my own strcpy

 
0
  #11
Dec 2nd, 2008
Originally Posted by StuXYZ View Post
can I just add, you need to remember to add a
*dest=0; line after the while. You will need something similar in any of the other posts.
No with Arkm's algorithm you don't because it will also copy the null terminator.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Help, Im trying to implement my own strcpy

 
0
  #12
Dec 2nd, 2008
Regrettably, it's not ArkM's algorithm. It's K&R code.
Thank you, Ancient Dragon: this code is OK, it copies null byte
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: Help, Im trying to implement my own strcpy

 
0
  #13
Dec 2nd, 2008
What about Buffer Overflow?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Help, Im trying to implement my own strcpy

 
0
  #14
Dec 2nd, 2008
Originally Posted by Laiq Ahmed View Post
What about Buffer Overflow?
Look at the requirements: if you forgot strcpy specification I can recall, try to search the target size there
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,406
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help, Im trying to implement my own strcpy

 
0
  #15
Dec 2nd, 2008
Originally Posted by Laiq Ahmed View Post
What about Buffer Overflow?
That is a problem with all standard C functions. If you use one of the new Microsoft compilers, Microsoft declared them depreciated and wants us to use the _s functions, such as strcpy_s(), which has an additional destination buffer size parameter. But even that could cause buffer overflow if the calling function passed the wrong buffer size. There is no 100% bullet-proof method in C to prevent buffer overflow. But if you follow the rules and bother to actually read the function's requirements then you can minimize the buffer overflow problem.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 21
Reputation: namehere05 is an unknown quantity at this point 
Solved Threads: 1
namehere05 namehere05 is offline Offline
Newbie Poster

wow this thing is rolling!!

 
0
  #16
Dec 2nd, 2008
I modified the program it looks like this but no doubt theres something wrong as it wont output anything I await your wisdom guys

  1. #include <iostream>
  2. #include <cstring>
  3. using std::cin;
  4. using std::cout;
  5.  
  6. void StrCpy(char * Dest , char* Src )
  7. {
  8. int i;
  9. char *tmp = new char[strlen(Src)+1];
  10. for(i = 0 ; *Src!='\0' ; Src++)
  11. tmp[i]=*Src;
  12. tmp[i]='\0';
  13. Dest = tmp;
  14. for(int i= 0 ; tmp[i]!='\0' ; i++)
  15. cout << tmp[i];
  16. }
  17.  
  18. void main()
  19. {
  20.  
  21. char *string1;
  22. char * string2 = "EL";
  23.  
  24.  
  25. StrCpy(string1, string2);
  26. // while(*string1)
  27. // cout << *string1++;
  28. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,406
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help, Im trying to implement my own strcpy

 
0
  #17
Dec 2nd, 2008
Your version doesn't work because the first parameter Dest needs to be passed by pointer, not by value
  1. #include <iostream>
  2. #include <cstring>
  3. using std::cin;
  4. using std::cout;
  5.  
  6. void StrCpy(char ** Dest , char* Src )
  7. {
  8. int i;
  9. char *tmp = new char[strlen(Src)+1];
  10. for(i = 0 ; *Src!='\0' ; Src++)
  11. tmp[i]=*Src;
  12. tmp[i]='\0';
  13. *Dest = tmp;
  14. for(int i= 0 ; tmp[i]!='\0' ; i++)
  15. cout << tmp[i];
  16. }
  17.  
  18. void main()
  19. {
  20.  
  21. char *string1 = 0;
  22. char * string2 = "EL";
  23.  
  24.  
  25. StrCpy( &string1, string2);
  26. // while(*string1)
  27. // cout << *string1++;
  28. }
Last edited by Ancient Dragon; Dec 2nd, 2008 at 10:55 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 443
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 68
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: Help, Im trying to implement my own strcpy

 
1
  #18
Dec 2nd, 2008
  1. for(i = 0 ; *Src!='\0' ; Src++)
  2. tmp[i]=*Src;

should increment 'i' also. as of now 'i' remains 0 throughout.
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 21
Reputation: namehere05 is an unknown quantity at this point 
Solved Threads: 1
namehere05 namehere05 is offline Offline
Newbie Poster

Re: Help, Im trying to implement my own strcpy

 
0
  #19
Dec 3rd, 2008
It worked wonders, just a lil bit confused why the extra pointer indirection was needed but I think that'll come in the future
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC