I
I
Insolent Muzzle2021-07-08 15:54:48
C++ / C#
Insolent Muzzle, 2021-07-08 15:54:48

Why is everything like this in C++?

There will be several questions at once, some of them may relate not only to pluses.
1. Why are const char* and char* types not compatible?

char* foo = "foo";
const char* bar = foo; // Ошибка

Why is this happening? Both are strings, but why aren't they compatible?
2. Can the code below be considered a copy constructor? 0 is of type int, right? 3. How does the code above differ from this: Or from this Or from this Or from this in general What is the difference between these options for initializing a variable and which one is better to use? 4. In continuation to the question above. Why do we use = in array initialization? Why is it not customary to do so? 5. Why do we need void pointers, what do they do? Yes, how can you point out what is not there? I heard that void actually takes 1 byte. Is it real?
int zero = 0;
int zero(0);
int zero = int(0);
int zero{0};
int zero = int{0};
int array[] { 1, 2, 3};
6. This applies not only to the pluses, but still. Why do we need two types of increment/decrement (postfix and prefix)?
7. Why does this code output 1?
#include <iostream>

class Empty {};

int main(int argc, char** argv)
{
  std::cout << sizeof(Empty{});
}

8. I read somewhere that return from main calls std::exit. If so, how does it work if you do not include standard libraries? Where does the function come from? (The same can be asked about std::terminate, std::abort)
9. How is std::abort implemented in general? How does it work?
10. How does I/O work? How can the functions of the standard library put something somewhere if nothing has already been implemented for it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
1
12rbah, 2021-07-08
@pluffie

https://stackoverflow.com/questions/9834067/differ... the answer to the first question
to your initialization questions
1
2
Well, you can put almost all your questions into Google and it will give you an answer.

M
Mercury13, 2021-07-13
@Mercury13

1. Const-correctness was invented by Bjarne Stroustrup, and only ≈1999 it was introduced into C without crosses. And since the memory occupied by a string literal cannot be changed (and in modern operating systems, literals sit in a special immutable segment), of course, const char * has become a new type for them. Because C99 allowed writing for compatibility , and C(++) development stalled for a decade, C textbooks took a very long time to write this deprecated line. 3. For small objects, this is the same thing, but for large objects there is a considerable difference. This program compiles starting with C++17.char* text = "foo";

#include <iostream>

class WrapInt
{
public:
    int value = 0;
    WrapInt(int x) : value(x) {}
    WrapInt(WrapInt&) = delete;
private:
};

int main()
{
    WrapInt x = 42;
}

int zero = 0;before C++14, create a temporary object, call the move constructor, and the optimizer does the rest. From C++17, a constructor call if it is not explicit. It seemed to me that in very old versions of DJGPP op= was involved, but XEZ, MinGW and C++03 call the constructor.
int zero = int(0); - which is surprising, the same, only any constructor! Although it looks like an explicit creation of a temporary object.
int zero(0);— constructor call in all versions of C++.
int zero{0}; — C++11 universal initializer, also a constructor call.
4. — all versions of C and C++. is a C++11 universal initializer. 6. Edsger Dijkstra toldint array[] = { 1, 2, 3};int array[] { 1, 2, 3};
7. void is a pseudo-type whose elements cannot be created. Pointers can be moved in increments that are equal to the size of the type - that is, add 1, actually add 1, 2, 4, 8, or whatever. For void*, which is a simple memory pointer, the stride is 1 byte. Or 1 word, if the machine operates with larger words - there were such, but due to overcomplicated work with text, they were abandoned. But void* increment-decrements, if possible, are not desirable, and here's why.
To "connect hedgehog to snake" (long word and byte access), x86 says: dealing with unaligned data (eg 4 bytes at address 21) will be slow and undefined. Itanium in general, EMNIP, does not access unaligned data, as it is arranged in compilers: char* → int* says that you need to access it slowly and programmatically, void* → int* - quickly and in hardware.
8. libc may not be included, although it is difficult. And return from main does NOT call std::exit. The linker creates a binding code that opens all global objects, calls main), closes all global objects, and exits the program system-wise.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question