|
I'm a beginner at C++, and I'm stuck at a task I'm doing. I've created a class Money that looks like this:code:
|
# ? Sep 30, 2010 12:52 |
|
|
# ? Jun 7, 2024 14:28 |
|
Mopp posted:I'm a beginner at C++, and I'm stuck at a task I'm doing. I've created a class Money that looks like this: cin.getline() [char array strings] and getline() [Object strings] would be your friends here.
|
# ? Sep 30, 2010 13:13 |
|
HFX posted:cin.getline() [char array strings] and getline() [Object strings] would be your friends here. Yeah, I don't get how to use getline correctly. I'm pretty much stuck at this: code:
Am I making this way harder than it is?
|
# ? Sep 30, 2010 18:13 |
|
pseudorandom name posted:Getting the source file and line number information would require interpreting the DWARF information, which is much more involved than just figuring out which dynamic symbol (probably) contains the address. DWARF information isn't even available unless I compile in debug mode, right? (in solaris, CC -g or -g0 as opposed to CC -xO3 for optimized)
|
# ? Sep 30, 2010 18:33 |
|
Common occurrence in Java:code:
I have a C++ function that reads from a pipe (using ReadFile) and returns a std::string (it looks like std::string Read()). When the pipe closes, ReadFile returns false, GetLastError() gives me ERROR_BROKEN_PIPE, that's all well and good. Instinctively (possibly because Java has damaged my brain) I want to return null, but NULL in C++ means a "pointer to NULL" I think, so checking if (s != NULL) isn't going to work. What is The Right Way to handle this? Should it be std::string* Read() (which I can check against NULL)? Edit: or maybe...should my prototype be void Read(std::string& s)? epswing fucked around with this message at 18:38 on Sep 30, 2010 |
# ? Sep 30, 2010 18:35 |
|
epswing posted:Edit: or maybe...should my prototype be void Read(std::string& s)?
|
# ? Sep 30, 2010 18:44 |
|
Yes! Thank you!
|
# ? Sep 30, 2010 18:46 |
|
Another memory leak-fix fiasco has sent me over the edge. It's too late to fix our code by switching to boost::shared_ptrs (and my coworkers would throw a fit because that would be "unreadable" ), but since we're on Solaris I just realized an alternate solution is available: linking to a garbage collector, libgc. I'm reading what I can on this thing, but in the meantime, are there any major gotchas or pitfalls to using a garbage collector in C++ that I need to think about?
|
# ? Sep 30, 2010 18:54 |
|
The collector shipped with Sun Studio is implemented with the Boehm collector, which is well-used. Just test it out first, because this (old) link indicates that it uses an old version of Boehm. If it's been updated, I can't find any info on it. There are also more things to watch out for when using the collector for C++ versus C, but it seems like the Sun library does a decent job of wrapping things up, so maybe you'll be okay.
|
# ? Sep 30, 2010 19:24 |
|
Ledneh posted:DWARF information isn't even available unless I compile in debug mode, right? (in solaris, CC -g or -g0 as opposed to CC -xO3 for optimized) Yes. Although you should be able to build fully optimized with debugging information enabled. (gcc accepts both -O3 and -g at the same time, I don't know about Sun Studio.)
|
# ? Sep 30, 2010 19:38 |
|
pseudorandom name posted:Yes. Although you should be able to build fully optimized with debugging information enabled. (gcc accepts both -O3 and -g at the same time, I don't know about Sun Studio.) That I didn't know, I thought one ran exclusive to the other. Though I guess including debug info would only raise the size of the compile results, now that I actually think about it (I've never tried vOv). Regarding that garbage collector (and please bear with me, I'm new to the whole subject), if an object is leaked then later the GC tries to reclaim that memory, is that leaked object's destructor called, or is the memory straight up freed and drat the consequences?
|
# ? Sep 30, 2010 19:49 |
|
Who knows. Garbage collectors are crazy like that.
|
# ? Sep 30, 2010 19:52 |
|
Mopp posted:Yeah, I don't get how to use getline correctly. I'm pretty much stuck at this: So I kind of solved my problem, but I'm still having difficulties. My function look like this: code:
code:
|
# ? Sep 30, 2010 20:37 |
|
Ledneh posted:Regarding that garbage collector (and please bear with me, I'm new to the whole subject), if an object is leaked then later the GC tries to reclaim that memory, is that leaked object's destructor called, or is the memory straight up freed and drat the consequences?
|
# ? Sep 30, 2010 20:45 |
|
Mopp posted:So I kind of solved my problem, but I'm still having difficulties. My function look like this: The problem is that you're reading "20.50" as two integers back to back. ie. It reads "20", leaving ".50", which won't be read properly as an integer. You probably want to read the 20.50 as a floating point, not an integer - replacing tmpu and tmph with a single variable.
|
# ? Sep 30, 2010 21:24 |
|
Mustach posted:The destructor is automatically called if the class inherits from gc_cleanup. You can also explicitly specify cleanup functions when you call new. More details in the opening comment here. One last (and more general) question, then. Does having a working garbage collector give the users carte blanche to comment out their deletes (and most destructors), since the GC will take care of it later? Or would counting on the GC as a rule cause a gross performance impact? (Or maybe even a performance boost, if destructors don't need to be called anymore? I don't know, I'm talking out of my rear end here.) Ciaphas fucked around with this message at 21:33 on Sep 30, 2010 |
# ? Sep 30, 2010 21:31 |
|
Grazing Occultation posted:The problem is that you're reading "20.50" as two integers back to back. ie. It reads "20", leaving ".50", which won't be read properly as an integer. No, this is an exercise for a course that I'm taking. It requires two different integers. edit: the problem remains, there's an error in the code up there since it manages to enter the if(ispunct(is.peek())), but temph remains 0. Mopp fucked around with this message at 21:44 on Sep 30, 2010 |
# ? Sep 30, 2010 21:39 |
|
Mopp posted:edit: the problem remains, there's an error in the code up there since it manages to enter the if(ispunct(is.peek())), but temph remains 0.
|
# ? Sep 30, 2010 21:51 |
|
Grazing Occultation posted:The problem is that you're reading "20.50" as two integers back to back. ie. It reads "20", leaving ".50", which won't be read properly as an integer. If you treat money as a floating point, I will find you and hurt you.
|
# ? Sep 30, 2010 22:04 |
|
roomforthetuna posted:That's what Grazing Occultation said was happening - it enters that because ".50" "ispunct"[uation], but then you try to get the integer from the string ".50", which isn't an integer, because integers don't begin with a "." - you have to strip that off after the punctuation is matched and before you try to read an integer. well that was something that I missed. It works great now, thanks for the help!
|
# ? Sep 30, 2010 22:10 |
|
Ledneh posted:That I didn't know, I thought one ran exclusive to the other. Though I guess including debug info would only raise the size of the compile results, now that I actually think about it (I've never tried vOv). Depending on circumstances, using optimization and debug together can result in some confusion - things you expect to be examinable being optimized away, confusion about line numbers - but it won't break anything.
|
# ? Sep 30, 2010 22:56 |
|
Ledneh posted:One last (and more general) question, then. Does having a working garbage collector give the users carte blanche to comment out their deletes (and most destructors), since the GC will take care of it later? Or would counting on the GC as a rule cause a gross performance impact? (Or maybe even a performance boost, if destructors don't need to be called anymore? I don't know, I'm talking out of my rear end here.) In most cases it'll have no noticeable impact.
|
# ? Oct 1, 2010 00:48 |
|
king_kilr posted:If you treat money as a floating point, I will find you and hurt you. This needs to be reiterated because it wasn't replied to.
|
# ? Oct 1, 2010 01:24 |
|
I feel dumb. I for the life of me cannot figure out how to get my method of a class template to work. All I'm getting is an unresolved external for my constructor, but I can't tell where I hosed up. code:
code:
|
# ? Oct 1, 2010 01:35 |
|
^^^^^ put your implementation in the header. It can't get instantiated from the .cpp And on an unrelated note: Ugg boots posted:
To elaborate, I am pretty sure you can't accurately represent numbers as simple as 0.10 (e.g. 10 cents) in (binary) floating point.
|
# ? Oct 1, 2010 01:37 |
|
OddObserver posted:To elaborate, I am pretty sure you can't accurately represent numbers as simple as 0.10 (e.g. 10 cents) in (binary) floating point. Maybe not necessarily 100 for some currencies, but whatever, currency can always be stored as an integer number of whatever the smallest unit of currency is. So when England still had half-pennies it could have been output as (money/200) + "." + ((money%200)/2) + ((money%2)?" and a half penny":"") (You'd probably want to use a 64-bit integer, since $43M isn't a very high limit, but 184 quadrillion dollars should work out okay. Or 92 quadrillion if you're allowing negatives.) As an example of floats losing precision at higher numbers: code:
code:
Edit: Also, if you add one or two pennies to a million, it's just flat lost. roomforthetuna fucked around with this message at 02:20 on Oct 1, 2010 |
# ? Oct 1, 2010 02:18 |
|
Another question on the Boehm-Demers-Weiser GC. Is there some magic incantation I have to perform to make datain STL containers traceable (if not collectable)? In my test code so far I'm getting objects deleted prematurely everywhere, seemingly becauseit isn't tracking pointers stored in STL maps.
|
# ? Oct 1, 2010 03:29 |
|
Ledneh posted:Another question on the Boehm-Demers-Weiser GC. Is there some magic incantation I have to perform to make datain STL containers traceable (if not collectable)? In my test code so far I'm getting objects deleted prematurely everywhere, seemingly becauseit isn't tracking pointers stored in STL maps. IIRC, You need to get it to use GC_alloc for memory. Hopefully it provides an allocator...
|
# ? Oct 1, 2010 03:47 |
|
Yes, it provides an allocator. See this link that I linked, under "C++ Interface".
|
# ? Oct 1, 2010 04:14 |
|
Ledneh posted:Another memory leak-fix fiasco has sent me over the edge. It's too late to fix our code by switching to boost::shared_ptrs (and my coworkers would throw a fit because that would be "unreadable" ), but since we're on Solaris I just realized an alternate solution is available: linking to a garbage collector, libgc. Is it possible to override new/delete/malloc/free/etc. so that they capture a callstack (or partial callstack) for every allocation and then have it log what wasn't cleaned up on exit?
|
# ? Oct 1, 2010 04:46 |
|
ehnus posted:Is it possible to override new/delete/malloc/free/etc. so that they capture a callstack (or partial callstack) for every allocation and then have it log what wasn't cleaned up on exit? It goes something like this: code:
I also have a "DebugStart()" function that sets up the recording structure, and "DebugEnd()" that outputs all the stuff, though you could do that with a global object's constructor and destructor instead. The one gotcha I found with this setup is that if you allocate stuff in the constructor of a global object (ie. before 'main') or destroy it in the destructor of a global object (ie. after 'main' ends) then it's not good. (Also, make sure you don't have the #define'd new/malloc being used for allocation within your logging stuff, you don't want it to start logging itself.) roomforthetuna fucked around with this message at 08:10 on Oct 1, 2010 |
# ? Oct 1, 2010 05:47 |
|
I meant in his situation, it's a technique I use fairly often for tracking down memory leaks.
|
# ? Oct 1, 2010 05:50 |
|
Mustach posted:Yes, it provides an allocator. See this link that I linked, under "C++ Interface". Thanks, sorry, my reading comprehension has apparently gone straight to poo poo today. Thanks for the new/delete logging suggestion too, if I can't get the garbage collector functional I'll see if that'll help, I don't see any reason that can't work in our codebase (edit) Is there any template or typedef fuckery I can do to make something like this work? (I know template typedefs can't happen, this is just to demo what I want) code:
Ciaphas fucked around with this message at 08:13 on Oct 1, 2010 |
# ? Oct 1, 2010 08:03 |
|
Ledneh posted:Thanks, sorry, my reading comprehension has apparently gone straight to poo poo today. code:
|
# ? Oct 1, 2010 09:22 |
|
drat it, just when I thought I managed to fix all errors this happens. I'm still using my money class, and I'm writing a program to test it's functionality. However, it returns a seg fault. I know that the "=" operator works fine since I can construct Money M("USD, 20, 50), Money M2, and then use M2=M. Money has three parameters, string curr, int units and int hundreds. The problem is when I try to set Money M3 = M + M2. Here is the code: code:
code:
|
# ? Oct 1, 2010 15:04 |
|
king_kilr posted:If you treat money as a floating point, I will find you and hurt you. Hahaha I don't deal with money at all. I'm sure I do more than a few things that would make you want to hurt me anyway though. vv (Any suggestion on good resources describing pitfalls and how to handle currency properly? Now I'm curious.)
|
# ? Oct 1, 2010 15:38 |
|
Grazing Occultation posted:Hahaha I don't deal with money at all. I'm sure I do more than a few things that would make you want to hurt me anyway though. vv "don't use a datatype that will make your clients' money randomly disappear" is pretty much it
|
# ? Oct 1, 2010 15:41 |
|
roomforthetuna posted:Not sure if you mean this question as "in his situation" or "in general". In general, yes, it is certainly possibly, because I have a little library I use for all my debug code that does a simplistic version of that (as well as doing the same thing for most of the common Windows resource allocation/deallocation pairings, like CreateBitmap etc.) Would I also need to #define delete and delete[] to RFree as you did with free? Ciaphas fucked around with this message at 15:58 on Oct 1, 2010 |
# ? Oct 1, 2010 15:55 |
|
Mopp posted:The program runs fine until return *this;, then it segfaults. Could anyone more experienced with this spot the problem? Implement operator=() like this: code:
code:
Lexical Unit fucked around with this message at 16:34 on Oct 1, 2010 |
# ? Oct 1, 2010 16:11 |
|
|
# ? Jun 7, 2024 14:28 |
|
operator+ doesn't need to be a friend. Also, as usual, there's a Boost for that.
Mustach fucked around with this message at 16:22 on Oct 1, 2010 |
# ? Oct 1, 2010 16:19 |