|
KNITS MY FEEDS posted:zmq::message_t msg(sbuf.data(), sizeof(char *) * sbuf.size(), NULL, NULL); This is wrong, presumably sbuf.size() gives you the size of the buffer in chars, not char pointers. I don't know if that'd cause the deserialization to fail, but it might.
|
# ? Apr 9, 2013 11:00 |
|
|
# ? Jun 7, 2024 23:16 |
|
This was how I solved this:code:
|
# ? Apr 9, 2013 11:03 |
|
b0lt posted:typedef goes the other way, it'd be typedef int silly_t. I had the typedef syntax on a post it in my cube for years until someone walked in, saw it, and said "You know typedef has the same argument order as cp, right? It's like you copy the type into the new name." Jersey Mike posted:I've been recommended several books for learning the C programming language, and the two that show up most are "The C Programming Language" and "C Programming:A Modern Approach". Most of the individuals I ask tell me to go with the former, but others tell me the latter is better. Bearing in mind that I am completely new programming, does anyone happen to know which would be better? I think you'd probably learn more from a course than from a book. You might want the book too. The MIT course recommends "The C Programming Language".
|
# ? Apr 9, 2013 16:40 |
|
Dren posted:I had the typedef syntax on a post it in my cube for years until someone walked in, saw it, and said "You know typedef has the same argument order as cp, right? It's like you copy the type into the new name." Personally, I just find it easiest to remember that it's the same as a variable declaration but with typedef in front.
|
# ? Apr 9, 2013 16:52 |
|
Dren posted:I had the typedef syntax on a post it in my cube for years until someone walked in, saw it, and said "You know typedef has the same argument order as cp, right? It's like you copy the type into the new name." Holy poo poo, that's excellent. Thank you for pointing that out. I appreciate that.
|
# ? Apr 9, 2013 19:07 |
|
I think the chief objection people have to "The C Programming Language" (colloquially referred to as "K&R") is that it doesn't cover the latest C standard, C99. It's great for the rest of C but you'll want something to cover the newer stuff as well.
|
# ? Apr 10, 2013 07:57 |
|
Dren posted:I had the typedef syntax on a post it in my cube for years until someone walked in, saw it, and said "You know typedef has the same argument order as cp, right? It's like you copy the type into the new name." typedef oldtype newtype; Just remember "ton".
|
# ? Apr 10, 2013 09:42 |
|
Typedef is just like a regular variable declaration with the word typedef in front. int variable; -> typedef int typename; This particularly applies to function pointers.
|
# ? Apr 10, 2013 09:51 |
|
GrumpyDoctor posted:I think the chief objection people have to "The C Programming Language" (colloquially referred to as "K&R") is that it doesn't cover the latest C standard, C99. It's great for the rest of C but you'll want something to cover the newer stuff as well. As long as it's at least the ANSI C book, I think there shouldn't any issues starting with K&R
|
# ? Apr 10, 2013 10:52 |
|
Suspicious Dish and Edison was a dick Brilliant! Thanks. Maybe i'll suck less at C someday.
|
# ? Apr 10, 2013 13:34 |
|
GrumpyDoctor posted:I think the chief objection people have to "The C Programming Language" (colloquially referred to as "K&R") is that it doesn't cover the latest C standard, C99. It's great for the rest of C but you'll want something to cover the newer stuff as well. C11's the latest C standard.
|
# ? Apr 10, 2013 16:19 |
|
Rothon posted:C11's the latest C standard. Well that's embarrassing, but my point is the same.
|
# ? Apr 10, 2013 17:11 |
|
Is there a good (preferably thread-safe) transactional map structure in any library out there? I found a library called nbds which does what I want, but it's in super rough shape and not actively maintained any more. It wouldn't even build without me fixing the makefile (needed -lpthread moved to the linker stage), which doesn't fill me with confidence, and the transaction support was described as "a toy model". Atomic has transactional maps, but the transactions are implemented directly in the structure rather than thread-local memory. It looks a little cleaner than nbds, but what's on the site is literally the first release. SQLite is going to be way overkill for this and (at a guess) would probably be slower than using Atomic with locks. Paul MaudDib fucked around with this message at 23:39 on Apr 10, 2013 |
# ? Apr 10, 2013 23:36 |
|
This is a hypothetical question since we've already thrown out this code, but I just ran into something in a legacy codebase that looked something like this (not so abstracted, obviously):C++ code:
So what the hell would x and y have been for this call? Is there a standard behavior for this or does it vary by compiler?
|
# ? Apr 10, 2013 23:37 |
|
Ciaphas posted:This is a hypothetical question since we've already thrown out this code, but I just ran into something in a legacy codebase that looked something like this (not so abstracted, obviously): Undefined behavior
|
# ? Apr 10, 2013 23:41 |
|
Ciaphas posted:So what the hell would x and y have been for this call? Is there a standard behavior for this or does it vary by compiler? http://en.cppreference.com/w/cpp/language/eval_order posted:
Edit: or, y'know, what they said. Xerophyte fucked around with this message at 11:53 on Apr 11, 2013 |
# ? Apr 10, 2013 23:46 |
|
Thanks folks, good to know the code was exactly as lovely as I thought it was for once (instead of being a nice esoteric trick or something).
|
# ? Apr 10, 2013 23:50 |
|
Can someone either point me to a resource or just explain the right way to follow the rule of five by using swap idioms? (I barely even know how to describe this poo poo.) I feel like I've almost got it, but I can't figure out how to deal with the fact that it seems like I should be using move assignment to implement swap while using swap to implement move assignment.
|
# ? Apr 11, 2013 00:01 |
|
Tusen Takk posted:This is awesome, thanks man!! I'll take another whack at it tomorrow, but it made a whole lot of things snap I managed to get it working perfectly, thanks guys! C++ code:
|
# ? Apr 11, 2013 00:43 |
|
GrumpyDoctor posted:Can someone either point me to a resource or just explain the right way to follow the rule of five by using swap idioms? (I barely even know how to describe this poo poo.) I feel like I've almost got it, but I can't figure out how to deal with the fact that it seems like I should be using move assignment to implement swap while using swap to implement move assignment. A "rule of 5" isn't really a thing in the way the "rule of 3" is. While having one of copy, copy-assign, or destructor generally means that you should have all 3, the same cannot be said with the addition of move and move-assign. Simply having a copy or a copy-assign or a destructor rarely has any implication at all as to whether or not you should make separate move operations (and having a move constructor generally doesn't even imply that you need a move-assign operator, etc.). The benefits of move operations come primarily from optimization over corresponding copy operations, their simplicity of implementation over their copy counter-parts (proper copy semantics can sometimes be unclear or difficult to implement so it's not worth implementing copy/copy-assign if all you really need in practice is movability), and their ability to almost always easily be noexcept. I'm sure there are more benefits I left out but those are probably the most common. Anyway, swap idioms are more of a C++98 thing than a C++11 thing. They basically exist because C++98 didn't have move semantics. Now that we have move semantics, using swap for implementing things like copy-assign is now done via move-assign instead, in a similar manner. Also keep in mind that the default std::swap implementation uses move operations, so overloading swap often isn't that important if your type is movable.
|
# ? Apr 11, 2013 00:50 |
|
That Turkey Story posted:A "rule of 5" isn't really a thing in the way the "rule of 3" is. While having one of copy, copy-assign, or destructor generally means that you should have all 3, the same cannot be said with the addition of move and move-assign. Simply having a copy or a copy-assign or a destructor rarely has any implication at all as to whether or not you should make separate move operations (and having a move constructor generally doesn't even imply that you need a move-assign operator, etc.). The benefits of move operations come primarily from optimization over corresponding copy operations, their simplicity of implementation over their copy counter-parts (proper copy semantics can sometimes be unclear or difficult to implement so it's not worth implementing copy/copy-assign if all you really need in practice is movability), and their ability to almost always easily be noexcept. I'm sure there are more benefits I left out but those are probably the most common. Thanks for your answer, although its main purpose was to show me that I asked my question wrong. What I really should have just said was where do I go to learn best practices for writing copy and move constructors and assignment operators because I feel like I've seen three or four different ways to do it and I trust this thread more than I do wandering around stackoverflow and random blog posts in what feels like a drunken haze.
|
# ? Apr 11, 2013 00:58 |
Tusen Takk posted:I managed to get it working perfectly, thanks guys! A purely stylistic thing, you're using a strange comment style that could look like a confusion of traditional C style block comments and C++ style line comments. In traditional C, you open a comment block with /* and it then runs until a following */ which might even be on a different line. In C++ you can also use line comments which are opened by // and then run until the end of the line. C99 adopted that style too, so now both are legal in each language. But there doesn't seem to be any point to you using line comments and then putting a // sequence at the end of them, it does nothing.
|
|
# ? Apr 11, 2013 01:00 |
|
nielsm posted:A purely stylistic thing, you're using a strange comment style that could look like a confusion of traditional C style block comments and C++ style line comments. Ah, yeah I guess it is a cross between the two . I think that that's how you make comments in whatever "language" that Rainmeter uses, too, since it seemed really natural to use the //words// thing I'm doing there...
|
# ? Apr 11, 2013 01:12 |
|
Xerophyte posted:The standard guarantees that the arguments will be placed on the stack from left to right I'm curious what you think this means. To be less of an rear end: on pretty much every platform, it is true that, when arguments are passed on the stack, later arguments are passed at higher stack addresses. But this means nothing about anything actually visible to the source language, like the address of the formal parameter variable in the callee. And many platforms do pass some or even many arguments in registers, and it's frequently possible to have one argument on the stack, the next in a register, and then the next back on the stack. rjmccall fucked around with this message at 01:21 on Apr 11, 2013 |
# ? Apr 11, 2013 01:13 |
|
GrumpyDoctor posted:Thanks for your answer, although its main purpose was to show me that I asked my question wrong. What I really should have just said was where do I go to learn best practices for writing copy and move constructors and assignment operators because I feel like I've seen three or four different ways to do it and I trust this thread more than I do wandering around stackoverflow and random blog posts in what feels like a drunken haze. There's not really an authoritative source or a simple "always do this" kind of answer, so you pretty much just have to go based on what makes sense for your particular situation. If you want all 4 operations (copy, move, copy-assign, move-assign) and have the ability to provide move and move-assign operations that are more efficient and/or have more strict guarantees than than their "copy" counterparts, in most cases it's probably "best" to implement move, copy, and move-assign on their own, then implement copy-assign simply in terms of copy and move-assign. This gives you a copy-assign that will be correct and that likely has the most strict exception guarantee possible for the type. The act of creating the move and move-assign operations also implicitly improves the efficiency/guarantees of std::swap. If you've done all of that, manually writing a copy-assign operation without using copy and move-assign, or explicitly implementing a swap operation will most likely not be able to provide any stricter guarantees and would likely not be able to decrease complexity by anything but a constant factor (and depending on the type and your compiler's optimizations there may not be any difference at all). In other words, the only reason to do either of those things would be after profiling. The only other thing I guess I'll stress again is don't use the swap idiom for new types in C++11. Use move semantics directly instead.
|
# ? Apr 11, 2013 02:45 |
|
Oh, std::swap is now written in terms of std::move, isn't it?
|
# ? Apr 11, 2013 02:55 |
|
OddObserver posted:Oh, std::swap is now written in terms of std::move, isn't it? Yes. It moves operand "A" to a temporary, then move-assigns from operand "B" to operand "A", and finally move-assigns from the temporary to operand "B."
|
# ? Apr 11, 2013 02:58 |
|
Is there a simple reason C++14 has the :=: swap operator?
|
# ? Apr 11, 2013 03:35 |
|
MrMoo posted:Is there a simple reason C++14 has the :=: swap operator? It's just been proposed. The rationale makes reference to the copy-swap idiom (though as mentioned earlier, that's unnecessary in modern C++) and to the difficulty in properly calling swap in a generic context. I'm not convinced a new operator is necessary or that it will make it into the next standard.
|
# ? Apr 11, 2013 04:31 |
|
rjmccall posted:I'm curious what you think this means. Since you ask, what I thought it meant was that the standard calling conventions guaranteed that the caller always placed the evaluated arguments on the stack in ascending order without making any guarantees on how those arguments would be evaluated. On bothering to check I realize that isn't the case for cdecl and stdcall even, never mind other conventions and platforms. Thanks for pointing it out, I'd have happily lived on in ignorance otherwise.
|
# ? Apr 11, 2013 11:53 |
|
This is kind of dumb, but what's a good, endianness agnostic way to mask out the least significant bits of an integer? Kind of like a reverse modulo--it should have the result of x - (x%(0xFF)) (where 0xFF depends on how many bits you want to dump) I've been doing this: C code:
C code:
evensevenone fucked around with this message at 15:23 on Apr 11, 2013 |
# ? Apr 11, 2013 15:20 |
|
foo &= 0xff00 should work regardless of endianness. It's not like you have to write integer literals backwards on some architectures.
|
# ? Apr 11, 2013 16:08 |
|
Vanadium posted:foo &= 0xff00 should work regardless of endianness. It's not like you have to write integer literals backwards on some architectures. Right. Remember that 0xFF00 is not some endian-unaware bit pattern even though you may often think about it as one. In C and C++ (and many other languages) it is a just number in hex and is the same as writing the decimal literal 65280. You only really need to worry about endian-ness when you are doing things like saving/loading binary data from files that need to be able to be opened on computers of a different endian-ness, or similarly when sending/receiving data across a network.
|
# ? Apr 11, 2013 16:37 |
|
Yeah, actually that makes perfect sense.
|
# ? Apr 11, 2013 16:49 |
|
I've got code that looks a little like this, part of a class that I'm trying to port from Solaris SunCC 12.1 to Linux gcc 4.6:C++ code:
Anyway, is there something wrong with using strings as a key in an unordered map or something? My only hint is the mention of boost::hash in the exception stack. Ciaphas fucked around with this message at 22:06 on Apr 11, 2013 |
# ? Apr 11, 2013 21:07 |
|
Follow-up post because I've got a guess at the problem I want to run. I found other code that looks like this:C++ code:
(edit) Checked google after posting this and this does, indeed, seem to be the faux pas I thought it was. Well, damnit. Follow-on question applies: what can I do about this without having to refactor a LOT of code to make one of these things non-static? Ciaphas fucked around with this message at 22:06 on Apr 11, 2013 |
# ? Apr 11, 2013 22:04 |
|
Put them in the same file, play games with link order, or use the init_priority attribute.
|
# ? Apr 11, 2013 22:09 |
You can use just-in-time initialisation. Instead of having a class static variable, make a static (or global) function in the class that returns a reference to an instance of the class, and have it return a static variable inside that function. It's not the neatest solution, but it should work. code:
|
|
# ? Apr 11, 2013 22:11 |
|
Ciaphas posted:Follow-up post because I've got a guess at the problem I want to run. I found other code that looks like this: I think you can generally expect consistent behavior but there's no guarantee on initialization order. I'm almost certain that isn't in the spec but I do not have it memorized so I could be wrong. What nielsm mentioned above is what I would likely do.
|
# ? Apr 12, 2013 17:54 |
|
|
# ? Jun 7, 2024 23:16 |
|
nielsm posted:You can use just-in-time initialisation. Instead of having a class static variable, make a static (or global) function in the class that returns a reference to an instance of the class, and have it return a static variable inside that function. Thanks all, this is more or less what I ended up doing. Worked a treat. Got another related problem that I'll address with a short question: is there any problem with doing I/O in a constructor for an object that's being statically initialized? In this specific case the code is using ifstream to open a file and initialize some other stuff, but I'm finding that said other stuff goes uninitialized. (edit) Actually, turns out the problem is different than I thought it was, but still similar to my static initialization problem. C++ code:
Ciaphas fucked around with this message at 20:13 on Apr 12, 2013 |
# ? Apr 12, 2013 19:13 |