|
evensevenone posted:isn't that going to cause a memory leak though? That isn't how delete works, I think. As long as you delete something the same way you allocated it you should be fine. Also I did this and had stable memory usage. code:
|
# ? May 8, 2010 06:23 |
|
|
# ? Jun 9, 2024 03:05 |
|
Yeah, virtual destructors are your friends.
|
# ? May 8, 2010 07:34 |
|
That's actually really awesome.
|
# ? May 8, 2010 07:58 |
|
virtual destructors just mean that ~B() gets called instead of ~A(), it will still free sizeof(B) bytes even if you forget to mark it virtual. http://codepad.org/cXr4TnKo This does not leak any memory.
|
# ? May 8, 2010 08:24 |
|
UraniumAnchor posted:This does not leak any memory. code:
|
# ? May 8, 2010 09:15 |
|
Null Pointer posted:Yes, in the same sense that this: Uhh not at all. That trivially leaks memory. delete foo where foo is a B* with a non-virtual destructor only leaks if B has dynamically allocated memory that is expected to be deleted by the destructor
|
# ? May 8, 2010 09:18 |
|
king_kilr posted:Uhh not at all. That trivially leaks memory.
|
# ? May 8, 2010 09:20 |
|
Null Pointer posted:Guess again. the behavior of the code you posted is undefined soooooo...
|
# ? May 8, 2010 10:58 |
|
ShoulderDaemon posted:The size of every allocation as well as destructor information is stored as part of the allocation; the type of a pointer given to delete does not matter, as long as it was allocated with new, it will be correctly deallocated. Worth pointing out that this only works within a class hierarchy with virtual destructors.
|
# ? May 8, 2010 13:21 |
|
Vanadium posted:Worth pointing out that this only works within a class hierarchy with virtual destructors.
|
# ? May 8, 2010 14:08 |
|
Deleting something via a pointer to its baseclass, which does not have a virtual destructor, is undefined behaviour, as is deleting something through a pointer to an entirely different type.
Vanadium fucked around with this message at 15:42 on May 8, 2010 |
# ? May 8, 2010 15:31 |
|
Plorkyeran posted:No, it doesn't. As has already been pointed out several times, virtual/non-virtual destructors have nothing to do with how much memory gets freed (other than that the derived destructor that doesn't get run might have needed to do something). No, he's right, part of what I said was that "destructor information is stored as part of the allocation" and that is only true if the destructor is virtual.
|
# ? May 8, 2010 16:13 |
|
Avenging Dentist posted:realloc almost always frees/mallocs a new block unless the new block is smaller, and then sometimes it gets optimized for that case. On OS X realloc reuses the same block for shrinking, and it will grow in an amortized efficient method (ie not n^2). Whether this is a side effect of the set allocation sizes or a property of realloc I don't know, but I was pleasantly surprised to learn about it. Not sure what the behavior is on other systems. functional fucked around with this message at 01:07 on May 9, 2010 |
# ? May 8, 2010 18:31 |
|
I want to make a method that does finite differencing on any kind of member function that returns say a double, like:code:
I also read this which only served to confuse me further. At some point it suggest using a #define macro which sounds like the ugliest hack I could think of. The bit on functionoids sounds more reasonable, but it seems rather pointless given that the most general base class I can think of would be nothing but a bunch of virtual functions. Precambrian Video Games fucked around with this message at 22:18 on May 8, 2010 |
# ? May 8, 2010 21:58 |
|
The STL has nothing to do with it. You can take the address of an instance method; it's just that instance methods are *not* ordinary functions and can't be passed in as one. The error message suggests that you're also getting the syntax wrong for taking the address of an instance method. It would be helpful to see some code.
|
# ? May 8, 2010 22:30 |
|
Well, here's an example that works:code:
code:
Precambrian Video Games fucked around with this message at 23:21 on May 8, 2010 |
# ? May 8, 2010 23:16 |
|
You could use templates and overloading to do what you want:code:
Mr.Radar fucked around with this message at 23:47 on May 8, 2010 |
# ? May 8, 2010 23:45 |
|
Can anyone see a glaring error with this code? It runs fine in Microsoft VS 2005 if I hit F5 (debugging), but if I try and run it without debugging mode on (Ctrl+F5) it crashes.code:
code:
|
# ? May 10, 2010 02:05 |
|
Oh my god I didn't think anyone actually used Horstmann style braces. Also you're indexing off the end of your array when you read from the first line.
|
# ? May 10, 2010 02:10 |
|
Avenging Dentist posted:Oh my god I didn't think anyone actually used Horstmann style braces. Gah, I forgot to change maxAllocs to 2 in the code I posted. It still crashes with maxAllocs= 2. Is there something wrong with the braces I use? Jose Cuervo fucked around with this message at 02:31 on May 10, 2010 |
# ? May 10, 2010 02:23 |
|
It is the worst brace style I've ever seen (except for "random"). Also your program doesn't crash.
|
# ? May 10, 2010 02:36 |
|
Avenging Dentist posted:It is the worst brace style I've ever seen (except for "random"). Also your program doesn't crash. I have another fstream object(?) declared to write some data to a file. It seems this is the problem. Am I not allowed to have two fstream objects in the same file? The additional code is: code:
|
# ? May 10, 2010 02:44 |
|
Provide a test case with a reproducible error. I can't track down errors that don't exist.
|
# ? May 10, 2010 02:48 |
|
Avenging Dentist posted:Provide a test case with a reproducible error. I can't track down errors that don't exist. My full code is here. The text file is called newresults.txt and contains the data found here. This makes the program crash if I hit Crtl+F5 but not if I only hit F5. Thank you for your help.
|
# ? May 10, 2010 03:01 |
|
Avenging Dentist posted:It is the worst brace style I've ever seen (except for "random"). Also your program doesn't crash. Worse than Whitesmiths and GNU? Surely you jest.
|
# ? May 10, 2010 03:07 |
|
Jose Cuervo posted:My full code is here. You're still writing off the end of the array.
|
# ? May 10, 2010 03:11 |
|
Zakalwe posted:Worse than Whitesmiths and GNU? Surely you jest. Horstmann is like cancer. It's like someone set out to undermine every editor's auto-indentation mechanism.
|
# ? May 10, 2010 03:12 |
|
Zakalwe posted:Worse than Whitesmiths and GNU? Surely you jest. I don't think any of those styles introduce a risk of the code reader entirely missing a line of code at the beginning of the block...
|
# ? May 10, 2010 03:17 |
|
OddObserver posted:I don't think any of those styles introduce a risk of the code reader entirely missing a line of code at the beginning of the block... Can someone explain what is so terrible about the way I have my braces set up? I am not sure how a reader can miss an entire line of code at the beginning of a block. I have not had a formal computer science class, so the way I set up my braces is the way that made the most sense to me. The code I write is generally not read by others unless I am posting for help (which I seem to do quite a lot these days). EDIT: Jeez... seems to be quite a lot of different formatting styles on wikipedia.
|
# ? May 10, 2010 03:30 |
|
Basically the whole world (excluding GNU people who are insane anyway) uses K&R or Allman. Horstmann and a its derivatives are the only ones out there that have arbitrary statements on the same line as a brace. All other styles keep braces either 1) on their own line, or 2) on the line of a statement that precedes/follows a block.
|
# ? May 10, 2010 03:34 |
|
Avenging Dentist posted:Basically the whole world (excluding GNU people who are insane anyway) uses K&R or Allman. Horstmann and a its derivatives are the only ones out there that have arbitrary statements on the same line as a brace. All other styles keep braces either 1) on their own line, or 2) on the line of a statement that precedes/follows a block. Okay. If that is the convention then I will switch to that. Thanks for the help (you were right about me writing off the end of my array), and the useful knowledge about indenting.
|
# ? May 10, 2010 03:41 |
|
It's worth noting that even Horstmann stopped using Horstmann style in favor of Allman style. And on a completely unrelated but similarly so-weird-it's-harmful nature, you really shouldn't pretend your array indexes start at 1 instead of 0. Both of these things are really needlessly confusing for anyone you want to show your code to, even if they don't seem to cause you personally any problems.
|
# ? May 10, 2010 03:46 |
|
Jose Cuervo posted:Can someone explain what is so terrible about the way I have my braces set up? I am not sure how a reader can miss an entire line of code at the beginning of a block. I have not had a formal computer science class, so the way I set up my braces is the way that made the most sense to me. The code I write is generally not read by others unless I am posting for help (which I seem to do quite a lot these days). There was a long argument about brace styles in the Coding Horrors thread recently. I think your style is the only one that wasn't at least mentioned. Most people seem to use either K&R or Allman, except one guy who apparently still uses Whitesmiths. The only thing everyone agreed on is that GNU style is poo poo.
|
# ? May 10, 2010 03:48 |
|
ShoulderDaemon posted:And on a completely unrelated but similarly so-weird-it's-harmful nature, you really shouldn't pretend your array indexes start at 1 instead of 0. It causes me problems as well, so it is something else I am going to try and eliminate from my coding. I am used to coding in Matlab where the indices start at 1, but from now on I can only see myself using C++ so I should used the indices correctly.
|
# ? May 10, 2010 03:57 |
|
Is there a way I can resize a vector without reducing its capacity? If I have an int vector std::vector<int> my_vector; and I want to reserve 100 spots, but only want the size of the vector to be 25 for now, won't doing my_vector.reserve(100); my_vector.resize(25); reduce the vector capacity to 25? I could reserve the memory after the resizing but that just seems like a waste.
|
# ? May 12, 2010 21:43 |
|
Vectors never shrink.
|
# ? May 12, 2010 21:51 |
|
Works for me. Thanks.
|
# ? May 12, 2010 21:55 |
|
Avenging Dentist posted:Vectors never shrink. code:
I realize it's not a common case, but hey.
|
# ? May 13, 2010 05:27 |
|
UraniumAnchor posted:Am I misunderstanding something? Move semantics for STL containers essentially destroy the original container's storage and shunt the new container's storage into it. You can do the same sort of thing with swap in C++03. See the shrink-to-fit idiom: code:
Avenging Dentist fucked around with this message at 05:53 on May 13, 2010 |
# ? May 13, 2010 05:48 |
|
|
# ? Jun 9, 2024 03:05 |
|
Avenging Dentist posted:(Also, I'm pretty sure it's undefined whether clear() destroys the container's storage). Which of course causes compiler vendors to implement it differently. Don't know about GCC today, but for the PS3 clear() meant "delete memory". For the 360/PC, memory was not deleted. Yay for working with undefined behavior while making real-time systems!
|
# ? May 13, 2010 07:39 |