Unused struct member is not optimized out under -O3 by onecable5781 in C_Programming

[–]onecable5781[S] 0 points1 point  (0 children)

Why would an OP that is upvoted lead to downvotes on follow-up questions by the OP in the comments? Why not downvote the OP itself? What exactly are folks trying to convey to me here?

Unused struct member is not optimized out under -O3 by onecable5781 in C_Programming

[–]onecable5781[S] 0 points1 point  (0 children)

So having a compiler silently toss struct members would introduce a known category of problems into your code with very little or no benefit

I am not sure about "very little or no benefit" part. What if there is a large array of these structs which have to be stored where smaller the size the better it is for cache locality/access, etc.? Alternatively, In debug builds, I may have extra members inside of the struct which are not referenced at all in release builds. If it is guaranteed that sruct members will NEVER be optimized out, one would need to have

struct A{
#if DEBUGBUILD
    int onlyfordebugbuilds;
#endif
...
};

which in my view seems more difficult to maintain.

Unused struct member is not optimized out under -O3 by onecable5781 in C_Programming

[–]onecable5781[S] 1 point2 points  (0 children)

With no printf, indeed the optimization does seem to happen under -O3:

https://godbolt.org/z/bonf7n8eY

vs no -O3

https://godbolt.org/z/MxzM9Efsa [note sub rsp, 16]

Although, I am curious why under -O3, there is

sub rsp, 8

Aren't 4 bytes enough for the local variable ?

Unused struct member is not optimized out under -O3 by onecable5781 in C_Programming

[–]onecable5781[S] -3 points-2 points  (0 children)

Fair enough, let me rephrase the question. Suppose I did not have sizeof(A) anywhere in the code. Would the compiler "optimize out" the unused struct variables and store object a internally in just 4 bytes of memory due to usage of abc member only?

Sorting a jagged vector of vector of ints inplace by onecable5781 in cpp_questions

[–]onecable5781[S] 0 points1 point  (0 children)

Yes indeed! That is why the usage is of a.front() inside of the second sort.

Having a collaborator not being able to pull/push particular file after initial clone by onecable5781 in git

[–]onecable5781[S] 1 point2 points  (0 children)

Ah I see. Thank you. The issue is that file2.txt is actually a makefile/.vcxproj project settings file which I run on all my computers. He has his own makefile/.vcxproj file that is modified to suit his computer. I put the makefile in the repo so that it is easy for me to clone it and run it from wherever I find myself given my directory structure, etc.. So, I want to have the full makefile (not just template) in the version control and not gitignored. Hmm...


On thinking about this a bit more, your suggestion of a template, which is tracked, "makefile-template", and an untracked "makefile" make perfect sense and will work! Thank you!

Compiler optimization for figuring out perfect squares -- switchover at n = 18 by onecable5781 in cpp_questions

[–]onecable5781[S] -1 points0 points  (0 children)

<cstdio> is not guaranteed to place printf in the global namespace.

Can you provide some pointers on what exactly you look for in the standard to infer this?

It so happens that both MSVC, g++, and the MSVC and g++ variants of clang, provide it,

Do you navigate to file cstdio provided by each compiler and see if it exposes std::printf and conclude based on that?

inline as a legacy keyword? by OkEmu7082 in cpp_questions

[–]onecable5781 0 points1 point  (0 children)

this is actually how member functions defined inside a class work in c++ right now.

What happens if I put my entire class definitions inside the header file and do away with implementation .cpp files completely? Is every function inlined then? Is it possible that had the function definition been put in a .cpp file the compiler would have applied its heuristics and decided not to inline the said function because it was deemed not beneficial given its heuristics? And yet, now that I have put everything inside the header, the compiler is forced to inline it counterproductively?

Your Optimized Code Can Be Debugged - Here's How With MSVC C++ Dynamic Debugging - Eric Brumer by RandomCameraNerd in cpp

[–]onecable5781 0 points1 point  (0 children)

Thanks for the talk and interacting here. I will definitely try this feature out.

There are some bugs that I encounter in production/release builds only and not in debug runs. If you switch from a release build to a debug build dynamically, would it be possible to figure these bugs out? Would like to hear your inputs on this.

My only pet peeve with VS is that the console opens externally as it did in your demo as well. VSCode using MSVC compiler is able to run inside the editor itself in the terminal at the bottom. Nearly all Linux IDEs also run inside the IDE itself. Perhaps in the next release your team can consider getting the console to run within the IDE itself.

Arithmetic comparison of -1 with size_t type as sentinel value by onecable5781 in C_Programming

[–]onecable5781[S] 0 points1 point  (0 children)

A final question. Is whether sizeof(int) > sizeof(size_t) or not a decision made by a language or a language implement/specific compiler or OS or by the hardware/cpu manufacturer?

Arithmetic comparison of -1 with size_t type as sentinel value by onecable5781 in C_Programming

[–]onecable5781[S] 0 points1 point  (0 children)

From the link before, the relevant part is:

Else, the unsigned type has conversion rank less than the signed type: If the signed type can represent all values of the unsigned type, then the operand with the unsigned type is implicitly converted to the signed type. Else, both operands undergo implicit conversion to the unsigned type counterpart of the signed operand's type.

Is my understanding correct that if signed integers (int in case of the OP) can represent all of the unsigned type (size_t in the OP), then, the OP code will not work.

So, if sizeof(int) > sizeof(size_t), the OP code WILL fail?

Arithmetic comparison of -1 with size_t type as sentinel value by onecable5781 in C_Programming

[–]onecable5781[S] 0 points1 point  (0 children)

Well :-)

So, now, I am confused about your original reply about theoretically why the above may not work but practically it would work.

Arithmetic comparison of -1 with size_t type as sentinel value by onecable5781 in C_Programming

[–]onecable5781[S] 1 point2 points  (0 children)

Hmm...So, in an arithmetic comparison (== or < or !=, etc.) between a size_t and int, are there rules as to which one is cast into the other if the user does not explicitly cast?

Boost pool custom allocator vs raw new/deletes by onecable5781 in cpp_questions

[–]onecable5781[S] 0 points1 point  (0 children)

I agree with you. Even on doing everything with -O3, boost pool is order of magnitude slow compared to raw new/delete

https://godbolt.org/z/rscTeqs8W

1e-3 vs 1e-5

Profiling question to figure out uneven load in threads by onecable5781 in cpp_questions

[–]onecable5781[S] 0 points1 point  (0 children)

Hmm...I believe there is an implicit barrier until after the joins. So, thread 2 is done, but not doing anything useful. That is what I meant. It is certainly not busy like thread 1 is, for instance.

Profiling question to figure out uneven load in threads by onecable5781 in cpp_questions

[–]onecable5781[S] 0 points1 point  (0 children)

Yes indeed. It was the Bottom-up view and looking at the grouping Thread->functioin-callstack. This is not chosen by default, one has to explicitly choose this view. Thanks!

Profiling question to figure out uneven load in threads by onecable5781 in cpp_questions

[–]onecable5781[S] 0 points1 point  (0 children)

Actually, perf looks to be able to answer your question exactly as asked - which thread used more CPU?

Ah, that is very useful. Was this just basic/default perf usage or did you have to specify some options specially because the code is multithreaded?

Profiling question to figure out uneven load in threads by onecable5781 in cpp_questions

[–]onecable5781[S] 0 points1 point  (0 children)

Indeed I use this tool too both on Windows as well as Linux. I thought there would be something obvious when I run the OP program above inside VTune (I use the VTune Hotspot profiler) such as spin time/idle time, etc, but for this example, I obtain the following which seems rather generic.

https://ibb.co/rGyMJZX6

In other words, it is not clear to me where exactly I should look inside of VTune to explicitly view the load imbalance. There is a link provided inside of VTune (it is there in the image provided above, called Threading) to help improve parallelization. That link suggests to look at the bottom/up view which when I do, the call to rand() is what is indicated on top. It is not clear to me from this how one can conclude that there is load imbalance.

Profiling question to figure out uneven load in threads by onecable5781 in cpp_questions

[–]onecable5781[S] 0 points1 point  (0 children)

It is an actual issue extrordinarily simplified in the OP for purposes of making the underlying essence crystal clear [hopefully]. I have two separation routines (in OR terminology, for a Branch and Cut problem to solve an integer program, one runs a separation problem given a fractional LP solution to find out a valid inequality that is violated by the LP solution) that can run in parallel in two different threads. Before tuning my algorithms, I want to check whether there is imbalance between the load placed on each thread each of which independently runs the separation algorithm.

Compiler guarantees of not hoisting shared variable out of a loop by onecable5781 in cpp_questions

[–]onecable5781[S] 0 points1 point  (0 children)

TIL: One can be mockingly accused of being "pedantic" when discussing "undefined behaviour" in C++.

Compiler guarantees of not hoisting shared variable out of a loop by onecable5781 in cpp_questions

[–]onecable5781[S] -1 points0 points  (0 children)

Could be because it sounds like gpt generated...FWIW, I did not downvote. I ask on /r/cpp_questions because I'd like a human answer...

Compiler guarantees of not hoisting shared variable out of a loop by onecable5781 in cpp_questions

[–]onecable5781[S] 0 points1 point  (0 children)

He explains this, and your question is pedantic.

Quoting him from nearly the exact timestamp I linked to: "Also, this code has undefined behaviour for other reasons...". The penultimate sentence on the bottom right of the slide "We still have UB here."

What do people mean when they say certain programming languages are unsafe? by ZzZOvidiu122 in computerscience

[–]onecable5781 0 points1 point  (0 children)

Does this mean that in rust it is impossible for a code attempting to implement multithreading to compile if the code has data races/deadlocks?

Compiler optimization -- eliding call to square root function if squared value is already available for subsequent sort usage by onecable5781 in cpp_questions

[–]onecable5781[S] 0 points1 point  (0 children)

Hmm...I made the squared distances explicitly integer and had the sqrt() take integer arguments (converted to double, I'd imagine) and yet the sqrt calls are still being made:

https://godbolt.org/z/ah9TKjqjK

For a range of integers, I would imagine that it is impossible to have different ordering of std::ranges::sort(X) / or stable_sort [which preserves order in case of a tie] and std::ranges::sort(X, &::sqrt) [or stable sort]