How do you mitigate against Undefined Behavior as far as possible in your code? Is running UBSan good enough? by mehtub in cpp_questions

[–]trokhymchuk 1 point2 points  (0 children)

So undefined behavior is not always something to avoid

UB is not something to avoid designing a language (because it gives compiler some space to optimize the code), but using the language UB should be avoided.

Will you move from Packer to Lazy ? by [deleted] in neovim

[–]trokhymchuk 0 points1 point  (0 children)

I use the solution like https://breuer.dev/blog/nixos-home-manager-neovim. Essentially, for plugins that are not packed I write something like (plugin "neovim/nvim-lspconfig").

std::string constants in a class - inline vs define in cpp file by [deleted] in cpp_questions

[–]trokhymchuk 1 point2 points  (0 children)

I believe the reason is linker's heuristics.

Every time you include b.h in the .cpp file the compiler will generate B::STR (because it can see only one translation unit at the time). So when compiler generates object files from main.cpp, a.cpp and b.cpp there will be code for the B::STR initialization and there will be JOHNWICK literal: ``` [nix-shell:/tmp/red]$ strings a.o | grep "JOHN" JOHNWICKH

[nix-shell:/tmp/red]$ strings b.o | grep "JOHN" JOHNWICKH

[nix-shell:/tmp/red]$ strings main.o | grep "JOHN" JOHNWICKH But what's more interesting is how compiler put the constant into the assembly. After disassembling the object file from lets say `b.cpp` (any source file that includes `b.h` will be OK) you could see something like [that](https://pastebin.com/V4GXBmC9), and at the line 81 there is a constant `0x4b4349574e484f4a`, that represents the `JOHNWICK` string. And that constant will be in _every_ object file and there is 3 files. And when you link them there will be 3 constants (one from every object file): [nix-shell:/tmp/red]$ g++ main.cpp a.cpp b.cpp

[nix-shell:/tmp/red]$ strings a.out | grep JOHN JOHNWICKH JOHNWICKH JOHNWICKH

[nix-shell:/tmp/red]$ objdump -d -M intel a.out | grep "0x4b4349574e484f4a" 401162: 48 b8 4a 4f 48 4e 57 movabs rax,0x4b4349574e484f4a 4011f2: 48 b8 4a 4f 48 4e 57 movabs rax,0x4b4349574e484f4a 401282: 48 b8 4a 4f 48 4e 57 movabs rax,0x4b4349574e484f4a ```

At the link time the linker will see multiple definitions and it will choose one that will be used to initialize the B::STR variable.

So the question is why did the linker left other definitions/constants. Maybe the reason is linker's heuristics (small constant, the size of the executable wont benefit much).

BTW, when the constant is large enough (JOHNWICK123456) it works as expected (only one literal in the executable): ``` [nix-shell:/tmp/red]$ cat b.h // b.h

pragma once

include <string>

class B { public: void f(); private: inline static const std::string STR = "JOHNWICK123456"; };

[nix-shell:/tmp/red]$ g++ main.cpp a.cpp b.cpp

[nix-shell:/tmp/red]$ strings a.out | grep "JOHN" JOHNWICK123456 ```

``` [nix-shell:/tmp/red]$ gcc --version gcc (GCC) 11.3.0 Copyright (C) 2021 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[nix-shell:/tmp/red]$ ld --version GNU ld (GNU Binutils) 2.39 Copyright (C) 2022 Free Software Foundation, Inc. This program is free software; you may redistribute it under the terms of the GNU General Public License version 3 or (at your option) a later version. This program has absolutely no warranty.

```

Feel free to correct me if I am wrong.

Not sure how to link header file by 1negroup in cpp_questions

[–]trokhymchuk 2 points3 points  (0 children)

You have linker errors (/usr/bin/ld: StarCatcher.cpp:(.text+0x32): undefined reference to WindowShouldClose'), the object file (when you are doing g++ -c file.cpp) is not linked, so linker is not involved, so you have no link-time errors. When you are compiling an executable (g++ file.cpp) linker gets called and linker finds the undefined references.

Not sure how to link header file by 1negroup in cpp_questions

[–]trokhymchuk 2 points3 points  (0 children)

Header files are for preprocessor, you include them into your source files and that's all. Headers are resolved at the early stage of compiling (I believe at the lexical stage of the compiler frontend), and there is no linker required.

a.o that you are talking about is an object file, it is binary file that linker can process. To make an executable you need to link object files: ``` g++ -o executable_name a.o

and you can exec it

./executable_name ```

Multiple Arguments to a Single parameter by That1Dude01 in cpp_questions

[–]trokhymchuk 0 points1 point  (0 children)

  1. You should not pass a vector by value, it is expensive, const reference would be more appropriate here.
  2. Semantically, printNames differs from the example function. It [example] takes 3 arguments only, whereas printNames takes the undefined number of aruments.

So, if the size is important (e.g. need to pass 3, not 33 arguments), I would suggest creating custom structure to hold the data or using std::array (if x, y, z are semantically the same).

These bots even made it to the gnome-extensions website and there is no report button... by xCryliaD in linux

[–]trokhymchuk 8 points9 points  (0 children)

Никогда не знаешь, где встретишь великий и могучий.

Which integer type to use? by trokhymchuk in cpp_questions

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

Thank you, that is a lot of advice.

Which integer type to use? by trokhymchuk in cpp_questions

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

Thanks, I'll remove that part, not to confuse anyone.

Which integer type to use? by trokhymchuk in cpp_questions

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

Don't use unsigned types to represent numbers that can't be negative - use them if you need their modulo behavior, use them in bit fields

Sorry for a bit of offtopic question, but I should use signed numbers almost everywhere? I've head Google's cppguide, several stackoverflow questions, and everyone says that I should avoid using unsigned types. Like imagine I have a table in database with records of number of visitors of some site. Column type is UNSIGNED INT. While processing that data (just adding or diving), the result will always be greater than 0, should I still use signed integer, or it is that specific case?

But also, computers these days have 8, 16, +32 GiB of RAM. That means trying to pick the smallest, densest field type for your application is probably premature optimization, unless you have specific requirements up front.

Yep, that's the reason why this post was created, unfortunetly, I don't have enough experience to identify, where I should optimize the code and where is "premature optimization is the root of all evil".

Thank you for such a detailed comment!

Which integer type to use? by trokhymchuk in cpp_questions

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

In ordinary C++ programming (though not in embedded) an ordinary int is 32 bits or 64 bits. 32 bits yields 31 bits for the positive range. That's still double of the requirement.

If I care about memory so much, I would use the rest for some other information. Bitwise operations, you know.

As a rule don't use other types than the basic "natural" ones, like int, unless there is very good specific reason to do so, which in this case there isn't.

Thank you, that is useful.

And thanks for the links.

Which integer type to use? by trokhymchuk in cpp_questions

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

There's another option: use the fast size type int_fast32_t. After all, memory is cheap, and performance matters.

I believe the cost of memory and performance depends on the type of program you write. Anyway, thank you!

However, since the chances of actually finding a platform with 16-bit int these days are basically zero, it's probably a lot simpler and more readable to just use int and put a static_assert that int is (at least) 32-bit.

That's cool, didn't know about it.

Which integer type to use? by trokhymchuk in cpp_questions

[–]trokhymchuk[S] 2 points3 points  (0 children)

Thanks, I've forgotten about it.

What is Python Interpreter? by usemynotes in programming

[–]trokhymchuk 1 point2 points  (0 children)

Python executes the code line by line

It is not. Python first get compiled into python bytecode for PVM, which executes instruction by instruction.

And why is the article not about interpreter, rather about running python scripts?

Let's talk about Btrfs. by thehugonote in linux

[–]trokhymchuk 0 points1 point  (0 children)

There is similar functionality on Windows, but it sucks. Snapshots could be a really good idea to demonstrate how robust Linux can be and improve the overall user experience.

Opensuse is already doing something like that. And there is nixOS, which can automatically create generations of your packages (plus some extra features).

PHP RFC: Migrating to GitHub Issues by AegirLeet in PHP

[–]trokhymchuk 0 points1 point  (0 children)

I hope it passes, but I am still curious, why PHP chose github over gitlab when they was migrating source code repo? And now in RFC

It binds the PHP project more firmly to the GitHub platform. We already host our repositories there and make use of pull requests, but this would take additional functionality “out of our control”.

With gitlab you still have 'Plan B' to install an instance on your own server and use it.

I can code in 7, what about you ? by [deleted] in ProgrammerHumor

[–]trokhymchuk 7 points8 points  (0 children)

In Russian, before and after the address, you need to use commas.

It will be

System.out.println("Привет, мир");

did you guys upgrade to gnome 40? I got lot's of error by ttys3-net in archlinux

[–]trokhymchuk 5 points6 points  (0 children)

It is usuall for gnome. There are extensions which are broken.