« first day (1458 days earlier)      last day (1400 days later) » 

8:54 AM
To split a 32-bit integer into it's higher 16-bit and lower 16-bit ends,
this is what one can do:

`high = (number & 0b11111111111111110000000000000000) >> 16` and
`low = (number & 0b00000000000000001111111111111111) >> 0`

but of course that's a lot of typing for the binary numbers.
So how does one generate such binary as a single expression instead?
Example: 0b100000000000000000000000000000000 can be expressed as 1 << 32, instead
 
just use hexadecimal numbers :P
0xFF00
0x00FF
or if you want to confuse people ~0 << 16
 
9:09 AM
```
high = (number & 0xFFFF0000) >> 16
low = (number & 0x0000FFFF) >> 0
```
is definitely more readable and thanks for reminding me about the complement (`~`) operator to swap the bits
 
9:25 AM
we need new numeric literals, hexadecimal gets a little unreadable too with things like SIMD registers. Base64 is too unreadable with its upper and lowercase letters, so I propose Base32 :P
written as 32bVV00
oh wait that would mean 5-bit packets per symbol instead of nibblets, nah get that decimal seeming stuff out of here
 
Lol
Didn't Alan Turing calculate everything in base 32 (or is that an "old wives' tales" thing)?
 
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <cstdlib>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
is there not a more concise way of using things like map and set?
 
That's a lotta #include's
 
yeah
#include <map>
#include <set>
 
exactly
@PeterT not i mean is there not an include which includes map,set,....
its from theree ^^
 
9:35 AM
none that is standards compliant
 
omg
 
You also seem to be mixing C++ and C style functions
 
If it has to be that many `#include`'s, you could keep everything in a single header file:

`#include <2000differentlibraries> // -> don't have 2000 libraries pls...`
for example
 
@Mikhail its not mine, its from the link
 
if you keep finding yourself including so many headers you can just use precompiled headers for you project
 
9:36 AM
Definitely ^
 
^ Nope
 
Why not? 🤔
 
Those functions are template heavy
Also that isn't really that many headers
better strategy is to split the cpp files into functions that only use separate headers, but once again those headers are pretty common. and probably don't slow stuff down too much. You may find yourself really struggling to avoid them (for example map or vector).
 
@Mikhail Oh
@Mikhail "split the cpp files into functions that only use separate headers", how so?
 
So for example, the headers associated with std::cout<< might only apply to a few logging, printing functions, so make a separate cpp with that functionality.
 
9:42 AM
So, for example:

`logger.cpp` will include `<iomanip>` & `<iostream>` for it's logging functions and
the entry cpp file will include `logger.cpp`?
 
No, so if you have a function called cow.cpp you can have your printing headers in cow_say.cpp, that way cow.cpp doesn't have iostream, for example.
 
Oh I see, and then all the .cpp files are linked up by the compiler?
 
i just wanted to know whether there is a libary which is a superset of some of these #include's
ill assume no
 
@Mikhail Something along the lines of gcc cow_say.cpp cow.cpp -o cow
@Permian I don't think there is, no
 
thanks
 
9:46 AM
Some header files do include other header files but there's no single header file for <map> and <set> AFAIK
 
@Permian In that specific case you're mixing C style functions and C++ functions. For example, #include <cstdio> is C style.
 
where?
 
Instead of ctime maybe chrono
bunch of cmath stuff is now in <numbers> or something like that
 
ive got a good question
#include <stdio.h>
#include <string.h>
#include <time.h>

char *get_date()
{
    char    buf[80];
    time_t  now = time(0);
    strcpy(buf, ctime(&now));
    return buf;
}

int main(int argc, char *argv[])
{
    char *date = get_date();
    printf("date=%s\n", date);
    return 0;
}
whats wrong with this code? apparently its to do with the printf
 
nwp
Your compiler should simply tell you what's wrong with the code.
 
9:50 AM
char buf[80] is a local variable in the char* get_date() function so it gets destroyed when you return it from the function
To preserve the buffer (so it doesn't get destroyed) char buf [80] you can either make it static or dynamically allocate it via malloc() or new
 
"The problem was that when printf is called, it needs a part of the stack, and this may affect the stack-reserved array of characters buf."
which i dont understand
 
nwp
It's wrong. It's written by someone who doesn't understand C.
 
@Permian I don't think that's the problem. I'd assume it's the fact that buf is getting destroyed when its returned because it's not reserved in memory
 
nwp
The correct answer is that the error is in get_date returning an invalid pointer which you then pass on to printf. Not printfs fault.
 
9:52 AM
but the buf is defined outside the function strcpy
so would be in that scope
 
nwp
The incorrect answer which is what they are trying to say is that it's fine to access invalid memory under certain conditions. In this case they imagine buf to be on the stack and the pointer to still be valid because the stack still exists, even if C says the memory is inaccessible and doing it anyways is UB. Then you run printf which overwrites the stack and exposes the error. But again, that's not printf's fault.
 
char* strcpy(char* destination, const char* source); is the def of strcpy
so should return a pointer to destination
 
@Permian Yes it is defined outside strcpy(...) but it's defined within the get_date() function which destroys (corrupts) the buf variable after it is returned
That's why printf() prints out garbled text probably, because buf has been destroyed
 
why?
that makes no sense to me
buf is local to the get_date function and this function would return buf
 
nwp
@Permian True. When strcpy is called everything is fine and buf is valid and filled. But after get_date ends all its local variables stop existing, so now and buf are now gone.
 
9:56 AM
@nwp whats the point of anything returning then?
 
nwp
There is none. It's a programming error.
 
i dont understand this at all
you return something to leave something after you have finished the function
 
nwp
Maybe the core of the issue is that you cannot return C arrays by value.
 
buf is a pointer though
 
If you want to get the value(s) you've stored in buf, you need to allocate it in memory
 
nwp
9:57 AM
buf is not a pointer. It's an array.
 
oh i see
good problem
 
@Permian does this look right to you?

int* doStuff(){
int* a = new int[40];
delete[] a;
return a;
}
 
An array that gets destroyed when get_date() returns
 
@PeterT im really bad though
but no
 
char* get_date() {
    static char buf[80];
    time_t now = time(0);
    strcpy(buf, ctime(&now));
    return buf;
}
or
char* get_date() {
    char *buf = (char*) malloc(80 * sizeof(char)); // pray it doesn't evaluate as `NULL`
    time_t now = time(0);
    strcpy(buf, ctime(&now));
    return buf;
}
 
10:01 AM
@Lapys what about in c++11 + ?
 
Either code snippet should work as you intend. Although I'd recommend checking up on arrays & pointers in C
 
You'd just return a string by value probably
 
char * array = new buf[80] ??
 
@Permian That would work for C++
 
yeah i want c++
 
10:03 AM
std::string get_date(){
time_t now = time(0);
return ctime(&now);
}
 
char *buf = new char[80] {0}
But you might as well use std::string if it's C++
 
which would be how?
 
It would be as @PeterT's example
 
omg i didnt know there were two ways
ive used the more c like version of c++ in the past
 
C-style coding in C++?
 
10:07 AM
yeah
didnt use the stl at all
c++ 03 it probably was
 
That's usually tricky unless one knows exactly what they're doing.
Handling pointers properly, checking the error indicator and so on...
 
im trying to get fresh again
 
Ah, then good luck re-discovering C++ (or C) 👍
 
my biggest problem my mind is a mish mash of languages
that i have largely self taught
 
So got this working (for the lower bits of a number):
```
int get_low(int number, int count = sizeof(int) / 2) { return number & ~(~0 << count); }
```

but I'm still getting off-by-one errors (for the higher bits of a number):
```
int get_high(int number, int count = sizeof(int) / 2) { return (number & ~((~0 << sizeof(int)) - (~0 << count))) >> count; }
```
 
10:14 AM
lol, don't do the ~0 thing for calculations, I said "if you want to confuse people" which is pretty much never
 
not to mention that it should probably be ~0U
 
-1 then? :)
 
    do not use raw pointers.
    do not use C style strings.
    do not use raw arrays.
    do not use macros.
    do not manually manage memory.
no3) and no5) haha
and no2) and no1)
 
breh, I'm not gonna help you get your pseudo-obfuscatet bit-math to work
 
10:16 AM
@Permian You may wanna post this in Lounge<C++>: chat.stackoverflow.com/rooms/10/loungec instead
 
sorry
 
Lol, it's alright :)
@PeterT Lol, back to Googling then XD
I think this is it:

`high = number >> 16u;`
`low = number & ~(~0u << 16u);`
 
10:46 AM
Hey guys, I was wondering about this answer : stackoverflow.com/a/52037432/3753446. Why would the code throw if the lambda has automatic storage duration and doesn't go out of scope ?
 
nwp
A temporary std::function is created to which an invalid reference is kept. Also the temporary lambda's life ends at the next ;, but that's not a problem, because std::function makes a copy (using a move) of it.
 
Why is the reference invalid ? I thought the temporary lambda's life ends at }
 
nwp
> F f([]{ std::cout << "Hello World!" << std::endl;});
In this line the lambda dies at the ;.
But that's fine.
> F(const std::function<void()>& f) : myF(f) {}
In this line the temporary std::function to which f is bound dies at the }. But myF is a reference which outlives that std::function. That's the problem.
C++ says this is UB. It can call the lambda, throw an exception, crash or do something entirely different.
 
@nwp Can you source this ? cppreference says the opposite if I'm not mistaken (en.cppreference.com/w/cpp/language/storage_duration, "automatic storage duration. The storage for the object is allocated at the beginning of the enclosing code block and deallocated at the end. All local objects have this storage duration, except those declared static, extern or thread_local. ")
@nwp and why is there a difference in lifetime between the lambda and the temporary bound to f ?
 
nwp
11:50 AM
@Magix The lambda is of type lambda (unspeakable unique type) and the thing passed to F's constructor is an std::function. It's kinda like void f(const double &dr); int i = 3; f(i);. The compiler will create a temporary double to bind dr to because a double & cannot bind to an int. That temporary can die even if i is still alive.
I'm not entirely sure how that doesn't contradict your quote, but my guess is that your quote talks about allocation while mine talks about object lifetimes. So if you had the code
void f() {
    blargh(i);
    int i = 0;
}
then your quote says the memory for i is allocated at the { and deallocated at the }.
However, the lifetime of i only starts after the int i despite the memory for it having been allocated before hand, thus you cannot access i like I tried to.
So if I guessed correctly the memory for the lambda is still around after the ; until the } but the lambda itself is gone. Though ... I don't know if that interpretation is actually correct. Probably not.
 
 
4 hours later…
3:32 PM
Interesting !
 
 
2 hours later…
5:23 PM
Would it be consistent if `for` or `while` loops never existed?

```
// If `while` loops never existed, same syntax for if `for` loops never existed
for (...; ...; ...) ...;
for (...) ...;
do ...; for (...);
```
¯\_(ツ)_/¯
Just curious
 

« first day (1458 days earlier)      last day (1400 days later) »