« first day (555 days earlier)      last day (2300 days later) » 

2:55 AM
Hey
I'm trying to undertand how this works > stackoverflow.com/questions/2151084/…
say I have an int array[9][9]
items foes from 0-8 , 0,1,2,3,4,5,6,7,8
so if I have item arrray[2][2] = array[2][2]
the math to find it in linear array is
indexX * width + indexY
so
2*9+2
=20
god damint now it works ffs
well
crap, its 20 + 0,
that 0 is so annoying
 
 
3 hours later…
6:01 AM
CRT_INLINE int __cdecl getc (FILE* __F)
{
return (--__F->_cnt >= 0)
? (int) (unsigned char) *__F->_ptr++
: _filbuf (__F);
}
is this the ternary operator or is it used as something else
 
6:23 AM
why so many pi packages are written in python
 
@CoderCat Looks like a ternary operator from here. Implementation of getc or thereabouts?
 
searching the exact same key phrase in google and duckduckgo gives me two sets of totally different results ...
 
6:59 AM
yeah
its stdio raw sauce
 
Foo a, b;
a = Foo(b);
a gets destroyed after this :(
 
it got reassigned
 
a should be a copy of b
but the destructor of a is called right after the assignment
 
use static
 
lolol, got pi cam running with my 1st python script $
 
7:06 AM
do you know what the static keyword is
 
i'm not sure how to use it in this context
 
in that case you dont get what static does
 
 
1 hour later…
8:23 AM
@CoderCat please
@CoderCat are you trolling?
 
 
6 hours later…
2:49 PM
Hmmm...
T val;
memcpy(&val, src, sizeof(T));
val is accessed two times, right?
 
why are we using memcpy
 
@milleniumbug What are the adventages of std::copy over memcpy?
 
it actually copies the stuff instead of moving around bytes
also works on non-contiguous iterators
rather, why would you use memcpy in the first place
 
proper use of the copy constructor and no danger of getting the number of bytes wrong
 
@milleniumbug What do you mean by non-contiguous iterators?
 
2:53 PM
N days ago: You don't need C-arrays. Next day: uses C-arrays
 
6
A: Memcpy vs Memmove - Debug vs Release

MgetzThe following answer is valid for VS2013 ONLY What we have here is actually stranger than just memcpy vs. memmove. It's a case of the intrinsic optimization actually slowing things down. The issue stems from the fact that VS2013 inlines memcopy like thus: ; 73 : memcpy(store[i % 100], ...

you'll note that the comments make it pretty clear that just using std::copy is the best choice as it allows the library/compiler the most options to optimize the crap out of it
 
For example, you can't use memcpy to copy a linked list
 
@milleniumbug Thanks for claryfing it out, I was using it without knowing what's its name :P
@Mgetz Oh, cool...
 
@SzymonMarczak if you actually peek into an implementation they use a lot of type traits to figure out what you're copying and if it's safe to do binary copies or not. The compiler can then always issue the best choice for what you're doing.
 
What I want to achieve is for example get int16 from char[2]
But I'm not sure if I'm doing it right, I guess not.
 
2:58 PM
@SzymonMarczak ah... undefined behavior
 
@Mgetz Why?
 
@Mgetz actually char* is allowed to be used to peek and poke bytes in a struct
it's the one exception to strict aliasing
 
@ratchetfreak it is, but the byte order is undefined it's up to the implementation to decide how that will work out
@SzymonMarczak Endianness
 
@SzymonMarczak (a[0] << 8) | (a[1] << 0) for big endian and (a[1] << 8) | (a[0] << 0) for little endian
 
@Mgetz I use GCC's __builtin_bswap16 to swap bytes :)
*reinterpret_cast<int16*>(arr + index) doesn't look good IMO
 
3:01 PM
@SzymonMarczak that's great if you're always on a big/little endian system, but that's not portable
 
@Mgetz yup
 
also using compiler extensions removes even more portability
 
I agree with that
 
unless you #ifdef around it for every variant
 
@ratchetfreak which removes the whole point of having standards compliant portable code...
 
3:03 PM
But let's take this portability away for a while... I just want to get int16 from char[2]. Swapping bytes if needed.
 
2 mins ago, by milleniumbug
@SzymonMarczak (a[0] << 8) | (a[1] << 0) for big endian and (a[1] << 8) | (a[0] << 0) for little endian
 
@SzymonMarczak do an endianness test, then do what @milleniumbug suggested
 
the "endianess test" is for the input data
not your machine
 
that is portable, and IIRC technically standards compliant
 
@milleniumbug Alright, but what about int32 and int64? Implementing these all functions for setting and getting would be hundreds lines of code...
 
3:05 PM
more like 7 lines
 
ignoring mixed endianess I'm pretty sure @milleniumbug is right
 
Yup, about that...
@milleniumbug But what about performance? As far I as know doing it this way is slower than memcpy... Or am I wrong?
 
it's also correct
 
I'm not aware of any modern machine that uses mixed endianness, so unless you're coding for a PDP-11 you're fine
@SzymonMarczak compilers are really good at recognizing common patterns, don't try to help the compiler
 
i have a question, when we do, for example, a fopen oof a file, are we loading the hole file to the memory if we open a non binary file, or are we justmaking a pointer to the begining of the file in the disk?
 
3:20 PM
neither
the os creates a handle so it knows what file you are refering to and prepares to load the file to memory
when you fread it will copy the contents of the file into the buffer you provided
 
Also, why char a[8000000]; causes a crash? Just want to notice that char *a = new char[8000000]; (yes I remeber about delete[] a) doesn't crash. I'm getting Process finished with exit code -1073741571 (0xC00000FD) on MinGW64 on Windows.
 
nwp
Because windows has a ~1MB stack size.
 
@nwp Using new works fine
 
because heap has more contiguous space it can acquire
 
nwp
Using new uses the heap instead of the stack which doesn't have that limitation. At least not on reasonable systems that can run windows.
 
3:30 PM
I understand now.
 
Jan 13 at 12:30, by milleniumbug
lol don't use new
 
@milleniumbug What's the argument for not using it?
 
you will forget to delete
or delete it twice
 
leaking by default, exception unsafety, lacking any useful features
 
or mix up delete[] with delete
 
3:32 PM
Dec 19 '17 at 23:57, by milleniumbug
if sizeof(std::array<T, N>) goes over several kilobytes, you want to switch to std::vector<T>, even if you know the size at compile time
Dec 19 '17 at 23:58, by milleniumbug
(to avoid stack overflows for local variables)
 
@SzymonMarczak please use std::vector
 
Oh guys you're so wise, just being curious, how did you learn all these things? :O
 
half of the transcript of this room can be reduced to "don't use new"
 
@SzymonMarczak getting yelled at by @puppy
and LRIO
 
the other half is "I know but fucking uni hasn't updated their learning materials for 20 years"
 
3:35 PM
Haha
@milleniumbug That's true, I took part in a contest and they were still using GCC 4... :'(
 
there's also a percentage of language laywering in there
 
the other thing that taught me was a few videos on exception safety, where it was basically pointed out that making new exception safe was extraordinarily painful. So don't use it.
 
Sep 15 '17 at 16:04, by milleniumbug
@StephanHofmann If you need to release anything, you're doing it wrong. (see: RAII)
new is a special case of ^
 
if you need to write a destructor... you're probably doing it wrong
 
3:38 PM
oh yeah owning raw pointers are Rule of Zero violators
 
@milleniumbug Do you have a book where you store your quotes or something like that? :P
 
I use "search transcript" (top right corner) feature to look up what I had said previously
@SzymonMarczak passing knowledge from more experienced people in the chat to the less experienced people
 
@milleniumbug Oh, nowadays internet is so great...
 
it's a waterfall of knowledge basically
 
3:58 PM
@milleniumbug What if... I want to pass an array to my function? Then I need to pass it as a pointer.
 
make the function take a reference
 
@ratchetfreak But arrays don't have fixed size... I can make it [almost] any size I want.
 
then pass a std::vector
 
@ratchetfreak ok
@milleniumbug Are there any arguments for using new? If it exists then there should be some good points...
 
nwp
You need it to implement std::vector.
 
4:05 PM
@nwp not really, std::vector delegates to the allocator
 
nwp
Well, technically you don't, you use an allocater, but that allocator needs to use new.
I'm bad today. Allocators don't necessarily allocate using new either.
But there is a point in that rambling somewhere.
 
Ok, vector is one of the best solutions. But what's the use of the new keyword?
 
nwp
Let's say you use new to implement low level structures.
 
it's from days gone by when smart pointers weren't around
 
@ratchetfreak Sh*t, forgot about them
 
4:11 PM
Aug 18 '17 at 12:55, by milleniumbug
The reason is new is the late 80s and std::unique_ptr is early 10s, and Qt is late 90s
still, std::vector is late 90s too
 
@milleniumbug but at least that withstood the test of time
 
nwp
std::vector improved with C++11 significantly though. Arguably not the same thing as the 90s version.
 
@nwp This may be a dumb question, what are low level structures? I just know that they're a bit complicated.
 
@SzymonMarczak you need it if you're implementing a memory management primitive
 
primitive
 
4:13 PM
well, not new, rather, placement new
@SzymonMarczak smart pointers and like
 
so I should avoid primitive memory management
 
nwp
@SzymonMarczak Things like std::make_unique and std::allocator. Things that don't do anything besides handling bytes and placing objects into them.
 
because there are better methods for allocating memory right?
 
yes, containers and smart pointers
see: RAII
 
@milleniumbug dat thing?
 
4:16 PM
though it can be worth it to pay some attention to how memory is allocated and making sure you aren't triggering a deep recursion on a scope exit
 
@ratchetfreak what's deep recursion on a scope exit ?
 
dunno
 
nwp
@SzymonMarczak Not necessarily better, but there are situations when new is not ideal and it matters enough to use something else.
 
never encountered that
no, seriously, don't use new
it's a minefield
 
when you have effectively a linked list of objects which refer to the next with unique_ptr
 
4:18 PM
@milleniumbug minefield haha xD
 
I'm not kidding
 
if you destroy the root it recurses down to the tail to destroy it
 
intentional death? :P
suicide
 
generally don't implement graphs or trees with smart pointers, use an actual container
 
@milleniumbug sometimes they just appear through bad design
 
4:21 PM
@ratchetfreak K, understand that but can you explain what linked list of objects is?
 
object A has a unique_ptr to object B which has a unique_ptr to object C which has a unique_ptr to object D which has a unique_ptr to object E etc.
 
@ratchetfreak Ok, so if I destroy the root it's falling down?
and it will crash?
 
@SzymonMarczak not necessarily but it it's probable that it will blow the stack
 
4 mins ago, by milleniumbug
generally don't implement graphs or trees with smart pointers, use an actual container
 
4:24 PM
@ratchetfreak Will it try to access outbound data? Then it'll crash probably
@milleniumbug Remembered that already :)
 
nwp
@SzymonMarczak Falling down? Destroying element 1 destroys element 2 which destroys element 3 and so on. In theory everything gets destroyed properly no problem. In practice after calling enough destructors you run out of stack space.
 
@nwp no root = no element 1
It'll behave chaotic probably
 
nwp
I don't know what you are talking about.
 
I'm mistaken.
and confused
12 mins ago, by ratchet freak
though it can be worth it to pay some attention to how memory is allocated and making sure you aren't triggering a deep recursion on a scope exit
I still don't know what deep recursion on a scope exit is.
 
nwp
That is what my example was trying to illustrate. You have something like struct Node { std::unique_ptr<Node> next; }; which produces a chain of elements. Then the first Node goes out of scope which causes a chain reaction which deletes all the Nodes.
The problem is that you keep calling the destructor of Node recursively which might eventually make you run out of stack, depending on how many elements you have in that chain.
 
4:34 PM
@nwp That's the answer I wanted to hear
 
nwp
The solution is usually to use a vector<Node> to store all the nodes and then use a regular dumb pointer for the next pointer. That way the Nodes are deallocated sequentially and not recursively which uses a fixed amount of stack.
 
though with vector you can get into trouble if it ever needs to grow
 
nwp
True. You can use an index instead, and not remove nodes.
 
there are other containers which are referentially consistent on add and remove that don't have that problem
@nwp a freelist of nodes will let you recycle them
 
@ratchetfreak What's a freelist?
Generally speaking, low level functions are faster than high level? Or that's only a myth?
 
4:41 PM
@SzymonMarczak A linked list on the side where you keep the discarded nodes
 
@ratchetfreak What's the use of freelist?
Or when should I use it?
 
reusing discarded nodes
when you create a node-based container which has a lot of churn and want to avoid allocation overhead every time you add or remove
"churn" being additions and removals
 
ok
but what's the real example?
Can you provide a real example?
@ratchetfreak You can use pastebin
 
4:55 PM
or coliru ;D
@ratchetfreak Ok, I understand it, but it would be great if you could illustrate it :D (for example you would use it if you had to do ...?) Sorry if I'm bit annoying :P
Also, what does ~0u mean?
First weird thing I've ever seen
 
~ is bitwise negation
 
what about u?
 
that means the 0 is unsigned
 
4256
Q: The Definitive C++ Book Guide and List

grepsedawkThis question attempts to collect the few pearls among the dozens of bad C++ books that are published every year. Unlike many other programming languages, which are often picked up on the go from tutorials found on the Internet, few are able to quickly pick up C++ without studying a well-written...

 
I used that as the sentinal value for NULL
 
5:05 PM
@milleniumbug I'm reading Thinking in C++ btw
 
?
0 is int
0u is unsigned
 
size_t is always unsigned
if you remove the u the compiler complains abount mixing signed/unsigned in comparison because it's set to pedantic
 
@ratchetfreak Ok, but why did you use -pedantic?
 
because otherwise you can accidentally rely on non-portable constructs
 
and it's the default on coliru
 
5:09 PM
in fact, don't leave your house without -Wall -Wextra -pedantic
 
@milleniumbug what are non-portable constructs?
 
the compiler features which are not portable
int n = 42; int a[n]; // there, invalid C++, but gcc won't complain about it without -pedantic because it's a gcc compiler feature, doesn't matter that it won't work on Visual C++
 
@milleniumbug even with /permissive-?
 
? It not working on Visual C++ is a feature
VLAs are abhorrent
 
I derped the better question is does it fail to compile with -std=c++17
as I believe it defaults to -std=gnu-17
 
5:16 PM
@Mgetz nope, not enough. also needs -pedantic
 
@milleniumbug seems like a bug, as that's ill-formed IIRC in C++17
> This feature was briefly part of the C++14 working paper, but was not part of the published standard; as a result, it has been removed from the compiler.
 
@ratchetfreak Finally I have understood what a freelist is. It's used in Box2D.
lol different forums (outside stackexchange) have different methods for naming links, ()[], []() etc.
 
@SzymonMarczak welcome to the wonderful insanity of markdown
 
@ratchetfreak so I should avoid using smart pointers when I develop a freelist, right?
 
5:35 PM
@SzymonMarczak depends on what you mean... you can do something like github.com/hsutter/gcpp
which is designed for something like what you're doing
 
ok
if UTF8 string ends with \0, then empty string contains exactly one byte, right?
 
nwp
By "contains" you mean "requires storage"? Because an empty string obviously doesn't contain anything.
 
null terminated strings end with \0
non-null terminated strings don't end with \0
being UTF-8 is irrelevant
 
k, I've been mistaken :D
@milleniumbug does std::string end with \0?
 
it has a \0 character on a .size() position, to facilitate interop with C functions
still, it's not a "null terminated string"
because a \0 in a middle of it doesn't change anything, it doesn't truncate the string
the only thing that changes is how the C functions interpret it
 
5:47 PM
@milleniumbug Is there any string which is null terminated?
 
only in C
 
@SzymonMarczak not-specified if accessing via &foo[0] from pre C++14, specified to be zero terminated if accessing afterwards
 
Sep 11 '16 at 12:21, by milleniumbug
No one likes C strings
 
nwp
@SzymonMarczak "Hello" for example.
 
@nwp what about std::string hello = "hello"?
 
5:50 PM
@milleniumbug I've seriously started using constexpr std::string_view literals for all of those
 
What's the use of constexpr?
 
nwp
Although I'm not sure what breaks if you write like "Hello\0World".
 
@SzymonMarczak constexpr auto BAR_CHOICE = "foobar"sv;
 
nwp
@SzymonMarczak The right side is null terminated the left side is not. The point is that the right side says "The string ends when the first '\0' character is encountered" while the left side says "The string is exactly hello.size() long and it doesn't matter if and where '\0' characters are".
 
6:02 PM
@nwp So what's the destiny of null characters in std::string?
 
nwp
It is a regular character like any other.
I assume by destiny you mean purpose.
 
@nwp Exactly :)
 
@nwp so std::string foobar = "hello\0world"sv; in theory will have the whole string including the null
 
nwp
It probably does in practice too.
 
What does std::string_view stand for?
 
6:36 PM
Just thinking... Is it possible to learn all of C++?
 
all of C++? no
and it's fine though
 
@SzymonMarczak probably not, and you don't need to
 
6:58 PM
Just did a small test including 10 000 000 iterations, and std::copy was 2x faster than memcpy :O Switching to it right now...
It depends on what you need - in some cases it can be 2x slower.
 
nwp
You probably forgot to turn on optimizations or measured wrong. std::copy should never be faster than std::memcpy.
 
Probably the second option - now it shows it's same as memcpy... And yes, I forgot to turn on optimizations :D
Let's try again...
@nwp Nevermind, now it shows it's a little slower than memcpy :D
Optimizations were turned on
 
nwp
7:14 PM
Check godbolt. Ideally they should compile to the same instructions.
 
@nwp I heard about it too :)
 
7:33 PM
I hate CMake
 
cmake may suck, but cmake >> make >> manual compilation on the command line
 
 
1 hour later…
8:35 PM
Hi folks
I'm trying to delete all the zeros in front of a C++ vector
I tried this:
while(*it == 0) {
    v.erase(it);
    ++it;
}
where: vector<int>::iterator it;
It doesn't work
Could someone please tell me why?
 
read about iterators invalidation :p
 
8 messages moved from Lounge<C++>
 
replace the body of your loop by it = v.erase(it); and it should work
 
@Morwenn or better yet use the std::remove erase paradigm
 
yeah, there's also that
 
8:38 PM
Thanks @Morwenn
 
@Platus Even assuming that v.erase(it); didn't invalidate it (it does), this would be a terrible idea as a rule. To both make it work and make it substantially more efficient, start by finding the first non-zero item, then erase everything up to that point in one call.
 
in which case you'd do v.erase(std::remove(v.begin(), v.end(), 0), v.end());
 
auto it = std::remove(c.begin(), std::find_if(c.begin(), c.end(), [](int x){ return x != 0; })); c.erase(it, c.end()); derp, that std::remove isn't actually necessary
 
aaand it would probably be faster and much more correct
 
@Mgetz In this case, the remove/erase idiom doesn't really apply--he only wants to removing the zeros at the beginning, not the rest of the vector.
 
8:40 PM
(all in the front)
 
Ok, thanks all
I'm a C++ noob actually, I just use the language to practice algorithms
For job interviews
Do you know some good C++ tutorials?
 
@milleniumbug This works too, but it's a bit easier to just do: auto it = find_if(...); c.erase(c.begin(), it);
 
yeah :D
4256
Q: The Definitive C++ Book Guide and List

grepsedawkThis question attempts to collect the few pearls among the dozens of bad C++ books that are published every year. Unlike many other programming languages, which are often picked up on the go from tutorials found on the Internet, few are able to quickly pick up C++ without studying a well-written...

don't get a tutorial, they'll invariably suck because people who write online tutorials aren't paid, unlike people who sell books
also there's no "official" tutorial so there
 
I don't know about good tutorials for algorithms in C++, but that SO Q&A about implementing classic sorting algorithms in standard actually had good examples
 
@Morwenn yeah it was great
279
Q: How to implement classic sorting algorithms in modern C++?

TemplateRexThe std::sort algorithm (and its cousins std::partial_sort and std::nth_element) from the C++ Standard Library is in most implementations a complicated and hybrid amalgamation of more elementary sorting algorithms, such as selection sort, insertion sort, quick sort, merge sort, or heap sort. The...

 
8:45 PM
oh, you were faster than I could be ^^"
 
9:05 PM
@Platus woah. How do you practice algorithms without command of the tools. Ay, that's probably just me.
 
9:44 PM
Hi again
I'm having trouble trying to compare 3507897352 and -1
 
nwp
Hint: It's false
 
My program considers -1 as the greatest number
Those two values are assigned to long long int variables
 
nwp
Can you show some code? 3507897352 is not out of range of long long int on desktop platforms.
 
It's inside the customCompare function
 
nwp
customCompare uses ints. Your compiler should have given you a warning due to loss of precision.
 
9:53 PM
Yes indeed, thanks @nwp
 
 
2 hours later…
11:38 PM
I would like to compile my program with variables from an xml configuration file. What's the best way of doing this? I have thought of a 'pre-build' step to read the xml file and generate a header file which can then be included in the building of my program.
 
Whoo boy, you opened a can of worms there son
 
do it at runtime
 
@nobism If you want, you can use a preprocessor if you so desire
 
Exhibit A for the can of worms, there are reasons to do runtime, compile time config, or both. You need to identify use case
there are more ways to handle configs than there are HTML parsers
 
probably some constexpr magic
 
11:44 PM
I'm just gonna leave this here
 
Main.c of the future
`<?php generate_program() ?>
Who needs C anymore
 
I don't want to do it at run time, for various reasons. I want to build my program with different configurations and just have the binary. I'll look into the preprocessor, but what about my suggestion about generating a header file separately beforehand from the xml file?
I also can't use any 3rd party libraries. For reasons that I can't get into.
 
you know, it's okay to say "I have this assignment in class and I don't know how to approach it"
 
Yes I know, this is real life work though.
 
@nobism If this is for CI/CD I would just to envsubst :)
Building an intermediate XML layer seems like overkill UNLESS you already have said XML schema in use for other purposes
 

« first day (555 days earlier)      last day (2300 days later) »