How to debug ‘Stack smashing detected’?

If you read the website you will realize that this is a simple C++ wrapper over a C library. A typical issue with C library are buffer overruns: Try this program: Because the buffer can only contain 16 characters, the remaining characters will be written past its end. This is stack smashing, and undefined behavior. A number … Read more

std::string formatting like sprintf

Modern C++ makes this super simple. C++20 C++20 introduces std::format, which allows you to do exactly that. It uses replacement fields similar to those in python: Code from cppreference.com, CC BY-SA and GFDL Check out the compiler support page to see if it’s available in your standard library implementation. As of 2021-11-28, full support is only available in Visual Studio 2019 16.10, which … Read more

What does ‘\0’ mean?

C++ std::strings are “counted” strings – i.e., their length is stored as an integer, and they can contain any character. When you replace the third character with a \0 nothing special happens – it’s printed as if it was any other character (in particular, your console simply ignores it). In the last line, instead, you are printing a … Read more

Reading from text file until EOF repeats last line

Just follow closely the chain of events. Grab 10 Grab 20 Grab 30 Grab EOF Look at the second-to-last iteration. You grabbed 30, then carried on to check for EOF. You haven’t reached EOF because the EOF mark hasn’t been read yet (“binarically” speaking, its conceptual location is just after the 30 line). Therefore you … Read more

vector::size_type in C++

size_type is a (static) member type of the type vector<int>. Usually, it is a typedef for std::size_t, which itself is usually a typedef for unsigned int or unsigned long long.

What does -> mean in C++?

It’s to access a member function or member variable of an object through a pointer, as opposed to a regular variable or reference. For example: with a regular variable or reference, you use the . operator to access member functions or member variables. But if you’re working with a pointer, you need to use the -> operator: It can also … Read more

Callback functions in C++

Note: Most of the answers cover function pointers which is one possibility to achieve “callback” logic in C++, but as of today not the most favourable one I think. What are callbacks(?) and why to use them(!) A callback is a callable (see further down) accepted by a class or function, used to customize the current logic … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)