« first day (1451 days earlier)      last day (1402 days later) » 

11:52 AM
@Michael some preview pages are available in google books following this link, books.google.dz/…
@Michael This book is available in academia.edu (requires sign-in) academia.edu/13127144/Guide_to_Assembly_Language?auto=download
 
Hi all. I'm currently looking at value categories in C++ and I think I'm able now to differentiate xvalue, lvalue and prvalue. But I'm not sure to understand in what case it can be useful to make this differentiation. Do you have any example where it is important, as a developer, to make the distinction between value categories? Thanks.
 
nwp
It isn't. You only need to make this differentiation if you want to write a C++ compiler. Programmers only need to know what a temporary is, why it matters and how to force a non-temporary to be treated as a temporary.
 
 
3 hours later…
3:00 PM
Hello everyone. I am new to C++. I am coming from JS. Would you guys suggest I manage memory on my own or use a garbage collector library to start?
 
nwp
Neither. Stick to values and let standard containers worry about memory management.
 
Just use values where possible and reasonable and smart-pointers where it's not. That covers 99% of you memory management needs
We have a bunch of ruby and JS developers in our company that contribute occasionally to a C++ code-base, they don't run into any horribly memory management issues. Because the code-base is new enough that you won't find any "new" or "malloc" in it
 
@nwp Just to confirm, these will manage memory themselves?
 
 
nwp
Yes, though prefer ^ as a reference.
 
3:09 PM
Ok. Thank you.
 
Also avoid using new and delete you shouldn't need them for value focused coding
 
I understand delete, but why not new?
If I want to create a class instance I will need to use the new new operator, right?
 
nwp
If you don't know which one to use, std::vector should be your default.
@JBis No
Just never use new and you will dodge most problems.
 
@JBis No, you can create it with automatic duration by just declaring the variable and not using pointers
 
@JBis just to be clear the "they manage their memory" goes for value types you put in those containers, if you put raw-pointers in containers, then the container won't clean them up
 
3:11 PM
@JBis I'd suggest starting with Bjarne's book "A Tour of C++" otherwise you're likely to be very very confused
 
nwp
MyClass c; is how you make an object of your class, or MyClass c{args};. There is no new involved.
 
class A {};

A a; //<-- no new necessary
std::vector<A> vec;
vec.emplace_back(); // <-- no new necessary
 
interesting
 
but the latter link is more helpful
but honestly... just go read Bjarne's book
most of this is too in-depth for what you need to know now
 
@Mgetz Do you have an internet resource that you suggest instead? With JS i was able to find enough online.
 
3:17 PM
@JBis you're entering a world of ICE (Internal Compiler Error) dragons, Undefined Behavior Demons, and Implementation defined Devil's bargains.
 
sounds scary, but I've used PHP before so I should be ok
;)
 
@JBis PHP last I checked doesn't have Undefined Behavior, and very limited Implementation defined
 
@Mgetz Thanks, I will come back after reading.
Before I start, I assume I should use the most recent version (C++20) or should I start with something older?
 
@JBis C++14/17
C++20 isn't fully supported yet, C++14 is generally supported well, and C++11 is reasonably well supported
screw C++98/03
 
supported by who?
 
nwp
3:27 PM
Technically C++20 doesn't exist yet.
 
@JBis Implementations, e.g. gcc/clang/mvsc, and their associated stdlib implementations (STL is actually not the right term)
 
nwp
Note that only the first 3 columns matter.
 
btw the usual answer to someone coming from another language, particularly a GC'd language into C++ is "GO BACK"
 
@Mgetz i'm starting to see why
I'm guessing gcc vs clang doesn't matter for me yet
 
Is reading from uninitialized memory undefined behavior?
 
3:36 PM
c++ if you're willing to learn the idioms is a very expressive and powerful language
@Yashas yes...
 
@Mgetz even if I don't use it?
I am reading and storing it in a register but not using it.
 
nwp
Why are you messing with registers?
 
C++ seems like a logicial choice because it will allow me to write native extensions to my node programs. nodejs.org/api/addons.html
 
@Yashas Yes, OpenSSL infamously used to do this... and then it had an incident where the compiler detected it and did time travel because it's UB... causing hundreds of certificates to need to be regenerated
@JBis do you need C++ for what you want to do? or can you do it in JS just fine? Unless there is a massive performance need you can actually optimize your JS pretty well
 
@nwp It's actually CUDA code. So all local variables are generally registers.
 
nwp
3:38 PM
@JBis It doesn't really. Sometimes one gives better error messages or performance than the other, but it's not consistently one or the other.
@Yashas Which is not your problem because you're writing C++ and C++ doesn't have registers.
 
@nwp CUDA has extensions.. but it's irrelevant nvcc is based on clang last I checked and will optimize out the UB
reading uninitialized ram will just result in time travel
 
@Mgetz idk, I probably don't need it. But I'd like to learn some new langs. And C++ isn't going anywhere. I will probably also learn Kotlin eventually.
 
I actually have an extremely compute-intensive loop and adding a branch to protect from uninitialized access causes 40% loss in total execution time.
 
@JBis learn rust
@Yashas then preinitialize? that should be quick
 
allowing the useless computation to actually happen is faster (anyway the these are going to be predicated instructions regardless and will consume cycles)
 
3:41 PM
literally just do a quick kernel to zero
 
@Mgetz why rust?
 
nwp
@Yashas Of course it's faster. It let's the compiler remove the loop :D
 
@JBis fewer and harder to actually stumble on dragons
 
Also, many programs are written in C++ (e.g. Chromium)
 
@Mgetz That's what I have done but it felt so unncessary. The tests aren't failing even though there is some UB.
 
3:42 PM
and its difficult to do stuff with Chromium when I don't know C++
 
nwp
@Yashas Testing and UB don't work well together. Testing requires consistent results and UB is notoriously inconsistent.
 
@Yashas so how uninitialized is it? E.g. where are you allocating it? Because it could be live from a kernel perspective even if it's dead from a C++ perspective
can you show the code?
 
156-157 has the relevant comment and the code that follows is what does the uninitialized memory accesses
 
nwp
Manually deciding which loops to unroll. Bleh. I'm glad I don't need to work in such environments.
 
@Yashas group_i_boxes?
because I'm not seeing undefined access
 
3:50 PM
@nwp The synchronization barrier in the loop prevents the compiler from unrolling properly. This is probably correct behavior but by my logic I know that you can mix computation across the barrier. So I have explicitly unrolled in a way that I compute first half of the loop body for N iterations and then do the second half of the loop body for N iterations. The compiler is unable to do this.
@Mgetz group_i_boxes and group_i_size have uninitialized memory.
 
@Yashas __shared__ vector_type group_i_boxes[BLOCK_SIZE] is not "uninitialized" it's just "undefined state"
 
Whats the thoughts on operator overloading? Good? Bad?
 
it's a weird quirk of the standard that those are in active lifetime but the values are undefined
 
@Mgetz interesting, what's the difference between uninitialized state and undefined state?
 
so that's not officially UB
 
3:52 PM
@Mgetz I am so happy now
 
@Yashas wording in this case. Uninitialized memory is something like malloc and then not creating objects
because the object lifetime hasn't started there
 
nwp
@JBis Depends on how it's used. If you make a matrix class and provide the usual arithmetic operations then it's fine. If you add operator<< to your class so you can do std::cout << myclass; it's also fine. If you start creating your own language by defining that f | g means the the function f is executed and then g is called with the result then it starts getting not so fine.
 
@nwp btw there are caches which users can control
 
but an array like that the object lifetime starts when the array is declared, technically constructors of complex objects are supposed to run. en.cppreference.com/w/cpp/language/lifetime
 
nwp
@Yashas Again, I'm glad I don't need to know that.
 
3:54 PM
@nwp hmm interesting
thanks
 
@nwp you throwing shade at ranges :P
 
@Yashas FWIW you could just do __shared__ vector_type group_i_boxes[BLOCK_SIZE] = {}; and it'd be fully initialized
or zeroed
most floating point units have special cases for zeros and are faster computationally for that
 
@Mgetz Oh my, I remember that {} syntax. I instead moved the block stride loop condition inside the body and wrote zeros when false.
I don't think the compiler will be able to optimize that to what I have done though.
Probably zeros the whole memory and then again populates it.
For the grid dimensions I have, the zeroing is completely useless most of the time. Only the edge bocks actually do not fully fill up the shared memory.
 
@Yashas it should actually, depending on how it allocates it may just request a zero page. Most GPUs have a page zeroing subunit on modern GPUs
it's faster to just indicate you want it zeroed and the compiler will make it fast in most cases
GPUs have to have the zeroing unit at this point due to security issues, older GPUs do it via the driver sadly. But it still happens
 
allocating different pages is going to be hard when it's on the stack like that one. Or does CUDA not store function locals on a stack?
 
4:01 PM
How can I detect when, which user logged to my server.
Is it possible?
 
@PeterT for stuff like that no, it uses GPU ram but that's __shared__ so it might actually be host ram
 
I have 10 users on my server. They often logged in to my server, but I can detect when they logged in, which user logged in. If it is alright with you guys, please tell me.
 
No, the __shared__ makes the GPU allocate memory for that array in an on-chip cache with several TBps bandwidth.
 
@Yashas last time I actively wrote cuda was 1.5 IIRC
either way it should be zero'd by default so I'd be surprised if it's not already zero'd
but marking it as such shouldn't hurt
 
this is getting real complicated real quick
 
4:09 PM
@JBis what is?
 
c++
 
Or put in a way which scares the CPU programmers, CUDA shared memory is a cache where you manually control what is stored where. It is just a special addressable memory backed by fast on-chip memory but I don't like this description because it doesn't sound as scary as "user-controllable caches".
 
Yes...
 
alright im gonna start with a html parser
 
@JBis Don't... that's literally the deep end of pain
because SGML isn't xml etc.
and HTML5 has very clear parsing rules but is still a pain.
 
4:11 PM
i already built one in js, just gonna convert to cpp
 
JS devs are going to have a hard time writing C++ code. You're going to find stuff that obviously should work (at least works in JS) not work in C++. I blame JS though for have ridiculous features stuff that murders code readability and unnecessarily complicates things. I like C++.
 
@JBis don't... the idiom is completely different
 
@Mgetz so i can learn it, right?
 
nwp
It'll probably just make you hate C++.
 
@Yashas JS makes it very easy to be a shitty programmer. But there are plenty of people who write readable JS.
well lets find out
 
4:23 PM
@JBis I used to write basic C++ code since high school. I found JS super frustrating while learning in uni. It's so evil! I thought C++ was a very complex language but then I found JS! I felt like C++ has complexity but the complexity offers performance benefits or other advantages but JS complexity felt completely unnecessary.
 
1. JS has come a long, LONG way since you were in HS
2. I really don't feel its complex
 
I might be biased as I like C and C++ very much (and usually end up in fights defending C++ while arguing with python-ers and webdevs). I am also a n00b at C++ and JS and might not be able to see the overall picture. Should take whatever I say lightly.
 
@Yashas you'll enjoy TypeScript a lot more probably. I struggle with larger programs in dynamically typed languages, seems so insane that people actually do that
 
TypeScript is another addition that makes it easier to work with
 
nwp
Arguably it's a necessity, not an addition.
But then again I'm biased too :D
 
4:27 PM
I like implicitly strictly typed languages
 
@JBis coming from JS I suspect you're going to find you hate C++ pretty quickly unless you learn the idioms
 
nwp
@JBis Maybe you get along with C++ then. Just replace let with auto and it kinda sorta works out.
 
statically typed languages, and languages with as many footguns as C++ has are not to be taken lightly.
 
@Mgetz How else to learn them then to work on something more then a simple calculator app
 
@JBis you can yes
 
nwp
4:29 PM
@JBis The compiler you wrote seems to just be a lot of copy/paste. From a language feature point of view I'd expect the calculator to be more complicated.
 
P.S the code in that project is shit, i spent like 2-3 days writing that. Don't let it reinforce the idea that JS is shit.
@nwp copy and paste from what?
 
nwp
From the language grammar. You basically wrote the language's grammar in JS which uses the same JS features over and over.
The C++ version will not be different. You will struggle to express the state and the rest is copy/paste.
 
So what would you suggest I do instead?
 
nwp
Make the calculator. Learn some Qt at the side.
 
well a graphical calculator is a bit more complicated
I'll go with cli first
 
5:04 PM
Ironically they are actually suggesting something easier
 
I've got a couple questions too:
1. My error throwing doesn't seem to work. Why is it such a PIA to concatenate a char to a string? How do I create the prop exception?
2. My IDE/clang is complaining about `getCurrentValue` saying it should be `const` and `no discard` so :
[[nodiscard]] int getCurrentValue() const {
      return this->current_value;
    }
 
throw std::invalid_argument(error);
awesome
@Mgetz I'm unsure why those are needed. I dont understand en.cppreference.com/w/cpp/language/attributes/nodiscard
 
5:20 PM
@JBis ask yourself the likelihood of this happening and what should happen when it does? Is this something the caller should be able to restart? Or is this something that needs higher level cleanup and handling?
 
main should catch the error, tell the user and start the loop again
 
@JBis [[nodiscard]] because C++ doesn't give warnings that you're calling a method that the only valid reason to call it is to get a value. const so you can access that method on a const instance or reference as it doesn't modify and is safe
@JBis That's extremely expensive for an expected failure
 
ok, fine: static array of valid operators in calculator, with a bool validOperator method that main checks before calling parse.
@Mgetz i half understand this, but i'll move on and learn later
 
@JBis That's my concern
 
Alright, then I'll ask.
> C++ doesn't give warnings that you're calling a method that the only valid reason to call it is to get a value
Why does C++ care what my function does?
@Mgetz that has a GUI, don't really want to get into building a GUI
 
5:27 PM
it doesn't you're helping someone else who comes after you and yourself if you accidentally forget to store the result
 
huh? I apologize, I'm not understanding
So const says "this function doesn't mutate anything"
why does that matter
 
@JBis someone else looking at your code will be confused and it sometimes generally hints a typo
 
confused about what
 
there is even an attrbute [[nodiscard]]
you force the return value to be used always
 
@JBis actually what it says is that it's safe to call via a const ref or constant instance. Generally speaking that means it doesn't modify anything. That matters because using const correctly stops a lot of bugs. It's using the type system to help you
const is part of the C++ type system. const int is not the same as int
 
5:33 PM
ok, i understand, just don't see why its necessary...*yet*
 
so if I try to modify const foo bar; I would hope that would produce a compiler error
in most cases you're looking at things like const ref
e.g. const foo& bar as a parameter
 
@JBis it helps you write better code and avoid time wasted on debugging errors later
and sometimes the compiler can use it for optimizations
 
oh, i'm looking at the assembly. It seems to convert it to it's actual numerical representation for const int.
 
yes because it can't be modified
thus the compiler is free to just use it as a constant
 
alright, i gotta get used to how low level this is
 
5:36 PM
const in C++ is also much much deeper than const is in JS
in JS it's only the reference
in C++ it's the type
 
why would a method care if it gets a const int or a regular int?
is it just forcing optimizations
 
@JBis it doesn't because int is a primitive, but it does care if it gets const foo& vs foo& vs foo
taking a const & parameter is basically knowledge to the caller that what they pass in won't be modified
lol gotta love compilers godbolt.org/z/E7fxIb
 
int &num  - reference to arg, i'll probably modify num
int num  - value of arg, i'll probably modify num
const int num  - value of arg, i'll won't modify num
const int &num  - why do i need the reference if i am not going to modify it?
oh wait i may know the answer
If it isn't int, but an instance of a class say Foo. Then I would need const Foo &foo because I wouldn't want to clone the instance?
 
@JBis because it's a lot bigger than an int... yes
@JBis correct
 
exhale of understanding
ok it makes sense now, thanks
 
5:49 PM
the reference also makes the modification inside the function visible to the caller
 
that said doing things like foo bar for an argument has value because it allows either in place construction possible in the argument list or for someone to std::move something inside
 
modifying num inside the function when the formal parameter is int num won't cause the caller's copy of the num to be modified
 
it's not that uncommon to see std::string non ref as an arg for something storing the value
 
@Yashas yes, this example is commonly shown in java classes
 
so sometimes when you need a copy of an object, you will intentionally not take a reference (this also has some nice properties from an exception safety perspective which you will understand when you start learning this topic)
 
5:50 PM
they'll let the copy happen or let the caller decide to move into the method
 
@Mgetz wdym?
 
not going to lie... Java really screwed me up when I first used it until I realized literally everything was a pointer
 
i hate java, i don't use
 
@JBis don't worry about it yet... just understand there are ways of saying "I don't need this, but you do take it"
so that a copy doesn't have to happen
 
i see why they say going from higher langs to C++ is difficult and easier the other way around
 
5:53 PM
but the caller's instance is no longer valid
 
@Mgetz in which case?
 
@JBis but you get power and fine control as a tradeoff
 
@JBis after you move something the instance moved is no longer valid, you have to just let it be destroyed
 
Don't get me wrong, I think C++ has its place. It's just a lot of fundamentals that are built in in higher level langs, you take for granted.
 
you get other footguns
like everything being heap allocated, or causing allocations, or being a reference
lack of deep immutability hurts
it's something rust does REALLY well actually
 
5:56 PM
@Mgetz i understand 0 of this lol
 
:49799361
    // expects a copy of str
    void do_something(std::string str) { /* ... */ }

    std::string abc = some_other_string or you initialize it with something;
    do_something(std::move(abc)); // abc is not copied but instead moved

    // abc is no more valid
 
thats weird
 
@JBis that should concern you, because that means you have bugs from things you didn't realize because of how the language works
 
without the std::move, abc will be copied => you might potentially have to allocate memory to store a copy of the string (std::string can store strings of arbitary length using dynamic memory)
but you will have situations when you know that you don't need abc anymore, so you directly move it in and save a copy
 
C++ is like the machine shop where you can build your engine. Java is when you buy it out of a parts catalog and you know it just fits and works.
 
6:00 PM
@Mgetz wdym? I just started C++
 
but when you move abc, you are actually copying the internal pointer to the string and some other properties in some sense and abc is now empty
 
@JBis how much do you understand about pointers and references?
 
@Yashas seems like such a microptimization
 
@JBis well, it's not if you had a 256MB vector
 
@JBis it's not, it's core to serious parsing libraries where even a single badly timed allocation can have serious impacts
tbf most cases of moves happening are transparent to the programmer
usually it's return values
 
6:02 PM
sometimes as a library writer, you won't know what kind of inputs users will pass. You have to write code that takes care of the worst case scenario like 1GB vector!
 
@Mgetz Basics. Pointers point to an address in memory with an associated value. A reference copies a pointer to a different name. The before name and the new name reference the same value in memory so if one changes the other changes (because they refer to the same thing).
 
I don't think "A reference copies a pointer to a different name" is accurate. A reference always points to an object. It can never be null. Pointers can be null.
 
for example: this is memory
 
There are other differences when you use them as function parameters. You cannot pass a temporary object to a function that accepts a pointer (because you can't take the address of a temporary) but you can pass it to a function that accepts a const reference.
 
mem loc - value
––––––––––––
0 -> 8
1 -> 2
2 -> "foo"
-------
(obv those are not real addresses)
 
6:09 PM
    do_something(&std::string("hello")); // won't compile if do_something(std::string*)
    do_something(std::string("hello")); // compiles if do_something(const std::string&)
 
@JBis so you're also aware that when you pass an object value in JS/Java you're actually passing a pointer?
 
@Mgetz "pass by reference" yes
 
so in C++ you can choose if that happens, and the immutability is deep
 
Usually we say "pass by reference" or "pass by value", i've not heard "pass by pointer"
 
so if I pass const foo& I can only access methods on foo that are annotated as const
 
6:15 PM
alright, ill be back soon with more questions, thanks for help!
 
6:47 PM
Hi all!
#pragma pack(push, 1)
struct ComponentHeader
{
    unsigned char  role;
    unsigned char  format;
};
#pragma pack(pop)
How portable is this between gcc - Linux and msvc - windows?
 
not
 
If using c++17, what can I replace this with? An operator << sequence?
(Inside an operator<<(istream&, ComponentHeader&) I guess?)
The other question is, how do I replace constructs like offsetof(ComponentHeader, format)?
 
@iksemyonov not sure what you mean?
 
@Mgetz Sorry, here's more context. This is a part of code that is meant to read bytes in a certain layout from memory into a struct, or a tuple of variables, in other words.
 
@iksemyonov apparently gcc supports this gcc.gnu.org/onlinedocs/gcc/Structure-Layout-Pragmas.html
 
6:53 PM
so does clang, that makes it supported on all 3 then I guess
 
@Mgetz I understand that. What I wonder about is, well.. this looks too low-level for a codebase written in C++? Or is this how this is done in production code?
 
@iksemyonov usually only for serialization or networking code
otherwise it's usually avoided at all costs
but no it's necessary
 
@Mgetz In my case, this is a part of code that reads values out of a binary file in a certain format. I.e. a parser of a binary protocol.
 
@iksemyonov be careful, make sure data is longer than what you're trying to read into
otherwise you can end up with a heartbleed
this is why generally most people are lazy and use protobuf for binary protocols these days or BSON
 
@Mgetz This is being read out of a buffer in memory that is first filled using a read() on a file, sure.
 
6:57 PM
@iksemyonov still double check the read length and errno
 
@Mgetz C++, no errno :) I get the idea, thanks for the hint! I'm going to double check that.
 
regardless I'd suggest using protobuf and a protobuf library
rather than rolling your own
 
There are a couple levels of segmentation involved, you know, groups of segments, then segments themselves, etc.. so there is something to watch after, indeed.
 
unless it's an existing format... and if it is... treat every read from the file like it's nuclear waste
 
To sump up what we got so far.
Option 1. Protobuf.
Option 2. Leave `#pragma pack` be as it is right now.
Option 3. ? (probably the one I was looking for, if it exists)
 
7:01 PM
@iksemyonov BSON is an option too, alternatively text formats
 
Option 3 would be to read every member individually
 
and extremely carefully
 
@Mgetz I'm parsing this :)
@PeterT Using operator<<, correct?
 
@iksemyonov then treat each field like nuclear waste until you've validated things
@iksemyonov .read
 
@Mgetz that would be the case when reading from the file directly, however, there are couple levels of segmentation (and defrag) involved. It's simple and linear, but it's there.
 
7:03 PM
wut
 
Parsing an ancient binary format used to store some data in a specific domain
 
ok, but what's the difference between istream::read and istream::operator>> when you previously put stuff into a packed struct?
 
The code is written in C-style, not really portable C++. I'm updating, fixing it to match the protocol, etc.
@PeterT Not sure I understand the question honestly?
I mean, when stuff is in a memory buffer already, we can't use ifstream::read, can we? There has to be a different kind of istream for that? Sort of like stringstream, but a binary one.
 
well if it's already in memory then you can just memcpy it over
 
@PeterT To individual variables of primitive types, and then fill the struct by hand, right?
Currently, as you may guess, the code goes like header = (ComponentHeader)current;
 
7:11 PM
@PeterT hence my be careful warning... this is how heartbleeds happen
 
@Mgetz Now for the 2nd question. How can I replace offsetof? Is it portable?
 
@iksemyonov offsetof insofar is it's a POD yes it should be... ish
 
@Mgetz OK, nice, thanks.
I've read your warning, that will take another couple days of work to double-check. Are there any sanitizers that might help me with this?
 
PVS would be great if it was freely available
 
7:21 PM
honestly for this... fuzzers are still the best choice along with ASAN
 
@Mgetz Just to make sure: this code implements a parser, thus its behavior entirely depends on the input. Are sanitizers able to detect the kind of errors I'm looking for statically, without input data?
 
@iksemyonov that's where a fuzzer comes in
 
Hey, umm, a silly doubt:->
If I have a class A and class B inherits from it,
How do I make a protected member of class A (just one, from a dozen) private for class B?
 

« first day (1451 days earlier)      last day (1402 days later) »