|
rjmccall posted:There's no difference in layout, if that's what you mean. There's a difference in semantics at deletion time, where deleting a single object can require a virtual destructor to be called but deleting an array cannot, but again that's directed by the core division. The reason for that division is efficiency and simplicity. While the committee doesn't guarantee that the standard allocation functions will be implemented in terms of malloc and free, it does want to permit that, which would not be possible if the platform ABI required separate memory pools for single-object allocations vs. array allocations. And if the platform ABI doesn't require that, then it's basically forced to allocate extra memory with every single (non-POD) heap allocation. that makes sense, thanks. I hadn't thought about the problem with virtual destructors between delete and delete[].
|
# ? May 6, 2011 17:56 |
|
|
# ? Jun 1, 2024 21:49 |
|
[quote="Gordon Cole"] I've got a homework assignment that's driving me nuts. I'm implementing a compiler for a custom language that translates to C. This language supports nested functions that can reference variables in their parent functions. From what I understand, the best way to implement this is to pass a static link (a pointer to the stack frame of the enclosing function) to each function, from which you can find the variable you want by applying a constant offset.[/fixed] There are tons of different ways to implement nested functions. There are several key points of language design which influence the choice:
If all variables are captured by reference and the only thing you can do with a reference to a nested function is call it, then your basic choice is between Ugg Boots's suggestion (passing the address of every captured variable to the nested function) and Vanadium's (putting all the captured variables in a struct and passing the address of that to nested functions). Remember that you might have multiple levels of nesting.
|
# ? May 6, 2011 18:13 |
|
Ugg boots posted:Just figure out which variables from the parent function are being used in the nested function and pass references/pointers to all of them when calling the nested function. I guess that would work, though when we were going over the assignment in class the professor specifically mentioned that we should use a static link. Wikipedia and other places online also mention that as being the way nested functions are typically implemented. I'll probably do it this way as a last resort though. pseudorandom name posted:You can't predict anything about the layout of stack variables. The compiler can (and will) place them in any order it feels like, sometimes combining multiple variables into a single stack slot or never allocating any stack space at all. That makes sense. I suppose the solution is to figure out the offset at run time then? rjmccall posted:There are tons of different ways to implement nested functions. There are several key points of language design which influence the choice: Variables are captured by reference and functions aren't first-class objects, so all you can do with a function is call it, and you can never reference a function outside of the scope in which it was declared. I think I'm going to try out figuring out the static link offset at run time for now, and if that doesn't work out I'll go with Ugg Boots's suggestion. Edit: Well it looks like poo poo but it works. woop dizzywhip fucked around with this message at 21:17 on May 6, 2011 |
# ? May 6, 2011 18:47 |
|
code:
|
# ? May 7, 2011 06:01 |
|
You need to separate the template like so:code:
|
# ? May 7, 2011 06:03 |
|
tractor fanatic posted:You need to separate the template like so: Thanks! Hadn't seen the double template syntax before.
|
# ? May 7, 2011 06:09 |
|
I'm curious now if you can explicitly call a template member constructor, like is there any way to get this to work:code:
|
# ? May 7, 2011 06:15 |
|
Nope.
|
# ? May 7, 2011 08:59 |
|
I'm trying to find a uniform random number generator to use for a little program I need to write. I have previous experience with the generic random number generator in C, but when I googled information about it there was some stuff popping up about how it doesn't actually produce uniform values if you want to get values between 0 and 1. I also looked at the Boost library, but I don't have any OO knowledge so all the stuff about templates just flew way over my head. Basically, I need a URNG that I can get values from 0 to 1, does anybody have any suggestions?
|
# ? May 8, 2011 19:37 |
|
Eugenics posted:I'm trying to find a uniform random number generator to use for a little program I need to write. I have previous experience with the generic random number generator in C, but when I googled information about it there was some stuff popping up about how it doesn't actually produce uniform values if you want to get values between 0 and 1. I also looked at the Boost library, but I don't have any OO knowledge so all the stuff about templates just flew way over my head. Just generate a random integer then divide by the maximum number that rand() can output? That should get you a value between 0 and 1 that is uniformly distributed.
|
# ? May 8, 2011 20:02 |
|
In an assignment I had to create a Vector class using arrays and pointers to Student objects (Vector had knowledge of Students). In a new assignment, we have to create a templated Vector class using linked-list, and vector shouldn't have knowledge of Students. In the old assignment we could use Remove(int studentID) to remove Students from the vector; it would iterate through the array of pointers to Students and find the Student with studentID. How would I do that in a generic way? Adding is fine because I set a node to point to a passed-in Student generically. But how do I find a specific Student and remove them from the vector? Having a Remove(T& delObject) seems kind-of stupid because the caller would have to remember objects and defeat the purpose of a vector. Any tips on this?
|
# ? May 8, 2011 22:22 |
|
You can either use a templated "key" type (student ID was the key for your Student class) but that would make your type more like a map, or simply have a Remove(int index).
|
# ? May 8, 2011 22:35 |
|
Sedro posted:You can either use a templated "key" type (student ID was the key for your Student class) but that would make your type more like a map, or simply have a Remove(int index). Yeah I'm trying to wrap my head around implementing Remove(int index) but at some point the caller has to know the index of the Student. So Vector could have a T Get(int index) that returns the object at index in the list. And the caller can figure out if it's the right Student or not?
|
# ? May 8, 2011 22:40 |
|
darkhand posted:Yeah I'm trying to wrap my head around implementing Remove(int index) but at some point the caller has to know the index of the Student. So Vector could have a T Get(int index) that returns the object at index in the list. And the caller can figure out if it's the right Student or not? Doing it this way (caller iterates over the list to find the right one, then removes it) is feasible, yes. But doing it the specific way you describe (through a random-access get method rather than an iterator) isn't very nice, particularly for a linked list implementation.
|
# ? May 8, 2011 22:48 |
|
darkhand posted:Yeah I'm trying to wrap my head around implementing Remove(int index) but at some point the caller has to know the index of the Student. So Vector could have a T Get(int index) that returns the object at index in the list. And the caller can figure out if it's the right Student or not? You could have a class that takes objects of T and then objects of U where "T" is the type contained in your Vector, and U is the key, and then returns a bool indicating if that key matches your object, then pass an instance of this class to a remove function on your vector. So like, if you wanna indentify a student by id, you'd write a class like this code:
TasteMyHouse fucked around with this message at 00:13 on May 9, 2011 |
# ? May 9, 2011 00:08 |
|
Thanks for the help! I'll see what I can do with it.
|
# ? May 9, 2011 00:31 |
|
I'm working on converting a bunch of algorithms from Matlab to C because Matlab is painfully slow at pretty much everything. For loops aren't nearly as inefficient in C as they are in Matlab but all of the Matlab code utilizes lots of linear algebra routines. Would using a package like LAPACK be beneficial for both speed and conversion?
|
# ? May 11, 2011 14:58 |
|
Modern Pragmatist posted:I'm working on converting a bunch of algorithms from Matlab to C because Matlab is painfully slow at pretty much everything. For loops aren't nearly as inefficient in C as they are in Matlab but all of the Matlab code utilizes lots of linear algebra routines. Would using a package like LAPACK be beneficial for both speed and conversion? I'm not a matlab fan by any stretch, but if you've got lots of for loops in your matlab code, you're doing it wrong. That said, if you are converting matlab to C, then yes you should be utilizing BLAS or LAPACK or Atlas or FFTW or whatever already-existing, mature, well-tested numerical libraries are appropriate for your application. For speed and conversion, and most of all, correctness. There's pretty much no chance your 11-sided, elliptically-shaped reinvented wheel will be as robust off the bat...
|
# ? May 11, 2011 15:47 |
|
Modern Pragmatist posted:I'm working on converting a bunch of algorithms from Matlab to C because Matlab is painfully slow at pretty much everything. For loops aren't nearly as inefficient in C as they are in Matlab but all of the Matlab code utilizes lots of linear algebra routines. Would using a package like LAPACK be beneficial for both speed and conversion?
|
# ? May 12, 2011 03:49 |
|
What are good unit testing tools for C++? I have been looking into boost's unit testing framework.
|
# ? May 12, 2011 05:50 |
|
I have been compelled to write some Python wrappers for some code I've been using. This has been slow-going for awhile but I am slowly starting to wrap my head around the Boost Python wrappers. Where I am stuck is how I could wrap bit fields. I actually didn't even know about bit fields until just now. The Boost Python bindings work with references to the properties or functions being wrapped, but one cannot take a reference to a bit field according to what I know of C++. So what could I do to appease the beast if: 1. The class in question gets subclassed later on and is generally tossed around, so I can't just subclass off some bonus methods. 2. I can't change the original source and maintain interoperability. I am hoping theres some Boost Python voodoo to provide something but I wouldn't know what.
|
# ? May 15, 2011 08:04 |
|
pr0metheus posted:What are good unit testing tools for C++? I have been looking into boost's unit testing framework. I like google's googletest.
|
# ? May 16, 2011 12:09 |
|
Rocko Bonaparte posted:I have been compelled to write some Python wrappers for some code I've been using. This has been slow-going for awhile but I am slowly starting to wrap my head around the Boost Python wrappers. Where I am stuck is how I could wrap bit fields. I actually didn't even know about bit fields until just now. The Boost Python bindings work with references to the properties or functions being wrapped, but one cannot take a reference to a bit field according to what I know of C++. Why not just add accessor functions and wrap those?
|
# ? May 16, 2011 12:43 |
|
OddObserver posted:Why not just add accessor functions and wrap those? That being said, I saw they provided a function for adjusting the fields based on enum values, and that saved my butt. However, I had to basically give up when I realized I'd basically have to wrap a huge portion of this library just to do a basic thing, so I am taking a different approach.
|
# ? May 16, 2011 15:22 |
|
I'm a maintainer of a library, which is all written in C++ and the interface is also written in C++. Is there an easy way to generate a pure C library from this? One of my users is planning on spending the summer doing this, but if there's a very straightforward way to do it I'd be interested to know.
|
# ? May 17, 2011 22:44 |
|
Harokey posted:I'm a maintainer of a library, which is all written in C++ and the interface is also written in C++. Is there an easy way to generate a pure C library from this? One of my users is planning on spending the summer doing this, but if there's a very straightforward way to do it I'd be interested to know. Well if all your functions are in a header file, then to let C use them you have to declare it like this: extern "C" { #include "MyCHeader.h" } it will prevent C++ name mangling
|
# ? May 18, 2011 01:27 |
|
Harokey posted:I'm a maintainer of a library, which is all written in C++ and the interface is also written in C++. Is there an easy way to generate a pure C library from this? One of my users is planning on spending the summer doing this, but if there's a very straightforward way to do it I'd be interested to know. At one point SWIG was going to be able to output C but I don't know whatever came of that project. I think it was a Google Summer of Code thing that was never finished.
|
# ? May 18, 2011 02:07 |
|
Melted_Igloo posted:Well if all your functions are in a header file, then to let C use them you have to declare it like this: I don't think you understand what Harokey is asking.
|
# ? May 18, 2011 03:32 |
|
You mean changing C++ code into C code? Im sure you could write a parser to do that, you would have to make class into struct and all functions public also ClassFoo::Bar() into ClassFoo_Bar but you still have the problem where C only allows variable definitions at the beginning of the function You would basically go through all the compiler errors one by one and fix them (or find a tool somewhere on the web if that sounds too hard) Melted_Igloo fucked around with this message at 04:29 on May 18, 2011 |
# ? May 18, 2011 04:21 |
|
What he's looking for is an easy means of wrapping a C++ library with a C interface. I.e. if your library has an interface of code:
code:
code:
Mr.Radar fucked around with this message at 04:48 on May 18, 2011 |
# ? May 18, 2011 04:43 |
|
Take the object file output and demangle the names. Edit: Or, ffs, do it by hand. There's no way it should take all summer.
|
# ? May 18, 2011 04:44 |
|
If you're really clever and your C++ API is simple enough, you can go the COM route and make the C struct expose the vtable directly.
|
# ? May 18, 2011 04:56 |
|
Mr.Radar posted:What he's looking for is an easy means of wrapping a C++ library with a C interface. This is exactly what I want. Hopefully there's a program that can do this automatically. Also, I'd rather some tool to do this so that I don't have to make the changes in two places whenever I add something or change an interface. It'd be ideal if they were automatically generated at build time, maybe by CMake or something. bear shark posted:At one point SWIG was going to be able to output C but I don't know whatever came of that project. I think it was a Google Summer of Code thing that was never finished. I'm aware of swig, but I'm not sure if it could do this or not. In the not so distant future I'd like to toy with the idea of generating python and other bindings. This will actually be quite entertaining as my project is a C++ port of a python project. If swig was able to generate a C library from C++, that would be great. I looked very briefly at it, and didn't see that it was possible, but I didn't look long, and would love to be proven wrong here. Harokey fucked around with this message at 02:46 on May 19, 2011 |
# ? May 19, 2011 02:40 |
|
I was mucking around with C++ and came across something that is not making any sense to me. Could someone explain to me why a value of "zero" is being output: http://codepad.org/nhAwKd6V Is this something to do with std::cout or exponents when used in C++? Or what? This is really making me go . edit: Google solved my problem. I don't know why I never before figured out that "^" is the XOR operator. I'm pretty dense sometimes. Optimus Prime Ribs fucked around with this message at 07:50 on May 20, 2011 |
# ? May 20, 2011 07:46 |
|
^ is bitwise XOR, not exponentiation. y ^ y evaluaties to 0 for all y. C and C++ do not have exponentiation operators, instead there are functions in the standard library (pow, which is defined only for floating point numbers).
|
# ? May 20, 2011 07:51 |
|
I'm having a bit of trouble with templates - they're not something I've used much before. I have this: code:
code:
code:
quote:1>World.obj : error LNK2019: unresolved external symbol "public: struct Tile __thiscall BinaryFile::Read<struct Tile>(void)" (??$Read@UTile@@@BinaryFile@@QAE?AUTile@@XZ) referenced in function "public: bool __thiscall World::Load(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?Load@World@@QAE_NV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
|
# ? May 23, 2011 20:38 |
|
The most likely cause is that you're not including the function template definition in your World source file. Since function templates are not instantiated for particular template types until they're actually used, you can't just include declarations in a header file and leave the implementation for the linker to deal with. The implementation for the particular T you use literally does not exist for the linker to find unless the compiler creates it for you, which it doesn't know how to do if you haven't included the template definition. TLDR: Put definitions of function and class templates in header files.
|
# ? May 23, 2011 20:56 |
|
I've never really thought about that - where does the compiler/linker put the functions generated from a template? If, for example, I have a template defined in header "mytemplate.h" and then I have mytemplate<int> used in both foo.cpp and bar.cpp, which of those object files does mytemplate<int> end up compiled into? Or both? Or some special separate place?
|
# ? May 23, 2011 21:12 |
|
Staggy posted:
roomforthetuna posted:If, for example, I have a template defined in header "mytemplate.h" and then I have mytemplate<int> used in both foo.cpp and bar.cpp, which of those object files does mytemplate<int> end up compiled into? Or both? Or some special separate place?
|
# ? May 23, 2011 21:16 |
|
|
# ? Jun 1, 2024 21:49 |
|
Plorkyeran posted:Both. The linker throws away the extra copies.
|
# ? May 23, 2011 21:44 |