|
Why does this work?C++ code:
I can't figure out what to search for, and flailing wildly through the C99 spec just got me even more lost.
|
# ¿ Apr 2, 2013 18:02 |
|
|
# ¿ May 21, 2024 08:00 |
|
Suspicious Dish posted:What if I rewrote your code like this: So the compiler emits code to initialize the global before main runs. In retrospect, I don't know how else that would work. Thanks!
|
# ¿ Apr 2, 2013 19:15 |
|
rjmccall posted:highly informative novel about globals I read this twice and, just to make sure I get it: my little example program's global pointer falls into the second case. It takes the address of something provably in the same dynamic object, so the compiler leaves in the offset and a note asking the loader to relocate the address. Since this is a tiny executable, the only potential hit comes from adding the slide to the offset, but if it was a larger executable or in a dylib there could be disk reads that, in my example program, would be entirely pointless at the time. The compiler emits no initialization code.
|
# ¿ Apr 3, 2013 11:22 |
|
rjmccall posted:Exactly right, assuming your tiny program is a relocatable executable. Some platforms don't support relocatable executables, and on others you have to ask for them manually. On Apple platforms, it's the default; you can verify that an executable is relocatable with otool -hv and checking that one of the flags is PIE (for "position independent executable"). Awesome. Thanks a lot for writing all of that up.
|
# ¿ Apr 3, 2013 18:10 |
|
Morham posted:I have a quick question regarding some logic I have written. It's my first C program, and it's a guess the number game in the console. I wrote the following code to prompt the player if they wanted to play again, and to continually do so until they either typed 'y' (121) or 'n' (110). What's the difference between code:
code:
|
# ¿ May 15, 2013 10:02 |
|
GrumpyDoctor posted:Along these lines, say that - completely hypothetically here - I've been convinced by an internet forum that 1 is superior to 2, but I've got a tiny project (10k loc) all done using style 2 that I'm the only developer on. Is there a good automated way to go through the codebase and switch them, and if so, is doing so worth it? Just switch code over as you come across it in the course of other excursions into the project.
|
# ¿ Jun 10, 2013 07:55 |
|
pseudorandom name posted:HFS+ does, but that caused enough problems that APFS doesn't. And that caused enough problems that APFS now does. I think. https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/APFS_Guide/FAQ/FAQ.html
|
# ¿ Aug 10, 2023 15:28 |
|
I'd like some general way to return values asynchronously in C++. Currently we either park a thread or, occasionally, take a callback when e.g. making an HTTP request. But I'd prefer to return something rather than nest callbacks endlessly. Something like a Future type. Is there anything like that built into the standard library? Should I consider coroutines? Is there a focused lil library that everyone uses? This is recent-ish clang (Android NDK and iOS SDK) and we're on C++20 (I could probably bump that if necessary).
|
# ¿ Oct 4, 2023 22:39 |
|
I got the impression that std::future and std::promise block threads, which I was hoping to avoid. Is there some other way to use them like "when this future's done, run this code"? Coroutines look like what I want, just complicated. I'll give them a shot.
|
# ¿ Oct 4, 2023 23:29 |
|
Yeah I'm more looking for the "here's a chunk of code to run when the future's ready" version. Currently we can end up with dozens of threads all blocked. Possibly unbounded, actually, I'd have to double-check. And there's certainly other ways to stop doing that, I'm hoping this one is the least painful Thanks!
|
# ¿ Oct 5, 2023 01:19 |
|
Yep more async/await. It's a pile of C++ that an Android and an iOS app both call into. That pile of C++ in turn can call back out to the platform for database and network operations. A call sequence might be code:
I'd like to change the URL session, database, and topmost (in this case, logIn()) calls to return futures/be coroutines/something. The URL session or database will complete the future on some arbitrary thread and that's ok with me, they've got their own dispatchers and thread pools, I just don't want to park a thread waiting for them to finish.
|
# ¿ Oct 5, 2023 02:34 |
|
Jabor posted:In Java you have a ListenableFuture that allows you to get a callback on a particular executor when the value becomes available, which means you're not burning an entire os thread for every single future you're waiting on. Plorkyeran posted:std::future is missing then(), which is the thing that actually makes futures interesting and useful and it's kinda pointless as a result. It's relatively easy to roll your own future type which actually is useful. Woops, missed these. "Where is std::future::then()" is the concise way to ask what I'm going for. Seems like coroutines are the closest? The platform barrier is handled by Djinni and it has a future that works at that barrier, but I didn't necessarily want to spread Djinni stuff all throughout the C++ code. It seems like a fairly generic future type with a then() though, so maybe it's the right thing to use.
|
# ¿ Oct 5, 2023 02:44 |
|
then() also returns another future for the result of running the closure passed to then(), so it lets you get away from increasingly indented callbacks. I agree it doesn't look super useful with a single call. And in my case, I have a future type with then() that translates between C++ and Java/Objective-C, so I figure if I can use something similar within C++ it'll be overall easier to follow. (I can't add similarly translatable callbacks without a ton of work.)
|
# ¿ Oct 5, 2023 06:03 |
|
This discussion makes me think of 0mq. Did anyone ever take/use it seriously? I enjoyed reading their docs but never found a use for it.
|
# ¿ Nov 23, 2023 07:29 |
|
As someone who likes to understand wtf is going on, C++ is intensely frustrating. But I've gotten to the place where I'm no longer surprised to learn about a new footgun, I'm just disappointed. So yeah I feel for ya op.
|
# ¿ Feb 17, 2024 19:54 |
|
|
# ¿ May 21, 2024 08:00 |
|
I remember more about the couple hours I messed with bazel than the 20 hours I've messed with cmake. Something about cmake is just perfectly designed to slip right off my brain. Makes zero sense to me. Anyway op cmake is the safe choice. If you try something weird: redo.
|
# ¿ May 11, 2024 17:29 |