What's wrong with this strcpy and this pointer

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

Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: What's wrong with this strcpy and this pointer

 
0
  #21
Feb 20th, 2007
Originally Posted by joeprogrammer View Post

This is a C++ program. So then use C++ memory allocation methods. The OP's method of using new was fine. (Aside: 2 things the OP did do wrong; didn't use delete and there's a memory leak in the loop as I mentioned previously.)...

Yeah, except that getline() requires a C++ string as an argument... that's why the OP used it in the first place.
If this were a C++ program why use strcpy instead of str::swap, swap or std::replace? And if you didn't notice, I mentioned it is an alternative method; I personally don't like to see a lot of commonly used C code in a C++ program, but my opinion alone.

Oh.... if you're referring to the memory leak in the while loop, yes, I noticed you mentioned it. But you neglected to illustrate how to fix the problem - which I figured I do. I don't take credit for it however, just trying to help out.

I do agree that one should avoid using void main().

And while I'm critcizing people's code (sorry, I can't help it!)
Hardly criticizing but rather stating the obvious.

Good luck, LamaBot
Last edited by Lazaro Claiborn; Feb 20th, 2007 at 8:51 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: What's wrong with this strcpy and this pointer

 
0
  #22
Feb 20th, 2007
Originally Posted by Lazaro Claiborn View Post
If this were a C++ program why use strcpy instead of str::swap, swap or std::replace?
Because the OP's instructor required him to use C strings. I already expressed my disgust to this requirement, but it's a requirement that has to be met.

And if you didn't notice, I mentioned it is an alternative method; I personally don't like to see a lot of commonly used C code in a C++ program, but my opinion alone.
It still is a C++ program, and as long as it is, you should try to use C++ functions as much as possible. In some circumstances it's either impossible or impractical to do such a thing, but that did not seem to be the case here.

Oh.... if you're referring to the memory leak in the while loop, yes, I noticed you mentioned it. But you neglected to illustrate how to fix the problem
I explained it better in the previous thread, although it wasn't completely necessary, since it is expected that the OP has the knowledge of linked list traversal. If not, then there's some serious learning to be done.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: What's wrong with this strcpy and this pointer

 
0
  #23
Feb 20th, 2007
Originally Posted by joeprogrammer View Post
Because the OP's instructor required him to use C strings. I already expressed my disgust to this requirement, but it's a requirement that has to be met.
Ok that makes a load of sense.

It still is a C++ program, and as long as it is, you should try to use C++ functions as much as possible. In some circumstances it's either impossible or impractical to do such a thing, but that did not seem to be the case here.
I guess I don't like to use "new" unless declaring a new class instance. I rarely use it for a structure but I thought I'd remind if not inform the OP of the other way. Also, if that were true, then still, why not use the string::swap, swap or string::replace instead? Since it is not the case here, than you can't justify the usage of malloc instead of new.
I explained it better in the previous thread, although it wasn't completely necessary, since it is expected that the OP has the knowledge of linked list traversal. If not, then there's some serious learning to be done.
I admit it you surely did explain way better than I did. One of my down falls I think. Anyway, if the OP has the knowledge of linked lists then he should know how to properly use them, which is why I modified the while loop. If he doesn't, well, you and I know there is indeed serious learning to be done.

LamaBot
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: What's wrong with this strcpy and this pointer

 
0
  #24
Feb 21st, 2007
Well, except that string::replace can only be operated on a C++ string. In other words, you can use it to replace contents of a C++ string from a C string, but not exactly the other way around...

And I guess std::swap works, except that it's a rather crude method of implementing it. What if you want to make 2 identitcal strings, copying the contents of the original into a second one? Well, with swap it does not do that; it swaps them, like its name says. Using swap might work in some cases, like this example, where line is just thrown away. However, it's not the most elegant way of doing it, and I believe that strcpy() still does a better job.

Regardless of the copying methods, there shouldn't even be a need to copy very many C strings. So my feelings extend towards the OP.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: What's wrong with this strcpy and this pointer

 
0
  #25
Feb 21st, 2007
You're right swap and replace are not suitable for this OPs code, that wasn't my point though. If I really would of suggested something it'd probably would of been the following:

  1. eos = line.copy(PCourant->nom, 20, 0);
  2. PCourant->nom[eos] = '\0';

Good luck OP, LamaBot
Last edited by Lazaro Claiborn; Feb 21st, 2007 at 1:48 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: What's wrong with this strcpy and this pointer

 
0
  #26
Feb 22nd, 2007
Oh.... if you're referring to the memory leak in the while loop, yes, I noticed you mentioned it. But you neglected to illustrate how to fix the problem - which I figured I do. I don't take credit for it however, just trying to help out.
You just don't use malloc with c++ period. Unless you want undefined behaviour.
http://www.informit.com/guides/conte...seqNum=33&rl=1

The other points, like using char[] in c++. Well that's just preference, c++ is after all a multiparadigm programming language.


You're right swap and replace are not suitable for this OPs code, that wasn't my point though. If I really would have suggested something it'd probably would have been the following:
And while I'm crit-e-sizing ppls grammer. (sorry, I can't help it!), Tee he he.
Last edited by iamthwee; Feb 22nd, 2007 at 5:37 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,642
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 472
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: What's wrong with this strcpy and this pointer

 
0
  #27
Feb 22nd, 2007
Originally Posted by iamthwee View Post
You just don't use malloc with c++ period. Unless you want undefined behaviour.
That would be only if you use them to allocate memory for non-POD (plain old data). For datatypes like integers, floats etc. you can still use malloc though is much frowned upon in the C++ community.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
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



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC