|
That Turkey Story posted:Think about it like this. An instance of B has certain invariants. Inheritance here is non-virtual meaning that B knows it has its own base A and may rely on that fact to be certain its invariants aren't violated. An instance of D also has certain invariants, but inherits virtually from A. In E, we can't make the assumption that giving access to B's base A from D will not inadvertently allow D to violate B's invariants (or even vice-versa). If B inherited virtually from A, you'd be forming a contract that says "I know that other people share this base and that is okay". Without that virtual, E shouldn't allow D to point into B's A. Ah, yeah, it's pretty obvious when looked at it that way. I was looking at it purely from a "would the mechanism used to implement this break in that case" point of view. Truth be told, the reason I started wondering about this is that a few days ago I realized that I'd never really used virtual inheritance much, and that I had no goddamn idea how to use it beyond the typical "deadly diamond" pattern you see everywhere in books and examples. So I decided I'd just figure it out given the mechanism used by the compiler and see if it broke. It never occurred to me it would be bad software engineering.
|
# ? Feb 25, 2011 19:56 |
|
|
# ? Jun 3, 2024 11:02 |
|
Initialization order would also be really weird. Virtual bases obviously have to be constructed before any class that has that virtual base. As it stands, you get this nice phase distinction where you initialize all the virtual bases in the complete object, then initialize the normal hierarchy. If you could unique virtual bases with non-virtual bases, that first phase would leave the non-virtual hierarchy partially initialized, and you'd have to be able to tell arbitrary constructors not to initialize certain of their base classes. That's a *huge* increase in complexity.
|
# ? Feb 25, 2011 20:52 |
|
Is there any way for me to give an anonymous-namespaced function access to private class members? More generally, how do I create implementation helper functions for classes without cluttering up my header files?
|
# ? Feb 25, 2011 22:58 |
Bunch of options, maybe I'm missing something. 1. Let your header just define a pure abstract class (i.e. an interface), and implement the interface in your source files. Also have the header declare a factory function or class of a kind. 2. Forward-declare a class that will hold the implementation details, and let your public class hold a private pointer to that helper class. 3. Forward declare a class that will hold the implementation details, as just static methods. (A class used as a namespace.) Declare the actual class to have the implementation class as friend. I'm not sure if this is possible. I think the first is the "purest" regarding OOP theory. The second is rather ugly.
|
|
# ? Feb 25, 2011 23:08 |
|
GrumpyDoctor posted:Is there any way for me to give an anonymous-namespaced function access to private class members?
|
# ? Feb 25, 2011 23:22 |
|
GrumpyDoctor posted:Is there any way for me to give an anonymous-namespaced function access to private class members? If you're classes are getting too big and cluttered, you might have some luck by splitting off functionality/data into sub classes.
|
# ? Feb 25, 2011 23:43 |
|
GrumpyDoctor posted:More generally, how do I create implementation helper functions for classes without cluttering up my header files? Sadly, Mustach's answer is pretty much the best you can do, or if you want to take it a step further to avoid passing a bunch of separate parameters: code:
|
# ? Feb 26, 2011 00:06 |
|
There's also this.code:
|
# ? Feb 26, 2011 00:39 |
|
Well, thanks for all these suggestions, but I don't think any of them are more attractive than just sucking it up and using a handful of private functions.
|
# ? Feb 26, 2011 02:31 |
|
Is there anything that can cause the Visual Studio debugger to mysteriously stop being able to display what a struct holds? I can see the address of the struct but I don't have the nice + to expand it any more. It's just a simple struct like: code:
slovach fucked around with this message at 07:49 on Feb 26, 2011 |
# ? Feb 26, 2011 07:39 |
|
I am writing a scientific simulation. I am dealing with a handful of 2D arrays I am using for a grid that I want to scale to a very large size (think 40,000^2 elements, but really as large as possible given available hardware). The thing I have going for me is that they will be sparse a lot of the time, meaning a lot of the entries most of the time will be constant until information propagates through the grid. What I want to do is only store and iterate through elements that are changing/contributing. I am comfortable finding which elements are not changing and skipping over them, but my problem is really with the memory allocation. I have heard of mmap, but I want to be able to tell the system to deallocate elements as well once they stabilize to constant values again. I know there are great libraries for sparse matrices available, but I don't think they really do what I would like. I really just want to be able to iterate through the grid like a 2D array, but be able to tell the system to allocate memory for certain elements or deallocate memory for certain elements. I use C, but something like a quadruply linked list isn't out of the question if I need to move to C++ to implement it. I feel like there has to be a nice way to make my grid smarter about memory; hash tables or something. I just can't find any good information about this, but I'm sure people have done what I would like to do, if I'm not using the right search terms or whatever. Speed is obviously a factor, but I'm way more concerned about shrinking memory requirements at the moment. Sorry this got so long, but I'm at the point where I'm looking anywhere for ideas on this and it's somewhat specific I guess.
|
# ? Feb 26, 2011 09:49 |
|
Unless you're reading/writing the matrix to the disk (a lot), you probably don't want to use mmap. Quadtrees might be useful for implementing sparse grids.
|
# ? Feb 26, 2011 11:04 |
|
What if you split up your data space into fixed size nodes, say 128x128 cells each. So you could represent a 65536x65536 grid with a 512x512 2D array of pointers to nodes. In each node you store a 2D array of cells, plus a counter storing how many of those cells contain data. As you write data into system, you can use that counter to figure out when it's safe to deallocate a node. So, if you're empty value was 0.0f, you could use something like this: code:
(This is just off the top of my head, by the way, I've never done what you describe and there may be much better ways.) Caveat: I haven't compiled/tested the code above, so be warned about that. I really need to install a compiler on my home PC... Edit: For example, I might have done all my bitshifts backwards vv (now fixed) Gerblyn fucked around with this message at 11:16 on Feb 26, 2011 |
# ? Feb 26, 2011 11:08 |
|
slovach posted:I allocate memory for a bunch, the contents are there but the debugger can't pull it up and it's annoying. Something like Star stars[32]; works, Star* stars = (Star*)malloc(gently caress); doesn't. That's because a pointer doesn't know how big an array what it's pointing to is. You can debug-view further entries manually, by typing stars[1] into the viewer, and you might be able to cast it to a stars[32] in the debugger possibly though I haven't tried that.
|
# ? Feb 26, 2011 17:03 |
|
roomforthetuna posted:I think you're saying the malloced version will show stars[0]'s x,y,z but not stars[1]'s etc. right? You can hint the VS debugger with ",<#>". So in the watch "stars,32" will be treated as Star[32] instead of Star[1].
|
# ? Feb 26, 2011 17:33 |
|
Gerblyn posted:What if you split up your data space into fixed size nodes, say 128x128 cells each. So you could represent a 65536x65536 grid with a 512x512 2D array of pointers to nodes. In each node you store a 2D array of cells, plus a counter storing how many of those cells contain data. As you write data into system, you can use that counter to figure out when it's safe to deallocate a node. A simple multiresolution approach like that really depends on the underlying data. The same even goes for a quadtree. When dealing with a huge sparse matrix, it's important to know what exactly you're storing in it. For example, if you're encoding a geometric object and the matrix is just a big tensor that creates laplacians when multiplied by your points (ie. each row contains weight values for a vertex and its neighbors), then a space-partitioning method will still suck up a shitload of memory. In that case you're best off just storing the position of each of these in a simple array of lists (a list for each row), and having specialized matrix operations that deal with sparse rows and columns. It might take longer but you'll likely have an order of magnitude less memory usage. I've found that for a lot of the applications that require huge sparse matrices like deformations of large meshes, you'll run out of memory really really loving fast if you're not careful.
|
# ? Feb 26, 2011 19:35 |
|
SimpleCoax posted:I am writing a scientific simulation. I am dealing with a handful of 2D arrays I am using for a grid that I want to scale to a very large size (think 40,000^2 elements, but really as large as possible given available hardware). The thing I have going for me is that they will be sparse a lot of the time, meaning a lot of the entries most of the time will be constant until information propagates through the grid. You could probably just use a std::map indexed on coordinates for the sparse in-use table, with some sort of "last used" value thrown in there for when to drop it out of the std::map and back to the mmap. The quad tree approach suggested by others might be better if elements frequently start accessing previously-unused elements nearby. std::map is designed for reasonably fast access to sparse data, though, and a little bit of time spend picking up 'idle' elements from the disk wouldn't be too bad. So your "Get(x,y)" function would be approximately: code:
roomforthetuna fucked around with this message at 20:58 on Feb 26, 2011 |
# ? Feb 26, 2011 20:44 |
|
Wow, thanks for the replies gentlemen, and the coded effort Gerblyn. I truly appreciate the help. I'm still trying to absorb these ideas. I am also fine with just being given terms or ideas I should be searching for if anyone else has thoughts on it. I would really like to start off with established methods if I can, as I will be scaling this up with MPI after I (hopefully) shrink the iterations and memory requirements. I just feel like they should be out there, but I'm too dumb to find them. To anyone familiar, I'm just trying to avoid the complexity of implementing adaptive mesh refinement, which would be my ideal choice. I want my grid to adapt over time, but my element sizes can stay static at the moment. I also want to be clear that I don't need to do any matrix operations. I just have a 2D grid, that's 16 variables deep. So I need 128 bytes per element. I feel like it would end up being some sort of list of elements that are structs. Then I would have an extra grid (just some tiny data type) that's either 0 or 1 at each element stating whether or not the element needs memory or it's constant and whether I should do the calculations required for them, or skip them if they will have no effect on their neighbors. It's a hyperbolic wave equation type simulation, so meaningful data will be dense and grow from a certain location, and I will also need to access the neighbors of each element with little effort.
|
# ? Feb 26, 2011 22:13 |
|
I'm trying to debug a method that refuses to return. I've placed cout's and also used the debugger to verify that the final return statement is being reached, but then nothing ever happens and the program hangs. What could cause this sort of thing to happen? Googling the problem hints that somewhere the call stack might be getting currupted. If this ends up being the case, how can this be debugged, would the curruption have to occur somewhere in the current function, or could it have happened anywhere. (Single threaded) Thank you in advance CoC
|
# ? Feb 27, 2011 07:35 |
|
It could happen in any frame after the call, but is most likely in the immediately-called function or something it tail-calls. valgrind is usually a good first pass for memory bugs, but it's got inherent limitations with the stack. If you have a breakpoint immediately before a return that doesn't seem to return, I suggest looking at the stack to see if something's overwritten the return address and, if so, what data it was overwritten with. Often that's a pretty big hint as to what piece of code did the write. An alternative is to use a "step instruction" command, if your debugger has one; in gdb this is "stepi". In all cases, you'll need a pretty sophisticated understanding of call frames and the stack before it makes much sense.
|
# ? Feb 27, 2011 08:05 |
You may be able to set a breakpoint at the entry to the function and then set a data breakpoint on the return address on the stack.
|
|
# ? Feb 27, 2011 08:15 |
|
Do you have any objects in scope with a non-trivial destructor?
|
# ? Feb 27, 2011 08:45 |
|
roomforthetuna posted:I think you're saying the malloced version will show stars[0]'s x,y,z but not stars[1]'s etc. right? No, it doesn't show anything. Not a single thing. Just the address of the malloc'd struct and thats it, no values.
|
# ? Feb 27, 2011 21:44 |
|
slovach posted:No, it doesn't show anything. Not a single thing. Just the address of the malloc'd struct and thats it, no values.
|
# ? Feb 27, 2011 23:25 |
code:
|
|
# ? Feb 28, 2011 18:37 |
|
The elements of argv and string literals both evaluate to char pointers so == will check whether they point at the same location. Since they only point at distinct locations with potentially equal contents, they will never compare equal. Use strcmp(argv[2], "1") == 0 instead of argv[2] == "1", etc.
|
# ? Feb 28, 2011 18:41 |
|
I'm looking for something in C++ that using info in an array, I can write that data to an image (well, write the info of multiple arrays to multiple images), but I can't seem to figure out pngWriter (it crashes with a segmentation fault when I use image.close() giving me some error to do with libpng.dll). I'm basically questioning whether or not what I want can even be done in C++ and should I just look at a different language at this point as after a couple hours of trying to figure this out, I'm out of ideas how to get this to work. edit: After more work, I've got the code: code:
I'm using Code::Blocks 10.5 on Windows 7 if that makes any difference. I've linked up zlib, libpng, freetype up so I don't think there's a problem there, though there might be an issue with libpng as I dunno, I have the wrong version of the .dll file or something, but I'm unsure how I'd check this. Master_Odin fucked around with this message at 02:45 on Mar 1, 2011 |
# ? Mar 1, 2011 01:32 |
|
Master_Odin posted:edit: What line is throwing the fault? Use a breakpoint if you need to find it. Are you sure that the file "too-new.png" exists and is in the correct place? Does image actually have data in it when close() is called?
|
# ? Mar 1, 2011 06:09 |
|
Scinon posted:What line is throwing the fault? Use a breakpoint if you need to find it. Are you sure that the file "too-new.png" exists and is in the correct place? Does image actually have data in it when close() is called? if (png_sig_cmp( (png_bytep) sig, (png_size_t)0, PNG_BYTES_TO_CHECK) Of course, when setting up the check for just libpng, using code:
|
# ? Mar 1, 2011 08:10 |
|
Okay, so as I'm assuming is the usual in this thread, I'm new to C++. I chose it as my first language to attempt a project in because I played with it a little tiny bit in college (think "Hello World") before I joined the military; it seemed a good point to get back to programming since I at least know how to double click it's .exe. I'm trying to create an inventory program for checking tools in and out. Part of the program will be to amend or remove things from an inventory list (which is one giant step I haven't gotten to yet - trying to get the easy things done first). Part of that is a username / password dealio so that just anybody can't go in and add or remove tools. I found this bit o' code http://www.dreamincode.net/code/snippet554.htm that I'm trying to adapt to my causes, but when I simply copy and paste that code into a new project I get an error on my 'cin's. Under all my >>'s I get an "error: no operator ">>" matches these operands". I don't know why and from what I can tell and my little bit of research on file i/o it should work. But it doesn't work. I'd really like to figure out why because that bit of code seems like a good jumping off point for password / username (and general) file referencing.
|
# ? Mar 1, 2011 15:37 |
|
Rahtas posted:Okay, so as I'm assuming is the usual in this thread, I'm new to C++. I chose it as my first language to attempt a project in because I played with it a little tiny bit in college (think "Hello World") before I joined the military; it seemed a good point to get back to programming since I at least know how to double click it's .exe. The istream class (cin is a version of istream) doesn't define a >> for std::string. You need to use getline(cin, <stringname>) to read in the strings.
|
# ? Mar 1, 2011 16:23 |
|
HFX posted:The istream class (cin is a version of istream) doesn't define a >> for std::string. You need to use getline(cin, <stringname>) to read in the strings. you could also write your own overload!
|
# ? Mar 1, 2011 16:31 |
|
Rahtas posted:Okay, so as I'm assuming is the usual in this thread, I'm new to C++. I chose it as my first language to attempt a project in because I played with it a little tiny bit in college (think "Hello World") before I joined the military; it seemed a good point to get back to programming since I at least know how to double click it's .exe. Make sure that you're including <string>. For MSVC at least, the class definition for std::basic_string is included by some other headers, but the overloaded operators, including >>, are only defined in string.
|
# ? Mar 1, 2011 17:58 |
|
TasteMyHouse posted:you could also write your own overload! He could, but I wouldn't throw an almost newbie into extending the standard C++ library. Edit: Changed from STL to standard lib in case it offended anyone. HFX fucked around with this message at 19:11 on Mar 1, 2011 |
# ? Mar 1, 2011 18:56 |
|
I feel like this is an incredibly stupid question, but it's been on my mind for too long now to not ask; Will my tiny, crappy programs eventually eat up all my RAM because I'm not freeing up memory explicitly? I mean, reading about C/C++ you always hear about all these ways to both allocate and free memory, which I don't quite understand (having mostly used Java). Am I right that this is something I should be concerned about when using C/C++? (I feel so for asking this..)
|
# ? Mar 1, 2011 20:15 |
If you allocate memory dynamically (with the new operator, for example) and don't free it, and run for any amount of time, then yes it will stay allocated and eventually you will run out of address space, or memory in really bad situations. If you use auto-allocated objects (i.e. things not allocated with operator new, or other dynamic allocation functions) then those are released when they go out of scope. This function will leak memory: code:
code:
|
|
# ? Mar 1, 2011 20:28 |
|
nielsm posted:With proper usage of RAII, memory management won't be a major headache, however. (But it still requires care in design.) Thanks! What about deconstructors? I've never understood that either (as far as I know, there's nothing like it it Java?)
|
# ? Mar 1, 2011 20:38 |
|
Your Computer posted:Thanks! When an object is created, its constructor is called. When an object is destroyed, its destructor is called. When you're allocating on the stack, this means that an object's destructor is called when the object goes out of scope. When you're dynamically allocating memory (e.g. through "new") then this means that the destructor will be called when you deallocate the object (e.g. through "delete") If the object doesn't have any internal dynamic memory allocation, you don't need to write a destructor, as the compiler will automagically generate one that does what needs to be done. e.g. code:
however, if you do THIS, and dynamically allocate the member objects inside foo: code:
So, this program will loop, allocating three new bars every time and never deallocating them, and you'll quickly run out of memory. What you need to do is write your OWN destructor in the foo class, like... code:
A good rule of thumb for when you need a destructor is the rule of three TasteMyHouse fucked around with this message at 21:11 on Mar 1, 2011 |
# ? Mar 1, 2011 21:09 |
|
HFX posted:The istream class (cin is a version of istream) doesn't define a >> for std::string. edit: Whoah, beaten Mustach fucked around with this message at 13:09 on Mar 2, 2011 |
# ? Mar 2, 2011 01:42 |
|
|
# ? Jun 3, 2024 11:02 |
|
limip posted:Make sure that you're including <string>. For MSVC at least, the class definition for std::basic_string is included by some other headers, but the overloaded operators, including >>, are only defined in string. Thanks! That made it stop vomiting errors on me. TasteMyHouse posted:you could also write your own overload! I don't know if I should be cry, be angry, or just cock my head to the side like a dog that just heard a strange noise. So here's my follow up problem - from what I was able to tell from the code I linked (http://www.dreamincode.net/code/snippet554.htm), other then the person had to write it while driving a bus (based on the spelling), is that as long as someone were to enter a valid username from the list and a valid password it would allow someone in, regardless if a username / password combo corresponded to each other or not. I tried it and instead found the opposite, I can't get in at all. I made the .txt's as the site instructed and gave it a shot and I couldn't get in. Seeing as how I don't have much faith in that code anymore (because a) it didn't work, and b) it seemed to do a couple other screwed up things (what was the point of int num? Just so that the while loop will run indefinitely even though the if statement won't let you in if you've tried more than 3 times?)) I'm willing to try suggestions to fix that code, but I honestly wouldn't mind leaving it alone and shunning it. So is there another source I can look into for a simple username / password list? A lot of the stuff I've found googling around on my own launches into things much beyond me or into code I can't get to work. I'm just looking for something that sounds simple in my mind: someone types in a username, the program looks at a.txt and finds the username, it then looks at the password directly beneath it and tells you if the password you've entered is right or wrong. In my mind these entries are all in one .txt file one right after another, but I acknowledge that doing it a different way may make it much easier in c++. tl;dr - Got a recommendation for some simple password / username file i/o code and/or lesson I could peruse and modify to my own uses? Or can someone offer more guidance to make above linked code work properly?
|
# ? Mar 2, 2011 08:34 |