|
shrughes posted:Just a heads up, a deque is not necessarily a linked list. It never is a linked list. A Deque requires constant time random access. shrughes posted:Or you can walk two iterators through the vector, a writing iterator and a reading iterator that is always >= the writer. This would not work since erasing invalidates your iterators (Edit: Oh, nm, I just realized you meant copying as you go, not erasing. MutantBlue's answer is still most-likely what Dooey wants. That Turkey Story fucked around with this message at 17:37 on Sep 19, 2010 |
# ? Sep 19, 2010 17:16 |
|
|
# ? Jun 9, 2024 05:15 |
|
MutantBlue posted:Replace that for loop with: Ha Thats even better! I should really learn more about the STL it keeps surprising me with whats it can do.
|
# ? Sep 19, 2010 18:05 |
|
MutantBlue posted:Replace that for loop with: It is worth noting that this still requires copying some elements around. remote_if simple moves the elements to be removed to the back of the vector, and returns the iterator to the first of these elements, basically the order becomes (<good elements>, <bad elements>). Then the erase statement simply reduces the size (but not capacity) of the container to trim the "bad" elements. Of this is probably what you want, but it is useful to understand what is actually going on. unique works in the same way.
|
# ? Sep 19, 2010 18:37 |
|
litghost posted:It is worth noting that this still requires copying some elements around. remote_if simple moves the elements to be removed to the back of the vector, and returns the iterator to the first of these elements, basically the order becomes (<good elements>, <bad elements>). Then the erase statement simply reduces the size (but not capacity) of the container to trim the "bad" elements. Not exactly. The remove_if performs one pass through the container, moving good elements "left" on top of bad elements without altering the relative order of the remaining elements. It is essentially what shrughes was describing. Otherwise you are correct.
|
# ? Sep 19, 2010 18:48 |
|
Ok, this is my first time working with C++ in a while and I thought I had a good understanding of pointers and memory allocation and such but I must be missing something. I have a function that returns a pointer to a struct type, Mesh. My struct type is defined like so: code:
code:
code:
|
# ? Sep 19, 2010 22:19 |
|
Eggnogium posted:The function looks something like this:
|
# ? Sep 19, 2010 22:23 |
|
Brecht posted:How, specifically, do you do the population? code:
Eggnogium fucked around with this message at 22:38 on Sep 19, 2010 |
# ? Sep 19, 2010 22:31 |
|
Seems right to me. Here's a small, self-contained example that works:code:
code:
|
# ? Sep 19, 2010 22:51 |
I'm a programming noob. Can someone give me a quick tutorial on the difference between i++ and ++i?
|
|
# ? Sep 20, 2010 01:29 |
|
A MIRACLE posted:I'm a programming noob. Can someone give me a quick tutorial on the difference between i++ and ++i? i++ returns the old value of i, and ++i returns the new value of i. So int i = 3; int j = i++; will make j contain 3, while int i = 3; int j = ++i; will make j contain 4. And i will contain 4 in both cases.
|
# ? Sep 20, 2010 01:45 |
|
A MIRACLE posted:I'm a programming noob. Can someone give me a quick tutorial on the difference between i++ and ++i? pretend there is no difference and only ever use it on a line by itself
|
# ? Sep 20, 2010 02:20 |
|
kes posted:pretend there is no difference and only ever use it on a line by itself In C++ this can be bad, particularly concerning types that have overloaded ++. Learn the difference and prefer the prefix form when you aren't using the result in an expression since the post-fix semantics often imply having to make a copy that a compiler may not be able to (or be allowed to) optimize away.
|
# ? Sep 20, 2010 02:48 |
|
Just beware of stuff like this:code:
x = 11, y = 9 x++ = 10, y-- = 10
|
# ? Sep 20, 2010 03:12 |
|
Bob Morales posted:Just beware of stuff like this: To expand on what Bob Morales is saying, this code has undefined behavior. In C and C++ when you modify the value of an lvalue in an expression, the value of that lvalue is only defined at the next sequence point. In the above code there are three sequence points:
|
# ? Sep 20, 2010 04:20 |
|
Hey all, I'm at the end of my wits on a program I'm trying to write and I need help. Some background: I'm just starting to learn C, but I have lots of experience programming in other languages, so I'm not a complete beginner. My problem is this: I'm trying to rotate a substring of bits within a bit array by a given amount to either direction. So say I have "10110010" and I want to rotate the substring of bits starting at index 2 (0-indexed from the left) and ending (exclusive) at index 6. If I want to rotate it twice, to the left, then I'd get "10001110". Similarly, if I just want to rotate the entire bit string '1011' 3 times to the right, I'd get "0111". So I understand the problem and I think I have a way to solve it. Here's the way that the certain structures of the code are implemented: code:
code:
So here's my implementation: code:
However, I'm trying to run it on a bitarray that is 98775 bits long, with three rotations done in a row on the entire bitstring. Namely, rotating to the left -98755/4 times (the top number is a size_t, I'm not sure what happens), rotating to the right 98755/4 times and rotating to the right again 98755/2 times. Now while my code works on the smaller bit strings, it seems to work ~too fast on this big bit string, which leads me to believe it's not working properly. I've been working on this for about 2 days straight now and I'm mentally exhausted, but it's unfortunately necessary for a project I'm working on. So where am I going wrong and what can I do to fix it? What cases am I missing out that I'm not testing for, that screws up my algorithm? So sorry for the super long post, but I'm really in need of some help tonight. Thanks.
|
# ? Sep 20, 2010 05:28 |
|
Why are you implementing rotation by using reversals? That seems unnecessarily complex and error-prone for what is actually a pretty simple operation. Anyway, a rotation by one is trivial to implement by grabbing the bit that will be shifted off the end, moving every other element by one, and then sticking the "stored" bit back on the other end. A naive implementation will then repeatedly shift-by-one to get the result - it's slow and easily-improved, yes, but the point is that it works, which means that you can use it to easily test your current implementation to see if it's giving correct results. Once you can easily test whether it gives correct results, it's going to much easier to find any bugs. Also, why are you using that silly xor-swapping technique when you can simply call "set" with the opposite arguments instead?
|
# ? Sep 20, 2010 05:40 |
|
remove_if seems to be leaving one element in the vector when all the elements in the vector should be removed. I am using MutantBlue's method verbatim. Am I doing something wrong?
Dooey fucked around with this message at 07:03 on Sep 20, 2010 |
# ? Sep 20, 2010 06:40 |
I'm finishing a lab for data structures and I'm getting this warning:quote:warning: passing argument 1 of 'strcpy' makes pointer from integer without a cast code:
|
|
# ? Sep 20, 2010 06:48 |
|
A MIRACLE posted:I'm finishing a lab for data structures and I'm getting this warning: If you're looking for a "quick fix" instead of trying to understand what's going wrong, you're looking at this problem the wrong way. First of all, what are you trying to do? I'm guessing it's not "make a copy of a string".
|
# ? Sep 20, 2010 06:54 |
|
Dooey posted:remove_if seems to be leaving one element in the vector when all the elements in the vector should be removed. I am using MutantBlue's method verbatim. Am I doing something wrong? What are you using for the unary predicate in remove_if? This may help. MutantBlue fucked around with this message at 08:09 on Sep 20, 2010 |
# ? Sep 20, 2010 08:03 |
|
I want to create an object, passing in a function to be called later.code:
code:
quote:Error 1 error C2659: '=' : function as left operand Ok...but if I change the protected member to code:
quote:Error 2 error C2064: term does not evaluate to a function taking 1 arguments quote:Expression must have (pointer-to-) function type There are lots of examples of passing a function pointer, or about needing to wrap a member function in a non-member function before passing it to the likes of CreateThread, etc, but I can't find or figure out how to hold on to the callback for later use. epswing fucked around with this message at 18:39 on Sep 20, 2010 |
# ? Sep 20, 2010 18:35 |
|
Function pointer type looks like void (*myDelegate)(const string), member function pointer type looks like void (MyClass::*myDelegate)(const string) and needs to be called like (this->*myDelegate)("derp")
|
# ? Sep 20, 2010 18:44 |
|
This works. Thanks!code:
epswing fucked around with this message at 19:28 on Sep 20, 2010 |
# ? Sep 20, 2010 19:01 |
|
I have a class with a member function that returns an object of the same class the function is in, and I want its subclasses to return an object of the subclass. Can I do that? Example:code:
|
# ? Sep 20, 2010 23:30 |
|
Dooey posted:I have a class with a member function that returns an object of the same class the function is in, and I want its subclasses to return an object of the subclass. Can I do that? Example: You'll have to give a little bit more information since there are different answers depending on your situation. If the example is exactly like what you have shown and you don't want to have to explicitly duplicate code then you need to use templates (or macros if you really wanted). If you want to call the member function from a pointer or reference to the base and have it return an instance of the child then you need to also change your function to return a pointer or reference as opposed to the object directly. A solution to the exact situation you have shown could be: code:
|
# ? Sep 21, 2010 00:05 |
|
The example is almost exactly the situation I have, so I'll give your suggestion a shot. Thanks, I never would have thought of that myself.
|
# ? Sep 21, 2010 00:24 |
|
OK, its not quite the same as the example because func() is a template function, and A is also a subclass of something else. Which is a template class. I figured I could do something like this:code:
The error I am getting is error C2873: 'func_helper<ChildType>::Subset' : symbol cannot be used in a using-declaration and error C2244: 'func_helper<ChildType>::func' : unable to match function definition to an existing declaration definition 'ChildType func_helper::func(T)' existing declarations 'ChildType func_helper<ChildType>::func(T)' Help please?
|
# ? Sep 21, 2010 03:11 |
|
Dooey posted:OK, its not quite the same as the example because func() is a template function, and A is also a subclass of something else. Which is a template class. I figured I could do something like this: Just change those to using func_helper< A < T > >::func; and using func_helper< B >::func; You can't pull in specific instantiations of the function template with using, only the entire template. Also, in the example I gave earlier I made the func_helper bases private on purpose, but you made them public here. It won't affect execution, but you should try to keep any helper bases like that private as they are implementation details that users should not be able to work with directly.
|
# ? Sep 21, 2010 03:26 |
|
Alright thanks.
|
# ? Sep 21, 2010 03:35 |
|
I have some confusion about memory locations in C. Still new to this. If I do something like this, I can go to different slices by doing foo++.code:
|
# ? Sep 22, 2010 19:55 |
|
Like foo = fooStart + 2;?
|
# ? Sep 22, 2010 19:57 |
|
Yeah I thought I did that but I had problems. I'll try again but it's good to know that's the solution! Thanks! edit: Very simple example from a book that I edited just a bit. How the heck does it know that the main's 2nd argument is going to be a keyboard input value? I'm not having any problem with the code. I just don't know how it KNOWS this. I'm using OS X's terminal. The book doesn't go on to explain it. I'm using the command. ./AddressBook example1 example2 code:
LP0 ON FIRE fucked around with this message at 21:18 on Sep 22, 2010 |
# ? Sep 22, 2010 20:04 |
|
main is passed the arguments you type in with your keyboard on the command line (or launched from a script, etc). That's just what C does for main, don't worry too much about it for now. Just know that the first element in argv (argv[0]) is the name the program was invoked with (you can ignore this for now) and the following elements are the following arguments. The addressing you want to do, as mentioned in your other post, is with the [] operator: foo[2] provides access to the third element of the array or the second offset from the beginning.
|
# ? Sep 22, 2010 21:40 |
|
We've got a legacy program in C, currently built under VS2008 using the C compiler. It's composed of several DLLs. It's not particularly well written but it works...mostly. The issue I'm having is that every now and then a call like atan(0.7) will return -1#.IND0. It's not always atan/atan2 that fails, but it seems to always be math.h functions. This is in a trig heavy program; it does a lot of navigation-related geometry and this failure only happens exceptionally rarely. So rarely, in fact, that we can't work out when it started. I've checked for memory corruption using _CRTDBG_CHECK_ALWAYS_DF because when I have a problem this odd it's usually something along those lines. I've also tested for memory leaks. That ruled out corruption from dynamically allocated memory and there aren't many statically allocated arrays. Anyone got any ideas?
|
# ? Sep 24, 2010 15:43 |
|
Does the bug happen on different systems? Maybe one of the CPU cores it's running on has a slightly defective floating point unit which works 99.99999% of the time, but fails that other 0.00001% or maybe you have some bad RAM. Both would account for the rare and intermittent nature of the bug.
|
# ? Sep 24, 2010 17:29 |
|
Mr.Radar posted:Does the bug happen on different systems? Maybe one of the CPU cores it's running on has a slightly defective floating point unit which works 99.99999% of the time, but fails that other 0.00001% or maybe you have some bad RAM. Both would account for the rare and intermittent nature of the bug. It's happening on different machines - we've had it on machines running XP, Vista, and 7. It's not always happening in the same place though.
|
# ? Sep 24, 2010 18:19 |
|
Grazing Occultation posted:Anyone got any ideas? I've got a couple things that come to mind immediately. Are any of the libraries you're linking in overriding MSVC's libraries? Perhaps one that isn't written very well. If so, and assuming you need this overriding, can you verify the code for this particular atan is correct? Was it compiled with the optimizations all the way up? Optimization bugs occasionally crop up and are a pain because they are more difficult to debug in a debugger.
|
# ? Sep 24, 2010 21:24 |
|
DeciusMagnus posted:I've got a couple things that come to mind immediately. Are any of the libraries you're linking in overriding MSVC's libraries? Perhaps one that isn't written very well. If so, and assuming you need this overriding, can you verify the code for this particular atan is correct? Was it compiled with the optimizations all the way up? Optimization bugs occasionally crop up and are a pain because they are more difficult to debug in a debugger. Nope. No one I work with is that clever/prone to shooting themselves in the foot. I think I'm the only one who knows how the compiler options even work!
|
# ? Sep 24, 2010 23:00 |
|
It's pretty hard to determine what the problem is without the source. I know you said you tested for dynamic allocations so the only other place I can thing of is have you checked the type conversions, floats are tricky bitches. Also make sure that the type definitions are correct for the platforms you're using them on.
|
# ? Sep 25, 2010 07:30 |
|
|
# ? Jun 9, 2024 05:15 |
|
Do you have any code or libraries which are using mmx/sse* functions without issuing emms?
|
# ? Sep 25, 2010 10:14 |