| | |
What's wrong with this strcpy and this pointer
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
•
•
This is a C++ program. So then use C++ memory allocation methods. The OP's method of usingnewwas fine. (Aside: 2 things the OP did do wrong; didn't usedeleteand 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.
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!)
Good luck, LamaBot
Last edited by Lazaro Claiborn; Feb 20th, 2007 at 8:51 pm.
•
•
•
•
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
"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.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
•
•
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.
•
•
•
•
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 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.
LamaBot
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
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.
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.
All my posts may be freely redistributed under the terms of the MIT license.
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:
Good luck OP, LamaBot
C++ Syntax (Toggle Plain Text)
eos = line.copy(PCourant->nom, 20, 0); PCourant->nom[eos] = '\0';
Good luck OP, LamaBot
Last edited by Lazaro Claiborn; Feb 21st, 2007 at 1:48 am.
•
•
•
•
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.
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:
Last edited by iamthwee; Feb 22nd, 2007 at 5:37 pm.
*Voted best profile in the world*
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
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
![]() |
Similar Threads
- problems with pointers (C++)
- help!! about ADT list please (C)
- Errors help (C++)
- total newb - "passing arg 2 of `strcpy' makes pointer from integer without a cast" (C++)
- Array troubles? (C++)
Other Threads in the C++ Forum
- Previous Thread: IO design problem
- Next Thread: Need some help on my program!!!!!!
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph homeworkhelp iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






