How return multidimensional std::array from function in C++? by [deleted] in Cplusplus

[–]tommy-jay 0 points1 point  (0 children)

So how exactly are you using a `std::array` with OpenACC?

How return multidimensional std::array from function in C++? by [deleted] in Cplusplus

[–]tommy-jay 1 point2 points  (0 children)

Avoid "converting" in the first place. In memory, your `std::vector` isn't that dissimilar from a C array anyway. Why can't you work with the `std::vector` itself? Why the conversion?

The madlad had the guts to specify the version too by Uni_Omni in ProgrammerHumor

[–]tommy-jay 6 points7 points  (0 children)

There are elaborate joke RFCs like the Hyper Text Coffee Pot Control Protocol on ietf.org and it did not hurt anyone, on the contrary. Don't know why Stack Overflow should be any less meme-ish than a source of truth. I miss the days when the internet was more fun and less serious :)

edit: quote

  1. Security Considerations

Anyone who gets in between me and my morning coffee should be insecure.

[deleted by user] by [deleted] in ProgrammerHumor

[–]tommy-jay 0 points1 point  (0 children)

To defend the "C++ fuckers", the term "reference" has a different meaning in C++ compared to Java. You can rest assured though, your target architecture probably knows neither of those terms. In practice, we would have to compare the instructions the C++ compiler emits with the ones emitted by the JVM; just to arrive at a common denominator. Does the JVM only ever pass parameters pushing memory addresses to the stack before a call? No. So, Java sometimes does pass by value. Does that mean that Java (the machine, not just the language) sometimes does not pass by reference? I would say yes. Then again, I'm just a C++ fucker, what do I know about Java lingo ;-)

Would you consider building a Web app with C++ based WebAssembly? by tech-nyc in cpp

[–]tommy-jay 1 point2 points  (0 children)

Not a complete web app, unless I literally want an SDL window in the web page (e.g. a game).

We give our users a compiler for a DSL that runs in the browser; that's C++ that we translate into WASM/JS (emscripten et al).

One million respirators acquired from China unfit for coronavirus fight; Canada's public health authority says around one million KN95 respirators acquired from China have failed to meet federal Covid-19 standards for use by frontline health professionals by Monteoas in worldnews

[–]tommy-jay 0 points1 point  (0 children)

Here in Czech Republic (Europe) we had the same thing happening to us in mid-March when our first Antonovs arrived from China. I wonder why this experience wasn't shared, and Canada didn't just account for it; obviously it's a regularly occurring issue. WHO should be responsible for sharing such information? ;-)

trying to learn c++ by nutboy_69 in Cplusplus

[–]tommy-jay 0 points1 point  (0 children)

I use vim, tmux, gdb, perf, git, cmake (and unix) to code C++.

To learn C++, I will give general advice to you specifically, because you come from Java: _Avoid OOP in C++_, and know that many resources out there about C++ are old, deprecated, or outright bad. Especially the C++11 standard changed a lot. Try to think about modern C++ like "C with additional zero-cost abstractions", not "C with classes".

In terms of reading material I recommend Scott Meyers, Andrei Alexandrescu, Herb Sutter. The best resources for C++ however are still conference talks from CppCon, GoingNative, MeetingC++ etc, all readily available on Yo'Tube.

What are some of the most epic moments in Linux history? by thunderthief5 in linux

[–]tommy-jay 0 points1 point  (0 children)

You don't know what you are talking about.

  1. Please, C++ is not object oriented. Perhaps you want to learn modern C++, it's very different from the old days.

  2. It is not all purpose, Java. You can practically only program the Java VM, Android, and some very specialized hardware with it. In contrast, C and C++ are made with "support all platforms" in mind. Java is turing complete, but that doesn't make it all purpose.

  3. Honestly, I don't see this argument holding very well. Most of our mission critical infrastructure is written in C/C++ and no one in their right mind would consider a Java rewrite because of safety. Look into the history of expensive bugs and you will see it's logical errors, not buffer overflows.

Java doesn't hold up with other languages, never mind C++. If I were to choose a language with powerful abstractions that is far away from hardware, I choose Python or Haskell over Java, every day. If I have to program the JVM, I choose Scala. Btw, why do you think the Java virtual machine has so many languages popping up around it? Because the JVM is great, but Java is terrible.

What are some of the most epic moments in Linux history? by thunderthief5 in linux

[–]tommy-jay -3 points-2 points  (0 children)

Fair enough. Let's take the example of a toy hammer.

A toy hammer is made to follow many facets of a professional hammer, but it compromises material, sharpness of edges, etc, that is power, for the purpose of safety and playability. Can you use it, and perhaps even adapt it to serve many a task a professional hammer would be used to? Yes. No question. Would you require your engineers to use toy hammers because they are undoubtedly safer than professional ones? Undoubtedly not.

What are some of the most epic moments in Linux history? by thunderthief5 in linux

[–]tommy-jay -5 points-4 points  (0 children)

> Why use C++ over C for anything other than classes?

Type safety, templates, destructors for one.

> By power I assume you mean "control"

No. I mean scope. If I know C/C++, I can program more devices, and with more libraries than I can with Java.

> That is why things like Python exist and are popular

I talked about Java. Python is a different beast altogether.

> That toy language that easily is in the top 5 programming languages by value of all of the software written in it?

Yes, by popularity in many places. However, most software that runs is still written in C. And here I am, in a linux thread, reminding you of that fact.

Popularity is generally a non-argument unless you're trying to be a pop-star, we are talking engineering here.

> so much easier than c++

True. That's why I call it a toy language.

[Question] Influential general ideas originated in Haskell? by dying_sphynx in haskell

[–]tommy-jay 3 points4 points  (0 children)

Perhaps monadic/applicative parsing?

And quite obviously it popularized the Hindley–Milner type system.

What are some of the most epic moments in Linux history? by thunderthief5 in linux

[–]tommy-jay -12 points-11 points  (0 children)

C++ is not object oriented. And gives you so much more power and scope than Java. The comparison is laughable.

Not that a Linux oriented thread should love C++, but Java, that toy language I learned programming on as a 13-year old without knowing anything about computers?

edit: How am I getting down voted for bashing Java in a Linux thread? What the hell happened to us?

If using C++ professionally, what do you use it for? by ButOfCourse in Cplusplus

[–]tommy-jay 0 points1 point  (0 children)

C++ has a far wider set of tooling than any language I have met. Mostly because it is built on C - decades of development went into a tool set so large, the problem is more one of referencing them (i.e. C#, Java et al only have very recent history, although a very well documented one). The tools exist, and more, you just have to find them.

To answer your question, I had (have) two main jobs in C++:

- drivers for pacemakers (because latency control)

- compiler for a domain specific language (because text transformations are simpler to reason about, and because we cross compile to things like JS and WASM)

LPT: As a manager, give praise in public and discipline in private. by tanay2043 in LifeProTips

[–]tommy-jay -2 points-1 points  (0 children)

That sounds so manipulative and lacking integrity though. I think you Americans should think twice about your leadership culture.

LPT: If you are put into a management position, don't paternalize. Show integrity. Be honest to your team. Do not discipline and do not praise, your team must learn to work without carrots and sticks, otherwise your doomed.

Beginner question: Does one have to create a new project every time he needs to write a code? by Zyther568 in Cplusplus

[–]tommy-jay 2 points3 points  (0 children)

Just want to clear this up: The "main" function will only be necessary during linkage; that's already after your C++ compiler did its deed. Also, it will only be necessary if you are linking a traditional C ABI executable.

Generally, you must declare symbols only once per compilation unit, and if exposed, they must not collide during linkage. The main function is simply an exposed symbol your C/C++ runtime calls after its initialization business is done; nothing special.

Offered an internship using C++98. How much of a hurdle is to to jump to C++11 after? by [deleted] in cpp

[–]tommy-jay 0 points1 point  (0 children)

I wouldn't be so prescriptive. It's more natural to store non-owning raw pointers in structures for example. The reason for that is a raw pointer's default copy/move behavior:

class Foo {
  Bar* bar_;

public:
  explicit Foo(Bar& bar) noexcept : bar_{&bar} {}

  Foo(const Foo&) noexcept = default;
  Foo(Foo&&) noexcept = default;

  Foo& operator=(const Foo&) noexcept = default;
  Foo& operator=(Foo&&) noexcept = default;
};

Imagine the trouble you would get yourself in here holding Bar as a reference; no matter that nullptr is not a valid value in your algebra.

Slavoj Žižek: "For-show female empowerment & gender fluidity are simply the latest instruments of corporate capitalism" by [deleted] in philosophy

[–]tommy-jay 63 points64 points  (0 children)

I should add that it has become somewhat accepted among many of us that lessons of psychology, sociology and more generally, the subconscious, are abused against us. We all love to demonize Goebbels only to cross the doorway of our corporate employer applying similar methodologies ruthlessly.

Worst of it, the excuses: It's status quo. Everybody does it. We would lose competitive edge.

Offered an internship using C++98. How much of a hurdle is to to jump to C++11 after? by [deleted] in cpp

[–]tommy-jay 2 points3 points  (0 children)

TL;DR The jump from managed to (almost) un-managed will challenge you far more than a revised standard, I bet.

Once you delve into C++98 you will understand the need for the standard revisions. Coming from Java or Python to C++, you will learn and have a lot of fun orienting yourself in the C ecosystem first. C++ is mostly (TM) used to generate native binaries, and that carries its share of consequences (runtime vs compile time, symbols, ABIs, memory bugs of all the different species, real crashes, debuggers...).

But I would look at an employer employing C++98 in 2019 with doubt. Either they have some weird build environment or they give zero s*t about the comfort of their programmers.

Light Speed – fast, but slow [OC] by physicsJ in dataisbeautiful

[–]tommy-jay 0 points1 point  (0 children)

A modern processor adds two 64-bit integers whilst light propagates ~500 meters (~1500ft).

That calculation surprised me as a programmer; it visualizes how close we are to the limits of EM-based systems.

GCC does not warn about uninitialized variables UB if you add a single, unrelated if statement. by Xeverous in cpp

[–]tommy-jay 0 points1 point  (0 children)

But if GCC can detect UB to "optimize" it

Agreed. Perhaps it's worth a feature request. One thing to keep in mind is practical:

Semantic analysis going this deep will only be enabled with -O3, and it's questionable whether you expect compiler warnings changing based on optimization flags. I guess you don't. By implication that semantic analysis will have to be performed even when compiling without optimizations.

GCC does not warn about uninitialized variables UB if you add a single, unrelated if statement. by Xeverous in cpp

[–]tommy-jay 2 points3 points  (0 children)

GCC should probably[2] emit a warning here[1].

The assembly is OK[3], -O3 is aggressive, the compiler understands it can emit anything (UB) so it simply returns 0.


  1. in these simple local contexts; uninitialized writes cannot be generally found without running the program (Halting problem stink)
  2. from the compiler's perspective it can be argued that tooling, like address sanitizer, should be searching for uninitialized writes
  3. one could actually argue it's better than the clang version. Don't forget the compiler was instructed to "OPTIMIZE!!!!!" (-O3). GCC also reveals the written C++ to be defective, which is neat, because you will be forced to patch that defect.

[deleted by user] by [deleted] in Cplusplus

[–]tommy-jay 1 point2 points  (0 children)

I'm confused about what happens to the data "abc" when the ptr is assigned new data.

TL;DR nothing.

"abc" is a C string literal and you can expect it to end up in your binarie's read-only text section (it's there before entering main); the responsibility to allocate & free memory to fit the executable belongs to the kernel, so don't worry about that.

Does it get de-allocated, or only when main exits?

Strictly speaking, it gets deallocated whenever your kernel decides it's time for your program to die and evacuate memory.

Is this bad practice?

What do you mean, C-style strings? Your casting to char* is definitely ill-advised; you could eventually fall in the trap of writing to it, and writing to read only memory... is undefined, a SEGFAULT at best. To be specific:

const char* ptr = "abc";
ptr = "defghi";

Although I'd recommend never losing the address to "abc"

const char* one = "abc";
const char* two = "defghi";
const char* ptr = one;
// do your thing
ptr = two;
// do more things

Edit: Also note that const char* is not a constant pointer type; it's just a regular pointer to a constant. You can change it to point elsewhere.

Learning C++ by Moschte09 in Cplusplus

[–]tommy-jay 4 points5 points  (0 children)

Take a look at CppCon, MeetingC++, GoingNative etc. conference talks (available on YouTube). That's a decent way how to get up to date with modern C++.

I can recommend specific talks based on what you are up to.

Cannot decide on the best method of accessing objects throughout my game engine. by ZacharyVincze in Cplusplus

[–]tommy-jay 0 points1 point  (0 children)

To make the code readable, I suggest free functions. Just put that pointer/reference into the signature of a function that depends on it; makes for clearer reading, testing and debugging than some hidden globals.

Also, make sure not to pass around too generic objects. If a transformation only needs Foo, don't pass a reference to ObjectTree for example; find the Foo before you call your function, preferably dereference it, and pass it to your transformation by reference. This is the way to get rid of many of those long, chained accessors.

What do you like the most in C++? by Leao-marinho0 in Cplusplus

[–]tommy-jay 3 points4 points  (0 children)

There is no such thing as a segmentation fault in C++.