|
Hammerite posted:I am writing some code which will deal with large-ish (a few thousand members) arrays of small integers (like, no more than 4 digits). Should I use short, or just go with int? I know that short is at least 16 bits, so there is no question of it being big enough, I just wonder if it will be looked at as premature optimisation and/or an ignorant ineffectual attempt to optimise (because I don't know whether it, in fact, optimises anything) If this is for an embedded system, using the smallest type that can hold all required values makes sense. Otherwise, I'd call it premature optimization.
|
# ¿ Apr 8, 2014 18:42 |
|
|
# ¿ Apr 27, 2024 16:35 |
|
Subjunctive posted:I think he's asking about the storage type rather than the index type; the latter is size_t all the way IMO. Actually, the index type in C++ is ptrdiff_t (13.6.13).
|
# ¿ Apr 9, 2014 18:01 |
|
seiken posted:As Otto said, the fact that you don't care about the relative ordering of only red or only blue means this is an unstable sort. The fact that a simple linear-time algorithm exists is because you have exactly two different keys (red and blue). For three or more keys this won't work and you have to use a general n log n algorithm or radix sort or similar. It's somewhat common in parameterised computer science problems that the problem with K=2 is somehow fundamentally "much easier" than the problem for any greater K (see also SAT, graph colouring). I don't know if there's a name for that particular phenomenon, though. You can sort in O(n) for three keys - that's Dijkstra's dutch flag problem.
|
# ¿ Apr 9, 2014 18:13 |
|
Tusen Takk posted:It did This is legal syntax in C/C++: code:
|
# ¿ Apr 15, 2014 17:22 |
|
The Laplace Demon posted:
|
# ¿ Apr 16, 2014 17:15 |
|
Suspicious Dish posted:What if your source file is stored in a character encoding which flat out doesn't have representations for those tokens?
|
# ¿ Apr 17, 2014 18:37 |
|
qntm posted:I'm new to C++. I am using a class whose constructor has a reference parameter. I just discovered that inside the constructor, the class actually takes a pointer to this reference parameter and stores that pointer as a member variable. Just assume that references are pointers that don't require using '&', '*' or '->'. That's how the compiler will treat most of them anyway. References are a syntactic kludge that is needed to allow overloading assignment operators.
|
# ¿ May 7, 2014 18:17 |
|
qntm posted:This is going to make my life extremely hard though C++ is for men, not boys. Insane, masochistic men, but still.
|
# ¿ May 7, 2014 20:54 |
|
Paul MaudDib posted:
That's undefined behavior if 'validation_data_array' is NULL.
|
# ¿ May 10, 2014 12:14 |
|
The Gay Bean posted:I have a custom data class that is formed for and used by a specific algorithm. The data backing it is just a collection of 1D and 2D STL vectors and a cv::Mat (which can be stored using a 2D STL vector). I coded up serialization using boost::serialization because it's easy to read and fairly standard, but it turned out to be slow as poo poo in debug mode - taking several minutes to write / read a 1 GB archive. This is reduced to like 10 seconds in release mode, but for the time being I need to run the code in debug mode a lot, and I can't put up with those kinds of wait times. Can you split the implementation to put the serialization code in a separate source file (that can be compiled in release mode)? (Of course assuming that you don't want to debug that code, specifically, and that debug mode doesn't cause changes in data layout or other incompatibilities that would preclude mixing debug- and release-compiled code.)
|
# ¿ May 19, 2014 11:59 |
|
Newf posted:Are the following two lines of code the same thing? Essentially yes. Some people recommend using the style of the first line. These people are wrong, because syntactically the '*' binds to the identifier, not the type. The following does not declare two pointers: code:
|
# ¿ May 30, 2014 18:04 |
|
Vanadium posted:just use std::add_pointer<int>::type a, b; The Laplace Demon posted:Just use template<typename T> using ptr = T*; See all you fine dandies so proud, so cock-sure, prancin' aboot bein' able to use C++11. I'm still waiting for the day I can finally write auto butt = fart();
|
# ¿ May 31, 2014 11:13 |
|
Newf posted:Here's a more general (more stupid?) question about C(++). Why is it that when I look up something like ArrayList class from Java or .NET I'm brought to comprehensive documentation from the language specifiers (eg, Oracle and Microsoft) but when I look up std::vector I find sites like cplusplus.com? C++ is not owned by a single company; the relevant reference would be the ISO C++ standard. It's not available online (though draft versions are) because it actually costs money. The latest drafts are available at the C++ Standards Committee website. However, I wouldn't recommend using the standard for learning C++ - it's mostly an implementer's reference. Books like The C++ Programming Language are better for that. Sites like the ones you already found are indeed your best bet for online searchable reference material - since it isn't a single-vendor language, there is no one authoritative C++ reference site.
|
# ¿ Jun 5, 2014 18:50 |
|
Hammerite posted:An earlier version of the method did something like this: I would probably rewrite that to something like code:
|
# ¿ Jun 8, 2014 10:41 |
|
Xeom posted:I am trying to understand some of the differences between the different constant types when defining a function in a class. Lets assume all this code is written within a class definition. Please let me know if I have misunderstood anything, I have a feeling I got something wrong. The basic thing to understand is the difference between a constant pointer and a pointer to a constant. code:
Two more traps for the unwary: code:
Zopotantor fucked around with this message at 08:57 on Jul 20, 2014 |
# ¿ Jul 20, 2014 08:54 |
|
PS. Love the cabin posted:Is there a way to call functions at an address including passing arguments without having to write a function prototype for each one? You know how C/C++ lets you shoot yourself in the foot with extreme ease? This library lets you do it with a bazooka. Caveat emptor.
|
# ¿ Aug 11, 2014 17:23 |
|
eXXon posted:I guess I may as well ask if it's true that there's a potentially meaningful performance difference between ++i and i++ in a for loop, if it's not optimized away at least. I would think any C compiler written in the last 30 years will optimize i++ to ++i when it can (e.g., when the result is ignored). Having said that, I have used at least one compiler that didn't, and to this day I prefer the prefix form. (Alcyon C on the Atari ST. Three passes plus assembler, with all intermediate files stored on (floppy!) disk. But it taught me so much... )
|
# ¿ Aug 13, 2014 18:24 |
|
Jewel posted:STL devs thought they were smart and made << and >> both insertion operators and bitshifting operators at the same time, depending on what the object they're used on's implementation of them is. For numbers it's a bitshift, for iostream/stringstream it's a concat operation, and for filestream it's.. I guess technically concat but more insertion. STL's design decisions are Not Very Good. iostreams are pre-STL. Thank Bjarne for that poo poo; STL was a much needed improvement on prior C++ libraries.
|
# ¿ Oct 19, 2014 13:12 |
|
The Gay Bean posted:Linux client and server: done in milliseconds. You could use Wireshark to see what's actually going on.
|
# ¿ Nov 28, 2014 17:30 |
|
Suspicious Dish posted:strlcpy silently truncates and if your source isn't zero terminated it just starts poking everything. If your source isn't zero terminated you've either already hosed up or you shouldn't be using str* functions anyway.
|
# ¿ Feb 8, 2015 19:35 |
|
Fergus Mac Roich posted:-Wall -pedantic for life. Look at this poser who doesn't use -Werror.
|
# ¿ Feb 12, 2015 23:20 |
|
roomforthetuna posted:gdb is the Linux command-line debugger, it's not much fun but it just about works. It's often easier to use the old-school option than it is to use gdb. There's a GUI shell for gdb called "ddd" which is a bit easier to use. http://www.gnu.org/software/ddd/
|
# ¿ May 3, 2015 12:59 |
|
JawKnee posted:I'm writing a couple of clients and a server for a networking class that requires that the server and clients be able to handle sending/receiving concurrently. This hasn't been a problem for my server, or my TCP client, however I've run into a snag with my UDP client. UDP is a connectionless protocol. The receiver will not get a notification when the sender closes its socket, and recvfrom() will just wait endlessly. The OS X man page of recvfrom() mentions this explicitly: quote:RETURN VALUES
|
# ¿ Jun 4, 2015 08:52 |
|
Xerophyte posted:The basic idea is that you have an untemplated base interface for your data holder that's then implemented by a template class that can hold any type. The any class itself stores a pointer to the base interface. Constructing an any object as well as getting the stored class back out will be templated, but the any class itself wont. The basic scheme is described here. From that link: code:
|
# ¿ Jun 4, 2015 15:46 |
|
Slurps Mad Rips posted:Using a throw in an expression was mentioned in C++11 I believe, otherwise it would be really hard to error out of constexpr functions. Most stuff like string_view must be implement several functions in terms of the ternary with a throw of implementing it in C++11 instead of C++14. The proposal for exceptions in the ARM (Stroustrup & Ellis) already specified throw to be an expression. But it may never have been intended to be used in that way. (I don't think they intended templates to be Turing complete either.)
|
# ¿ Jun 4, 2015 18:16 |
|
FamDav posted:If you think of the expressible types in your type system, bottom is a subtype of all of those types. You can think of it as void, because any function that returns void returns nothing, except if I could actually assign the results of a void function. Actually void is the type that is called 'unit' in functional languages. Void functions return to their caller, but the 'value' they return is not used. Bottom is the conceptual return type of functions that don't return, not even a useless value. C++ doesn't allow the unit value to be passed around, so you can't do this: code:
|
# ¿ Jun 5, 2015 08:26 |
|
Frank Viola posted:I'm trying to write a simple program that converts an entered number from 1 to 100 to roman numerals. We can't use recursion or arrays yet. The code compiles but It only prints "C" (100), regardless of the value entered for n. Why would this happen? The comma operator does not do what you think it does. You need to use && instead. Oh, and your use of "return" also looks suspicious. You may want to replace it with "continue". Zopotantor fucked around with this message at 19:46 on Jun 8, 2015 |
# ¿ Jun 8, 2015 19:43 |
|
JawKnee posted:Post the changes you made, if you change all returns to continue, and change the assignment operators in the if-statement-condition to the equality operator, as well as changing the commas to && (again in the if-condition) then it will work fine, the only time you would be stuck in the loop is if n is being set to something higher than 0 and not decremented to 0 in a reachable statement (I'm guessing you either missed an assignment operator or changed the assignment operator inside the if-block to an equality operator accidentally). Check what happens if n is, e.g., 30.
|
# ¿ Jun 8, 2015 21:05 |
|
Slash posted:I'm surprised no one has attempted to do this as a one-liner yet. Knuth needed 20 lines for it in TeX, so I'm not going to try.
|
# ¿ Jun 9, 2015 18:52 |
|
Subjunctive posted:That's no justification. Justification takes even more code, man.
|
# ¿ Jun 9, 2015 20:29 |
|
DrSunshine posted:Oh! So instead of "ballSet[i]" it'd be like "ballset[ballSet.size()]"? That had not occurred to me! Brilliant! That is undefined behavior (out of bounds reference) if you're using a vector, and if you're lucky it will crash. (If you're unlucky, it will post your banking details to 4chan.)
|
# ¿ Jul 5, 2015 17:53 |
|
Doc Block posted:I have a kinda weird question: why do so many C++ developers make their for loops use prefix increment instead of postfix increment? In C++, the postfix operator has to create a temporary value while the prefix operator can just return a reference to *this. Modern compilers should optimize the temporary away when it is not used, but some of us still don't trust them...
|
# ¿ Jul 22, 2015 17:08 |
|
hackbunny posted:CVS is a positively ancient revision control system. In my first job we used SCCS.
|
# ¿ Sep 5, 2015 22:14 |
|
stochastastic posted:Check your input file for control characters. For instance, repeated backspace control characters would cause something like this, e.g. More likely a CR (\r) if removing a single character fixes it. That might happen when reading in binary mode on Windows.
|
# ¿ Nov 28, 2015 17:01 |
|
Rottbott posted:Every time I think I've finally learned everything about this language...!
|
# ¿ Feb 11, 2016 01:29 |
|
rjmccall posted:Creating a new kernel-scheduled thread is actually a really expensive operation on almost every established operating system, but IIRC that's especially true on Windows. I miss BeOS, where creating threads was so cheap that the general rule of thumb was, "when in doubt, spawn another thread."
|
# ¿ May 13, 2016 18:34 |
|
Plorkyeran posted:You can only partially specialize types and not functions, so to partially-specialize a function you have to make it a non-templated static member of a templated struct. You also have the syntax for partial specialization wrong; it's template<typename T> struct Bar<T, Foo> { ... }; to partially-specialize template<typename T, typename U> struct Bar;. You could try to overload the function and use SFINAE (enable_if etc.) to select which version to use.
|
# ¿ Jun 16, 2016 20:31 |
|
Shy posted:I don't know if that's better suited for the general programming thread but I've been meaning to ask about C++ standard/compiler progress over the last 15-20 years. If I understand it correctly C++11 and newer is mostly supported across all modern compilers and performs well, but let's say 15 years ago would a team choosing it over C for a large project encounter many additional difficulties compared to now? 15 years ago IIRC we already had the STL and (some) compilers that could handle it. 20 years ago you had cfront and whatever lovely class library your vendor supplied (I'm looking at you, HP codelibs ).
|
# ¿ Oct 6, 2016 19:03 |
|
Spatial posted:I want to automatically generate minimal boolean expressions for the control signals coming out of an instruction decoder. The inputs to this algorithm are a table of all instructions and the corresponding control signals they need to set. That's a hard problem. https://en.wikipedia.org/wiki/Espresso_heuristic_logic_minimizer
|
# ¿ Nov 26, 2016 20:51 |
|
|
# ¿ Apr 27, 2024 16:35 |
|
tractor fanatic posted:Can I use CRTP to make mixin constructors? I don't think it's possible without UB but maybe there's hope: For construction, it would be simpler (and completely legal) to do this, I think: code:
|
# ¿ Jan 8, 2017 09:33 |