943,553 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1969
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Dec 2nd, 2008
0

Re: Help, Im trying to implement my own strcpy

Click to Expand / Collapse  Quote originally posted by StuXYZ ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Dec 2nd, 2008
0

Re: Help, Im trying to implement my own strcpy

Regrettably, it's not ArkM's algorithm. It's K&R code.
Thank you, Ancient Dragon: this code is OK, it copies null byte
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Dec 2nd, 2008
0

Re: Help, Im trying to implement my own strcpy

What about Buffer Overflow?
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006
Dec 2nd, 2008
0

Re: Help, Im trying to implement my own strcpy

Click to Expand / Collapse  Quote originally posted by Laiq Ahmed ...
What about Buffer Overflow?
Look at the requirements: if you forgot strcpy specification I can recall, try to search the target size there
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Dec 2nd, 2008
0

Re: Help, Im trying to implement my own strcpy

Click to Expand / Collapse  Quote originally posted by Laiq Ahmed ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Dec 2nd, 2008
0

wow this thing is rolling!!

I modified the program it looks like this but no doubt theres something wrong as it wont output anything I await your wisdom guys

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 11
Solved Threads: 1
Light Poster
namehere05 is offline Offline
25 posts
since Dec 2008
Dec 2nd, 2008
0

Re: Help, Im trying to implement my own strcpy

Your version doesn't work because the first parameter Dest needs to be passed by pointer, not by value
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Dec 2nd, 2008
1

Re: Help, Im trying to implement my own strcpy

C++ Syntax (Toggle Plain Text)
  1. for(i = 0 ; *Src!='\0' ; Src++)
  2. tmp[i]=*Src;

should increment 'i' also. as of now 'i' remains 0 throughout.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Dec 3rd, 2008
0

Re: Help, Im trying to implement my own strcpy

It worked wonders, just a lil bit confused why the extra pointer indirection was needed but I think that'll come in the future
Reputation Points: 11
Solved Threads: 1
Light Poster
namehere05 is offline Offline
25 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Finding Positon of characters
Next Thread in C++ Forum Timeline: Need experienced debugger will pay.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC