|
pseudorandom name posted:Maybe you should compile it and see what happens instead of posting about it on the internet? Fine. It turned out I was having unrelated problems with an initialization but gcc was deciding that pointing around that literal was the best way to tell me.
|
# ? Jun 28, 2010 02:04 |
|
|
# ? Jun 8, 2024 18:15 |
|
Mustach posted:Don't define the template class/functions in a cpp file, put it in SequenceProperty.h. Hmm. Trying this leads to forward declaration errors and 'invalid use of incomplete type 'struct Chain'' errors because it's trying to access the length() member. When I actually include the Chain.h header and remove the forward declaration, then the class isn't even recognised as a valid type at all! When I copy the .cpp file contents (except the #include SequenceProperty.h line) and paste it after the template class definition, I still get forward declaration errors. This is pretty confusing. The Chain class is part of an inheritance tree of related classes, all of which seem to have forward declarations of each other. Is it possible that the order in which they are compiled may have some effect here?
|
# ? Jun 28, 2010 04:02 |
|
xPanda posted:Hmm. Trying this leads to forward declaration errors and 'invalid use of incomplete type 'struct Chain'' errors because it's trying to access the length() member. When I actually include the Chain.h header and remove the forward declaration, then the class isn't even recognised as a valid type at all! When I copy the .cpp file contents (except the #include SequenceProperty.h line) and paste it after the template class definition, I still get forward declaration errors. This is pretty confusing. Can you post the new code you have? I thought I saw some typos in your other posted code, but you might have fixed them by now.
|
# ? Jun 28, 2010 04:52 |
|
Couple of questions. New to C++ so I am trying to be careful with a lot of things. Does anyone use Intel Math Kernal Library for Blas and Lapack operations? I think I implemented everything right, but I have some questions if anyone uses it. As for simpler things. I am using Visual Studio 2010 and the debugger can only show the 1st value of a dynamic array. I googled around and the reasoning is the pointer only points to the first part of the array and how does the debugger know its size. My question is if the debugger doesn't know the size, then how does the program know not to use the other addressed memory for the array. Also, how should I pass dynamic arrays in functions. I have a function that will create a dynamically sized array from an input file. And, then I want to use that array as a constructor argument. code:
Johann Gambolputty fucked around with this message at 05:53 on Jun 28, 2010 |
# ? Jun 28, 2010 05:26 |
|
Johann Gambolputty posted:Also, how should I pass dynamic arrays in functions. You should use vectors and pass them. Johann Gambolputty posted:And, then I want to use that array as a constructor argument. Stop wanting that Johann Gambolputty posted:Is this correct. Is there a better way? It depends on how much knowledge you want to have. First, you don't want to manually manage the size of an array along with the array, because why would you want to do that? So it makes sense just to use a vector (a resizable array) for this sort of thing. Then, you can deep-copy the vector. Or, if the caller is willing to give up ownership of the data, you can .swap the vectors, as commented out below, which means that the vector passed by reference will be emptied and foo2 will be filled with its data (in O(1) time because it just swaps pointers and length information around). Or you could share the data by having foo2 be a vector<double>* -- this way you don't have to manually keep track of the length of the array, but you still have the risk of incorrectly keeping track of object lifetimes. Or you could use a boost::shared_ptr< vector<double> > which will keep a reference count. (Or tr1::shared_ptr? I forget.) (C++0x spoilers) Or you could have the API take advantage of move semantics? I don't know much about that. code:
Edit: quote:Also, is it ok to delete the foo2In variable after I construct the object? I would suggest avoiding raw pointers entirely (avoid having to use "*" anywhere in a type you declare) until you understand the answer to this question. shrughes fucked around with this message at 05:48 on Jun 28, 2010 |
# ? Jun 28, 2010 05:46 |
|
I wouldn't mind using vectors. I just don't know if Intel MKL utilizes them for their BLAS/LAPACK routines. Also, I can't really have too huge of inefficiencies. I will be solving rather large matrices sometimes non-linearly so multiple iterations will be used. I would prefer something that is reasonably fast when passing my variables around. I will try to delve deeper into everything you suggested though. And as for my delete question it leaves a dangling pointer. I figured as much just wanted to make sure.
|
# ? Jun 28, 2010 05:55 |
|
Johann Gambolputty posted:My question is if the debugger doesn't know the size, then how does the program know not to use the other addressed memory for the array.
|
# ? Jun 28, 2010 06:11 |
|
Johann Gambolputty posted:I wouldn't mind using vectors. I just don't know if Intel MKL utilizes them for their BLAS/LAPACK routines. Also, I can't really have too huge of inefficiencies. I will be solving rather large matrices sometimes non-linearly so multiple iterations will be used. I would prefer something that is reasonably fast when passing my variables around. I will try to delve deeper into everything you suggested though. Vectors are implemented internally with a contiguous array, so you should be able to pass &vec[0] and vec.size() wherever MKL expects a double* (or std::complex<double>* or whatever) array and its corresponding size. They're a useful tool in place of arrays, and if you find them useful, good.
|
# ? Jun 28, 2010 06:13 |
|
Plorkyeran posted:It doesn't. Just trying to get a deeper understanding of things. How does the delete operator work if the program can't find out at all how much memory/size the dynamic array is taking up? shrughes posted:Vectors are implemented internally with a contiguous array, so you should be able to pass &vec[0] and vec.size() wherever MKL expects a double* (or std::complex<double>* or whatever) array and its corresponding size. They're a useful tool in place of arrays, and if you find them useful, good. Thanks that most likely will work.
|
# ? Jun 28, 2010 06:25 |
|
Johann Gambolputty posted:How does the delete operator work if the program can't find out at all how much memory/size the dynamic array is taking up? Magic. For primitive types, where the destructor doesn't actually have to do anything, there's no reason the delete operator would need to be able to figure out the size of the array. And so that value might not be stored in any standard location. In general, from the debugger's point of view, your pointer could be pointing to the middle of an array, or to a single element, or to a fixed-size array in a struct or on a stack. It would be for the debugger to recognize the case where your pointer is pointing to to the beginning of a region allocated on the heap, and even then it wouldn't know how large the intended region was, since the expression new double[113] could allocate an array of size 300 if it drat well pleases (since double is a primitive type and destructors don't do anything. For some arbitrary type where the destructor might print out a congratulatory message on stdout, its behavior would have to be distinguishable somehow.
|
# ? Jun 28, 2010 06:39 |
|
shrughes posted:For primitive types, where the destructor doesn't actually have to do anything, there's no reason the delete operator would need to be able to figure out the size of the array. And for other types, the value is stored. Somewhere. For example: code:
And here's the output I get, annotated with comments code:
(1) For std::string, the new[] operator will apparently write the size right in front of the array (on my computer). (2) For double, apparently it doesn't. And the array got allocated right next to the std::string array, with nothing in between! (3) This is the first element of the double array, the double 0.0 is all zero bits. The others look like gobbleygook, of course, unless we displayed them in hexadecimal, in which case we'd see that most of their mantissa consists of zeros. (4) Some uninteresting values after this array.
|
# ? Jun 28, 2010 07:03 |
|
What OS are you running, shrughes? Is it a BSD by any chance?
|
# ? Jun 28, 2010 07:13 |
|
OddObserver posted:What OS are you running, shrughes? Is it a BSD by any chance? Mac OS X Snow Leopard I'm pretty sure something happened with the most recent version of Xcode or gcc or something because ghc has stopped working.
|
# ? Jun 28, 2010 07:15 |
|
Rainbow Pony Deluxe posted:Can you post the new code you have? I thought I saw some typos in your other posted code, but you might have fixed them by now. Sure. SequenceProperty.h code:
The Chain class is pretty ordinary I think, it's a non-template class: code:
code:
|
# ? Jun 28, 2010 07:19 |
|
Plorkyeran posted:It doesn't. I think you're misinterpreting Johann Gambolputty's question. I think what he's asking is how new doesn't return an address that overlaps with an already-allocated chunk of memory and how delete knows "how much" to delete. The answer to that question is that the underlying memory allocator keeps track of all of this in a hidden internal data structure. The debugger doesn't know about this because it can't: pointers can point to a single element or multiple elements, and there's no way to tell. Debuggers can't look at the memory allocator's data structures because while memory allocation is usually handled by the standard runtime library, you can swap out the standard implementation for a custom one if you really want to (Firefox does this, for example, to help combat memory "leaks").
|
# ? Jun 28, 2010 07:48 |
|
It doesn't say it's an unrecognized type, it says it's an incomplete type, which means it's been declared but not yet defined. And yes, this is because your headers have a cyclic depdendency. If Chain.h absolutely requires SequenceProperty.h (or rather, absolutely requires Residue.h which absolutely requires SequenceProperty.h), you might have to abandon the one-class-per-header rule you've apparently been working with, or at least do some funky restructuring (e.g. putting out-of-line method definitions in "more derived" headers).
|
# ? Jun 28, 2010 07:54 |
|
Mr.Radar posted:I think you're misinterpreting Johann Gambolputty's question. I think what he's asking is how new doesn't return an address that overlaps with an already-allocated chunk of memory and how delete knows "how much" to delete. The answer to that question is that the underlying memory allocator keeps track of all of this in a hidden internal data structure. Thanks Mr. Radar precisely what I was asking. I find all this really interesting.
|
# ? Jun 28, 2010 07:55 |
|
shrughes posted:Mac OS X Snow Leopard I see. Looking at it, its memory allocator doesn't store heap information for sufficiently small allocations inline. Neither does FreeBSD's phkmalloc, which has a pretty unorthodox design. Linux/glibc use a very classical design --- it's a derivate of dlmalloc, which is basically an ultra-well engineered version of the algorithms one would find described in TAOCP... So on a 32-bit machine, altering the code in the obvious way, for the string array you'll find something like: p[-2]: 0x49 p[-1]: 0xf p[0]: 0x804a884 p[1]: 0x804a89c p[2]: 0x804a8b4 .... Here, it conceptually allocates (15+1)*4 = 64 bytes for the array plus the non-POD operator[]'s size storage[1]. The 0x49 is 72 + 1, with the 1 being a a bitfield denoting that previous block isn't free, with 72 being some sort of internal size. The difference between 64 and 72 is for 4 bytes of overhead, +4 bytes for... bah, it's too late and I am too tired to figure it out. Anyway, the most important property of such allocators when it comes to this discussion is that: 1) If you have a pointer to the beginning of the block, you can figure out the size easily... But for a non-POD, the pointer won't be to the beginning of the block if new[] was used, due to the hidden size field --- and whether that's the case or not, that can't be determined from just the type. 2) If you have a pointer somewhere in the heap, it is actually possible to determine the size of the block --- but you pretty much have to walk the entire drat heap to figure it out, though there may be some shortcuts based on page management and the like. Fancier designs might actually be able to do it easier, depending on the kind of data structures they use. [1] Interestingly the size is stored in the caller code directly -- both the POD and non-POD versions call _Znaj; and delete[] on a non-POD actually emits an entire loop.
|
# ? Jun 28, 2010 08:11 |
|
Johann Gambolputty posted:As for simpler things. I am using Visual Studio 2010 and the debugger can only show the 1st value of a dynamic array. I googled around and the reasoning is the pointer only points to the first part of the array and how does the debugger know its size. The rest of your original post has been responded to pretty thoroughly, but just fyi, if you want to examine the contents of a dynamically allocated array in VS, add the pointer to the watch window, and add a comma followed by the number of elements you want to look at. E.g. if your pointer is called ptr, and you want to treat it as a 10-element array, then add a watch item called 'ptr,10'. The watch window will then show you 10 elements of your array. If ptr actually only points to an array of 5 elements, then the watch window will still show you 10 elements, but the last 5 will be random stuff in the memory immediately after your 5-element array. You can also perform casts and so-on in your watch window entries as well.
|
# ? Jun 28, 2010 10:53 |
|
I've got an ambiguous overload error that I'd like to figure out how to work around. Here's the sample code: http://codepad.org/vHjbW01E s = f; fails because the compiler doesn't know if I want: string& string::operator=(const string&) or string& string::operator=(const _CharT*) or string& string::operator=(_CharT) What I want is to force selection of string& string::operator=(const string&). But I really don't see any way to do this.
|
# ? Jun 28, 2010 14:34 |
|
Lexical Unit posted:But I really don't see any way to do this.
|
# ? Jun 28, 2010 14:46 |
|
But I really want to
|
# ? Jun 28, 2010 15:21 |
|
I am exploring the STL, looking for a container type like a set, but sequenced, so that I can access elements of its members by it's position. More like an array of pointers, but I should not be able to have two of the same sort. Which one? Or is there one at all?
|
# ? Jun 28, 2010 17:08 |
|
clearly not a horse posted:I am exploring the STL, looking for a container type like a set, but sequenced, so that I can access elements of its members by it's position. More like an array of pointers, but I should not be able to have two of the same sort. Which one? Or is there one at all? There isn't anything like that in the STL because there's no efficient way to implement it. You would either need to do a linear search through an array upon each insert or you would need to store the elements twice: once in a set and once in a vector. You could trivially roll your own using either technique, depending on whether space or time efficiency is more important to you. Edit: I assume by "position" you mean insertion order? If you don't, then there may be something that will do this efficiently, just not in the STL. Mr.Radar fucked around with this message at 17:31 on Jun 28, 2010 |
# ? Jun 28, 2010 17:25 |
|
clearly not a horse posted:I am exploring the STL, looking for a container type like a set, but sequenced, so that I can access elements of its members by it's position. More like an array of pointers, but I should not be able to have two of the same sort. Which one? Or is there one at all? Is there a reason you can't just use an iterator or a pointer to the element rather than an integer position?
|
# ? Jun 28, 2010 17:50 |
|
Mr.Radar posted:There isn't anything like that in the STL because there's no efficient way to implement it.You would either need to do a linear search through an array upon each insert or you would need to store the elements twice: once in a set and once in a vector. You could trivially roll your own using either technique, depending on whether space or time efficiency is more important to you. Uhm, for insertion order, you just cross-link your hash/tree data structure with a doubly-linked list. Very small constant-factor overhead for all set operations, though of course it may be too much if the data structure is extremely hot. Heck, Java even provides one of these in its standard library (LinkedHashSet<E>) If he means position based on a sort order, that's doable, too, by using a balanced tree (or a similar data structure) augmented by size of each subtree. It's O(log n) for every operation, of course, but that's unchanged from having this sort of container anyway, either.
|
# ? Jun 28, 2010 18:18 |
|
This is more of an IDE question than a c++ question, but does anyone know of a way to add a library to Dev-C++?
|
# ? Jun 28, 2010 20:32 |
|
Captain Frigate posted:This is more of an IDE question than a c++ question, but does anyone know of a way to add a library to Dev-C++? Don't use Dev-C++. Seriously. Get Visual C++ Express, or Visual Studio Express, or whatever they're calling it nowadays. Seriously.
|
# ? Jun 28, 2010 21:06 |
|
It really is terrible.
|
# ? Jun 28, 2010 22:49 |
|
shrughes posted:Don't use Dev-C++. Seriously. I was writing a post starting with "How the hell do people keep finding that poo poo, anyway?" but apparently people still keep installing "unstable beta" versions of it (as opposed to unstable release versions), based on a quick search for "free c++ ide".
|
# ? Jun 28, 2010 23:24 |
|
Painless posted:I was writing a post starting with "How the hell do people keep finding that poo poo, anyway?" but apparently people still keep installing "unstable beta" versions of it (as opposed to unstable release versions), based on a quick search for "free c++ ide". For free c++ ide I got Code::Blocks, Netbeans and VS Express, I guess my google is better than your google :p
|
# ? Jun 28, 2010 23:38 |
|
rjmccall posted:It doesn't say it's an unrecognized type, it says it's an incomplete type, which means it's been declared but not yet defined. And yes, this is because your headers have a cyclic depdendency. Yeah this is pretty much the conclusion I came to. Curiously that trace omits an intermediate include in there, which doesn't change anything but I find it odd. I guess if all these related classes require each other they might as well be bundled together, or I can try and find a way to prune some of the includes back to forward declarations. Or something. I wonder why MSVC allows it, though? Thanks for the help! I'm going to go strangle the original author, brb... On the topic of IDEs, I've recently been using Eclipse + CDT on Mac OS X, mostly because I was already using Pydev. Are there any real show-stoppers with this setup, other than a crappy compiler error parser?
|
# ? Jun 29, 2010 01:22 |
|
C++ [temp.res]p7: If a type used in a non-dependent name is incomplete at the point at which a template is defined but is complete at the point at which an instantiation is done, and if the completeness of that type affects whether or not the program is well-formed or affects the semantics of the program, the program is ill-formed; no diagnostic is required. Different compilers rely on this to different extents. MSVC is very permissive and does basically no checking until the template is instantiated. GCC checks some things but not others. Clang is very strict about requiring types to be complete. EDG-based compilers do different things depending on what mode you run them in.
|
# ? Jun 29, 2010 01:58 |
|
shrughes posted:Don't use Dev-C++. Seriously. So how is it done in Visual Studio Express?
|
# ? Jun 29, 2010 13:14 |
|
Assuming the latest version of VS: In the project's settings, add the path to the library's header files to "Additional Include Directories" under C++ Compiler Options, add the path to the library's .lib files to "Additional Library Directories" and the .lib's name to "Additional Dependencies" under Linker Options. If the library is a dll, under "Debugging" add Path=$(Path);Path/To/Your/DLL/Directory to "Environment" or copy the dll to your output directory. Some of the names might be different, I'm going off the top of my head.
|
# ? Jun 29, 2010 13:58 |
|
Mustach posted:Assuming the latest version of VS: In the project's settings, add the path to the library's header files to "Additional Include Directories" under C++ Compiler Options, add the path to the library's .lib files to "Additional Library Directories" and the .lib's name to "Additional Dependencies" under Linker Options. If the library is a dll, under "Debugging" add Path=$(Path);Path/To/Your/DLL/Directory to "Environment" or copy the dll to your output directory. Now what if I don't have an actual project or anything, just a .cpp that I need to compile with a custom library? It doesn't seem to like me doing any kind of debugging without setting up a project or anything? (I'm not very experienced with c++ and probably won't be doing too much with it after this).
|
# ? Jun 29, 2010 14:40 |
|
Captain Frigate posted:Now what if I don't have an actual project or anything, just a .cpp that I need to compile with a custom library? It doesn't seem to like me doing any kind of debugging without setting up a project or anything? (I'm not very experienced with c++ and probably won't be doing too much with it after this). If you're really dead set against creating projects just to compile one-source-file programs, cygwin is always an option.
|
# ? Jun 29, 2010 15:06 |
|
Dijkstracula posted:MVCC might have a command-line frontend, too, but I don't know. code:
Just like before, if the library is a dll, it has to be in the runtime path in order to load it.
|
# ? Jun 29, 2010 15:14 |
|
Dijkstracula posted:In Visual Studio (and most IDEs for that matter), you need to have created a project first. In Unixland you can simply invoke g++ from the command line to compile a single file; MVCC might have a command-line frontend, too, but I don't know. It's not so much that I'm dead set against anything, I just don't know how to do these things. If there's a simple way to make a project that wouldn't mess with the code that I already have that would give me a way to add this library that I need and compile, that's certainly good enough.
|
# ? Jun 29, 2010 15:14 |
|
|
# ? Jun 8, 2024 18:15 |
|
Captain Frigate posted:If there's a simple way to make a project that wouldn't mess with the code that I already have that would give me a way to add this library that I need and compile, that's certainly good enough.
|
# ? Jun 29, 2010 15:17 |