|
Avenging Dentist posted:I doubt that the Source engine dictates that you use a language feature that does not exist. He's got the x/y problem. "I want to do X. I think Y is a good way to do X, so I'll ask how to do Y rather than ask how to do X." e:
|
# ? Apr 8, 2009 07:09 |
|
|
# ? May 16, 2024 06:45 |
|
While you could set up the new values to be equal to what they would be if included in the original enum, methinks you don't want to do that. I'm willing to bet "base_last" is used for bounds checking or similar activities elsewhere, which means, ideally, your new enums (which would presumably have values equal to existing ones, or be out-of-bounds) would result in warnings and errors, down to the worst case of OHGODDOOM. e: I agree with Otto. But, Otto, what's with the OHGOD siggy?
|
# ? Apr 8, 2009 07:20 |
The engine uses continuations of enums for a bunch of other things in the a.i. system and it works just fine. However, for activities using a different enum breaks this nested macro that I have to use:code:
What is done elsewhere in the code is to declare the non-shared activity as an uninitialized int. Then the compiler doesn't know the value of the non-shared activities, so I can't, say, use a switch statement with different activities as cases. I guess it was foolish of me to expect that there was some magic way to alias one enum onto another, but sometimes it seems like anything is possible with typedefs
|
|
# ? Apr 8, 2009 08:41 |
|
huge sesh posted:
Sure you can, just handle the enum as an int code:
|
# ? Apr 8, 2009 17:13 |
|
I've just been assigned a project in my programming class and my professor tells the class that he is done teaching and everything else that we need to know is in the book. The problem is that the book explains it very poorly.code:
*Sorry about the layout, but it's fixed now. YellowJello fucked around with this message at 20:06 on Apr 8, 2009 |
# ? Apr 8, 2009 20:00 |
|
YellowJello posted:My question is if anyone knows the best way to do code like this. I've tried searching some stuff on Google, but nothing really helpful comes up. This is a straightforward intro programming task. What have you tried? What are you having trouble with? Section B of the assignment gives you a very good skeleton of what your program should do. Why don't you try writing the code for step 1, then the code for step 2, and so on, and ask us a question when you get stuck along the way?
|
# ? Apr 8, 2009 20:13 |
|
if you're trying to interpret the questions, I interpreted them as such: user inputs the size of the dataset, if it's > 50 or < 1, end. make an array of psuedorandom numbers of the size the user specified, the integers have to be <= 5. print the numbers out print the numbers out, sorted print mean, median, mode, variance, and standard deviation break it into functions for printing stuff if you haven't done so already Anything past this, you're on your own.
|
# ? Apr 9, 2009 02:20 |
|
The big questions are: How am I storing my data? When I need to pass my data, what form am I passing it in? What tasks need to happen more than once? What tasks need to happen more than once, but with differences? The wrong answer for the first question is "A set of named integer variables". You're going to want to use an array, a vector, or, if you have some additional reason to do so, a class or struct that contains an array or vector as well as some other stuff. (Maybe you want to put your initial data, and your mean, etc, in one handy package.) The second question comes up when you want to break your code up into functions ("modularize"); you'll need to get info from whatever function you're in, to the one you're calling, and back out again. You probably want to use whatever data type you're already keeping the data in, but maybe not. The last two questions help you figure out what to turn into their own functions, rather than coding multiple times. You also might look up "functional programming" and "encapsulation" for more about the whys and wherefores for doing stuff in this way. When you need to get them ordered, you want to look up sorting algorithms. The ones you're looking for are "Merge Sort" and/or "Quick Sort"; like most of CS, Wikipedia has decent articles on these. Or you could use vectors and just use the standard library sort, but that's boring. Oh! For the numbers, you want: rand() - Produces a random number, between zero and toobigIdontcare. int val = rand() % range; is the typical usage. srand() - seeds the psuedo random number generator. If you don't seed, every time you run your program, you'll get the exact same "random" numbers (which can be useful for testing). The typical trick is to do srand(time(NULL)); to use time(), include "time.h".
|
# ? Apr 9, 2009 05:17 |
|
I am wondering if someone can gutcheck me and make sure I'm not doing something incredibly stupid here.code:
The problem is with the destructor. If the class is destroyed when you're inside the doSomethingElse() call, you seg fault. There are a lot of solutions to this problem, but the thing is this piece of code really is performance critical, and every trivial solution I can think of essentially requires a mutex to be checked sometime within doSomething() method. I really need to avoid that performance penalty. The final solution probably involves counters which guarantee atomic operations, one implementation is this: this. On the platforms I care about the specialization ACE_Atomic_Op<ACE_Thread_Mutex,long> is implemented as hand-coded assembly and uses x86's atomic ops and should avoid most of the expense of a Mutex's context switch (can't really avoid using volatile variables though, oh well). I'm not really sure how I would use this though. Something like this is the first thing that came to mind but there has to be some pattern that avoid object creation/destruction on every call. Also I do not think there is a race condition, but I really would like other eyes to see if there might be one I'm missing. code:
|
# ? Apr 9, 2009 07:44 |
|
So, you've got one thread (possibly many threads) making a method call on an object, and a totally different thread which asynchronously tries to delete the object despite the object being used live in the other thread, and you want to solve this by using atomic operations to block the deleter until a particular method call is complete. Magically, there is no danger of the deleter deleting the object before the method call. Do I have this right? There is basically no way to diagnose the possibility of race conditions without some level of actual detail about (1) why a thread is deleting objects being used live by a totally different thread and (2) what constraints all these threads are operating under. Also, there is no overhead for creating and destroying objects here, and I am dismayed at the idea that someone purportedly doing performance work doesn't understand that.
|
# ? Apr 9, 2009 08:08 |
|
I'm also betting that you have done no profiling.
|
# ? Apr 9, 2009 08:13 |
|
I guess the replies mean I am doing something incredibly stupid. As for the overhead comment, well, heh, I don't really have a good excuse other than it's not a good idea coding at 4:00 am. I've done some basic profiling by throwing the mutex in and seeing what happens, execution time goes up by about 15%. I just threw the post together looking for ideas. I don't have access to the dataset I need on this computer to run it through gprof right now and see what's actually happening. As for the rest of rjm's, that pretty much is the scenario, and I have little control over it. The callback method is registered with a library and from that point I have pretty much no control over when it's going to be called. The first thing I do in the actual destructor is deregister the callback so I'm guaranteed that I receive no further calls, but I have to worry about calls that haven't returned yet. The code living outside the destructor has already been pretty throughly vetted to be thread safe. It's just that code didn't clean up after itself properly, but since the lifetime of the object was pretty much the same as the application this resource leak wasn't really a high priority. I want to use it somewhere else though where it will be created/destroyed dynamically, which means I pretty much have to fix it. Chuu fucked around with this message at 10:50 on Apr 9, 2009 |
# ? Apr 9, 2009 10:21 |
|
Okay, this has got me stumped. I have a class, EA2Skeleton, that contains a public member segmentStore, a standard vector of pointers to another class. The copy constructor has to do some weird stuff with the elements of segmentStore, I've tried to implement this as:code:
code:
|
# ? Apr 9, 2009 14:21 |
|
Use a const_iterator for the const object.
|
# ? Apr 9, 2009 14:30 |
|
I've just started getting back into C++ and I've been coding small console apps with Visual C++ 2008 Express and I keep running into weird linking problems while building my projects. Note that I usually have a header file (.h) and the definitions file (.cpp) for my class then a separate driver file for main. I was testing out program in the driver when I found out that I needed to modify the class. I do a few changes and then I go back to building the project and I get weird runtime errors. I step through the code and notice that the changes I made didn't take. I try a bunch of stuff like closing/opening the program, rebuilding the project, etc but I finally delete all of the project files created by VC++, create a new project adding my file back in, and it finally works. Is this a common problem or am I doing something wrong? Should I be using a difference compiler? Tomed2000 fucked around with this message at 17:23 on Apr 9, 2009 |
# ? Apr 9, 2009 17:16 |
|
You'll have to go into more detail. Give a small example of a header, a .cpp, your main.cpp, and what kind of change you made.
|
# ? Apr 9, 2009 17:25 |
|
Alright. Here are snippets of my files. driver.cpp: code:
code:
code:
I find out later that I actually want to initialize top and bottom to be topInput-1 and topInput-2 so I do a small change to Comparator.cpp: code:
Tomed2000 fucked around with this message at 17:46 on Apr 9, 2009 |
# ? Apr 9, 2009 17:42 |
|
Chuu posted:As for the rest of rjm's, that pretty much is the scenario, and I have little control over it. The callback method is registered with a library and from that point I have pretty much no control over when it's going to be called. The first thing I do in the actual destructor is deregister the callback so I'm guaranteed that I receive no further calls, but I have to worry about calls that haven't returned yet. Okay, this is better but still not quite enough information. For future reference, here's the basic information you should always give about any concurrency question: (1) What are all the threads doing? (2) How many threads are there of each type (one or many)? That is, you need to learn how to describe the problem as opposed to your proposed solution. We cannot possibly judge the solutions without understanding the problem. Let me try to guess, though — you have a pool of worker threads draining a queue, and there's an asynchronous callback that tells you when... nothing else is going to be enqueued? a subsystem has gone down and you shouldn't make calls against it anymore? something else? Are you in charge of draining the queue, or is the queue also managed by some other library? Is SomeClass::doSomething() the only thing that gets done while draining the queue, or is it only one of several possibilities? Are there multiple instances of SomeClass live among the worker threads? Where do they get the reference to this instance of SomeClass that's being deleted, anyway — taken from the queue, fetched from global data, given to them at initialization, something else? Speaking very broadly, I've found it's generally best for shutdown callbacks to simply set flags and let the normal worker threads tear down the objects they're working with — but again, it's very difficult to say without understanding more about how this system works.
|
# ? Apr 9, 2009 18:20 |
|
Tomed2000, I have no idea if this'll fix your linker error (probably not), but does your header file have a sentinel? They look something like this (I don't know if VS has any special skullduggery, this is what I've always done):code:
(edit) I just realized those are snippets, not the whole file. Ignore me. Ciaphas fucked around with this message at 20:32 on Apr 9, 2009 |
# ? Apr 9, 2009 20:27 |
|
Ledneh posted:Tomed2000, I have no idea if this'll fix your linker error (probably not), but does your header file have a sentinel? They look something like this (I don't know if VS has any special skullduggery, this is what I've always done): Ok, question: when constructors are compiled, is the code from any super-class constructors copied into the function, or are the constructors called separately, when instantiating a class?
|
# ? Apr 10, 2009 00:03 |
|
Same thing as any other function — if the source is available, the optimizer has the option of inlining it, but is not required to do so. The semantics don't change either way.
|
# ? Apr 10, 2009 00:09 |
|
rjmccall posted:Same thing as any other function — if the source is available, the optimizer has the option of inlining it, but is not required to do so. The semantics don't change either way. code:
code:
|
# ? Apr 10, 2009 00:15 |
|
ultra-inquisitor posted:Sorry, I don't think that's what I meant. I think rjmccall's answer is exactly accurate. If the compiler has the source of the base class available, it may optionally inline the base constructor into the child constructor, or it may generate a call.
|
# ? Apr 10, 2009 00:19 |
|
ShoulderDaemon posted:I think rjmccall's answer is exactly accurate. If the compiler has the source of the base class available, it may optionally inline the base constructor into the child constructor, or it may generate a call.
|
# ? Apr 10, 2009 00:20 |
|
does anyone know a good way of changing HSB color to RGB without using floating point math?
|
# ? Apr 10, 2009 16:19 |
|
Zaxxon posted:does anyone know a good way of changing HSB color to RGB without using floating point math? Have you tried doing the math with 0-255 values as opposed to 0-1 values? Failing that, you could always use a Rational type to do all the math then at the end convert that to an int in 0-255 (or whatever your scale is) without using any floating point math.
|
# ? Apr 10, 2009 20:06 |
|
What would be the easiest way to print out a float that's constantly updating onto the terminal? I just started writing in C++ and this is pretty much all I know how to do: getProgress just returns a float value. code:
|
# ? Apr 11, 2009 04:35 |
|
drcru posted:What would be the easiest way to print out a float that's constantly updating onto the terminal? Nothing wrong with waht you just wrote apart from the whole "what the hell does 1 mean in this context" thing and the lack of a space or a std::endl. I assume you want to replace the number in your terminal rather than scroll? Not supported in C++. You'll need to use an external terminal aware library like ncurses to do that
|
# ? Apr 11, 2009 04:38 |
|
Thanks for the prompt reply, that's exactly what I want to do. I was afraid that I'd have to use another library but it's better than flooding my screen with 0.000 0.000 0.000 0.000 0.0001 eh? Thanks again.
|
# ? Apr 11, 2009 04:45 |
|
Non-standard and fairly brittle (works in Windows and Linux):code:
|
# ? Apr 11, 2009 04:58 |
|
That seems to have worked too. Thanks.
|
# ? Apr 11, 2009 06:47 |
|
I'm working on project and I've hit a problem I don't know how to solve. I have the following: #include <iostream> #include <vector> using namespace std; typedef vector< vector<double> > A2D; //dynamic 2D array ugly but is useful class TestClass { public: int some_value; A2D some_matrix(0, vector<double> (0,0)); }; int main() { cout<<"hello world"<<endl; } This doesn't compile throwing up a lot of errors saying what amounts to that it doesn't know what A2D is at all
|
# ? Apr 11, 2009 17:37 |
|
Avenging Dentist posted:Non-standard and fairly brittle (works in Windows and Linux): I believe the behaviour of a carriage return written to a terminal is standard according to POSIX. And you can (must!) fix the brittleness by guaranteeing that `whatever' is of constant length. In particular, you'll get wrong output from use like: code:
ctz fucked around with this message at 17:46 on Apr 11, 2009 |
# ? Apr 11, 2009 17:42 |
|
Justinius: That's not how you call member's constructor. Try: code:
|
# ? Apr 11, 2009 17:44 |
|
Justinius posted:A2D some_matrix(0, vector<double> (0,0)); from your typedef don't you already have a vector-in-vector setup? Why are you initializing your 2D vector full of more vectors? As far as I know, it already has vector<double>'s in it and it's like you're already ready to rock with just code:
|
# ? Apr 11, 2009 17:46 |
|
Pooball, you nailed it on the head. I was trying to set a default and that was what was causing that issue. Thanks. Agscala, believe it or not if you want to declare an initialized A2D with say 5 rows and 6 columns set to 0 the declaration looks like this A2D some_mat(5, vector<double> (6, 0)); Thanks for the fast replies everyone.
|
# ? Apr 11, 2009 17:55 |
|
ctz posted:I believe the behaviour of a carriage return written to a terminal is standard according to POSIX. How do I properly format floats or set them to a smaller precision? Right now I'm just trying std::count.precision(3);
|
# ? Apr 11, 2009 21:43 |
|
drcru posted:How do I properly format floats or set them to a smaller precision? And?
|
# ? Apr 11, 2009 21:56 |
|
drcru posted:How do I properly format floats or set them to a smaller precision? <iomanip> and setprecision() I believe
|
# ? Apr 12, 2009 02:43 |
|
|
# ? May 16, 2024 06:45 |
|
drcru posted:How do I properly format floats or set them to a smaller precision? Use printf
|
# ? Apr 12, 2009 06:23 |