What is the meaning of unsafe in C++? [duplicate] - c++

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why is strtok() Considered Unsafe?
I have just noticed a warning (using Visual Studio) that strtok is unsafe, and strtok_s is not. Why is it unsafe and what is unsafe?
First part of my question is answered here but what is the meaning of unsafe and what are the problems and possible problems related to it?

strtok is not thread-safe. If two or more threads call strtok at the same time, the results will be undefined. I am reproducing here an answer by another user, Jonathan Leffler:
Be aware that strtok() destroys its input as it processes it. It is
also not thread-safe, and you have to be sure that no other function
that you call from your parser uses strtok(), and that no function
that calls your parser uses strtok(). The condition on functions
called is usually not too onerous; in library code, though, the second
condition (no calling function also using strtok()) is not
enforceable.
The response was given to this question: Dealing with input in C

Related

Is there any benefit to using `std::numeric_limits<uint64_t>::max()` instead of simply `UINT64_MAX`? (ie: `std::numeric_limits<>` vs cstdint) [duplicate]

This question already has answers here:
Are C++ Templates just Macros in disguise?
(25 answers)
Advantages of type traits vs static members?
(4 answers)
INT_[MIN|MAX] limit macros vs numeric_limits<T>
(5 answers)
C++: Why does numeric_limits work on types it does not know?
(1 answer)
Closed 2 years ago.
UINT64_MAX
is defined in <cstdint>: https://en.cppreference.com/w/cpp/header/cstdint.
This is what I've always used. However, I see C++ provides this alternative as well:
std::numeric_limits<uint64_t>::max()
(see: https://en.cppreference.com/w/cpp/types/numeric_limits/max), which is part of the many numeric limits options defined in the <limits> header: https://en.cppreference.com/w/cpp/types/numeric_limits.
The C++ version seems overly verbose to me and appears to have no benefits. I assume it's included simply to be thorough, since they were adding additional functionality via other numeric_limits<> created in C++, but wanted to cover everything, I imagine, while they were at it.
Is there any benefit of using the C++-only version (std::numeric_limits<uint64_t>::max()) over the C or C++ version (UINT64_MAX)?
I'd really prefer the UINT64_MAX version, but in general find a lot of pushback from hard-core C++ developers whenever they see me write something in C++ which also would be valid in C. :)
Note: I haven't had pushback on this one yet, but I'd like some insight first to see if there is something I'm missing which truly makes one version better than the other.
Update: here's my own answer:
I'm not too happy this question was closed literally in the very same second as I pressed the submit button to add my own answer, but here's my own answer to this question:
As #Bob__ pointed out in the comments under the question, std::numeric_limits<uint64_t>::max() is really preferred when writing a C++ template where the uint64_t part needs to be replaced with the typename T.
However, if not in a template, it seems to me UINT64_MAX would be preferred since it's short, straight-to-the-point, less verbose, and easier to read.

why is there no need for string library when using string vectors? [duplicate]

This question already has answers here:
Why does omission of "#include <string>" only sometimes cause compilation failures?
(7 answers)
Closed 2 years ago.
I was making some c++ programme where I pushed back strings into a string vector and I accidentally removed the string library from the file (#include <string>)... absolutely nothing happened at compile or run time and the programme executed successfully. Why is that?
That depends on what your standard library decides to include.
You should include string, but it may already be included somewhere, perhaps for exception handling. It will also depend on your compiler and your compiler version.

Is the main function callable in C++ or C [duplicate]

This question already has answers here:
Is it legal to recurse into main() in C++? [duplicate]
(5 answers)
Closed 7 years ago.
Can I call the main method in C/C++ from other functions. it seems to work but I do not know if it is a good software design in c++. please tell me the pros and cons ?
Thank you.
In C you can. In C++ you can't.
Quoting the C++ standard (ยง3.6.1.3):
The function main shall not be used within a program.
There's nothing in the C standard forbidding calling main.
Whether or not calling main is good design is quite opinion-based, but usually one would be better off using a loop instead.
According to C++ standard
5.2.2.9 "Recursive calls are permitted, except to the function named main"
You've already determined it is possible. However, it makes your entire program recursive. It also could make your code a little harder to understand.
So it's really hard for me to imagine any pros for this.

C++ - Create function at runtime [duplicate]

This question already has answers here:
Is it possible to create a function dynamically, during runtime in C++?
(14 answers)
Closed 8 years ago.
In C++, I have to make some user-defined actions on each cells of a big table.
Because of the size of the table, I'd like not to use interpreted instructions but to compile during runtime a function that I will call on each cell.
The user-defined actions are pretty simple :
if ((state1 && state2) || state3) then change_a_value_in_memory
That's why I don't need to use the LLVM or other JIT libraries.
I hesitate to just use mmap and add the code in hex directly.
I'd like to know if it exists better solutions, or, if not, where I can find the basic format of a C++ function code to directly write it in memory.
Thanks, and sorry for my english :/
This is not the most elegant, but it works always: generate a file with the C++ code of your function, call the compiler using system(), load the generated .so file using dlopen, and use the function!
This will take some time (due to the compiler call), but if you keep the function and maintain a database of the functions you already have, then you can save the amount of compilations.

What's the purpose of the "universal-character-name encountered in source" warning? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to address this instance of C4428 Visual C++ warning about a character literal?
If I compile the code:
wchar_t c = L'\u00A0';
with the /W4 flag, I get the warning:
Warning C4428: universal-character-name encountered in source
What I'm failing to understand is, what's the purpose of this warning? What mistake is it trying to prevent?
It's not really about mistakes.
To my understanding, the warning merely tells you that you've used the character code instead of the actual character. For most characters that's merely a matter of making things more readable, although in your case, the character will look the same as a normal space so using the code is actually a good idea (or even better, I like to make macros or enums with the unicode names, so it would be NO_BREAK_SPACE).
Either way, one could debate to no end whether the warning is actually useful for anything. Or you could just disable that particular one (there are probably more warnings you've already disabled, like the one about unused inlines etc.) and go on.