[C++] I have a simple question

Asked 2 years ago, Updated 2 years ago, 82 views

Hello, everyone

1

char test = 1;
std::cout << test << std::endl;

Only the new line comes out

Char is 8 bits and int is 32 bits, so I think data corruption occurs.

I think it's worth the garbage. I wonder why it's because nothing is filmed

2

    long test2[3];
    std::cout << test2[0] << std::endl; //0
    std::cout << test2[1] << std::endl; //0
    std:cout << test2[2] << std::endl; // garbage value

I wonder why the first two have zeroes

cout char c++

2022-09-21 18:13

1 Answers

Let's look at the first question.

The code below is the result of a c++ interpreter called cling. cling is clang based, clang supports C++11, C++14 and C++17. It is simply a compiler that complies with the standard of c++.

[cling]$ #include <iostream>
[cling]$ char test = 97
(char) 'a'
[cling]$ std::cout << test << std::endl
a
(std::basic_ostream<char, std::char_traits<char> >::__ostream_type &) @0x7fa1145c2e40
[cling]$ char test2 = 1
(char) '0x01'
[cling]$ std::cout << test2 << std::endl

(std::basic_ostream<char, std::char_traits<char> >::__ostream_type &) @0x7fa1145c2e40

If char is an integer, it is matched by ascii code.

Look at the ASCII table.

97 is lower case a and 65 is upper case A. What about 1?

Look at the ASCII table. It is called the ctrl+a character.

Number one would have made sense.

Actually, question number two can be difficult.

When storing variables in languages such as c/c++, use memory space called stack.

The problem is that when using this stack, it does not initialize. It's just a structure that overwrites a variable when it's a value.

In other words, variables that do not initialize may contain strange values. (A value already exists in that memory address.)

Later...Once you can handle the debugger, look at the value of each memory...You can understand and better understand the assembly language clearly when you study it.

To understand the c/c++ language well, you must study assembly.


2022-09-21 18:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.