C/C ++ basic pen question 1.1.2 (solution of operator 10) and 1.1.2

Source: Internet
Author: User

C/C ++ basic pen question 1.1.2 (solution of operator 10) and 1.1.2

I wrote an irresponsible blog post in the previous section and was trampled on it, causing a little loss.

In fact, it is really not good to simply ask questions, because a great advantage of reading blog is to see what you want to see more directly.

Well, return to the entire question.

1. What operators are in your mind, sorted by priority?

There is obviously no standard answer to this question, but the most basic one is to have it.

For example, +,-, *,/, %, front ++/--, rear ++/--, =, <, >,<, >> ,(),&, ^, | ,~ And comma "," Domain Name character ":", &, |, and so on.

This may be a little difficult, but you can certainly do it in a simple experiment.

The so-called priority order can be viewed at the end of the blog.


2. What is the difference between malloc/free and new/delete? (I have no questions ?!)

Both functions apply for/release dynamic arrays. However, malloc/free is a function of stdlib and the header file stdlib needs to be added, while new/delete is an operator.

1. malloc/free is the method (function) in C/C ++, and new/delete is the operator in C ++.

2. the memory space used by malloc/free is the heap zone, and new/delete is the free store zone.

3. free can be released only when it is determined that it is not null, but delete is not required.

4. In addition, new/delete is used to call constructor/destructor, while malloc/free is only used to allocate memory and therefore cannot meet non-Internal data requirements.

3. Let you write an overloaded function ++ I, I ++. How do you write it?

This is basic skill

/*************************************** * *******************> OS: linux 3.11.10-301. fcw.x86 _ 64 Fedora20> Author: yaolong> Mail: dengyaolong@yeah.net> Time: thursday, July 17, 2014 ******************************** * *************************/# include <iostream> # include <cstdio> # include <string >#include <cstring> using namespace std; class A {public: int x; A (int x = 0) {this-> x = x;} A operator ++ (int) {// I ++, returns the replica first, and then returns the original object A tmp = * this; this-> x ++; return tmp;} A operator ++ () {// ++ I, directly modify the object this-> x ++; return * this ;}}; int main () {A a1 (3); A a2 = a1 ++; cout <a2.x <endl; // 3 cout <a1.x <endl; // 4 A a3 = ++ a2; cout <a3.x <endl; // 4 cout <a2.x <endl; // 4}

4. Check a program.


#include<iostream>  using namespace std;    int main(){      int x=3;      cout<<x+x%2==1<<endl;       return 0;  }  

This program cannot run normally. The compilation is incorrect. The priority is %, +, <, =

So the entire sub-interpretation should be

X % 2-> 1

X + 1-> 4

1 <endl Error

Let's assume that the formula is like this,

Cout <(x + x % 2 = 1 <2) <endl;

In this way, the entire statement is as follows:

Because () has the highest priority (in this formula ),

X % 2-> 1

X + 1-> 4

1 <2-> 4

4 = 4 get 1

Subsequent operators <

However, <is left-to-right, so cout <1 and then output the transpose

The output is

1

When we write

Int y = x + x % 2 = 1 <2;

Cout <y <endl;

There is no error at this time. Why? Because the priority of = is very low, only the comma is lower than it, there is only one

Aren't we always doing this?

Int x = 3, y;

5. Let's see another one?What's different from what you think?

#include<iostream>  using namespace std;  int main(){      int x=3;      cout<<((x+x%2)==1)<<endl;      cout<<(x+(x%2==1))<<endl;    return 0;  }  
The above analysis is easy to understand.

First, execute the innermost layer brackets.

(X + x % 2)

% Ratio + takes precedence, so it is

X % 2-> 1

X + 1-> 4

Outer parentheses

4 = 1-> 0 (false)

The first output is 0.

The second is very easy to know.

X % 2-> 1

1 = 1-> 1 (true)

X + 1-> 4

Second output 4

6. Is it a bit difficult to feel the nausea of commas and parentheses?

#include<iostream>using namespace std;void fun0(int x,int y ,int z){  cout<<x<<" "<<y<<" "<<z<<endl;  int a=2,b=3,c=7;  x=a*b,c;  y=(a*b+z,z=c);  cout<<x<<" "<<y<<" "<<z<<endl;}void fun1(int x,int y,int z){  cout<<x<<" "<<y<<" "<<z<<endl;  int a=2,b=3,c=7;  y=(x=a+b+z),(z=b+c);  cout<<x<<" "<<y<<" "<<z<<endl;}void fun2(int x,int y,int z){  cout<<x<<" "<<y<<" "<<z<<endl;  int a=2,b=3,c=7;  y=((x=a+b+z),(z=b+c));  cout<<x<<" "<<y<<" "<<z<<endl;}int main(){    int x,y,z;    fun0(x,y,z);    fun1(x,y,z);    fun2(x,y,z);}

This question is too much, because the correct answer cannot be written by handwriting, and the output result pages of different compilers are different.

See fun0 (x, y, z)

Because x, y, and z have no initial values, the first output x, y, and z of each function are the default values of the compiler.

 int a=2,b=3,c=7;  x=a*b,c;  y=(a*b+z,z=c);
First, declare a = 2, B = 3, c = 7

X = a * B, c

Because comma = has a lower priority than =, as mentioned earlier, = is the second lowest, and comma is the first, x = 6. c has not changed.

Y = (a * B + z, z = c)

Run the brackets first

(A * B + z, z = c)

The number is run from left to right. If you are interested, you can test the number of p (p = a * B + c, z = c ).

So execute the expression first.

A * B + z, get 2*3 + z (z is a messy number, And my GCC compiled 32767 ),

Then execute z = c, and (expression 1, expression 2, expression ......) The last expression is returned.

So y = z = c, y = 7, z = 7

The first fun0 is output

0 0 327676 7 7
For fun1 and 2, the priority is equal to =.

The result can be inferred easily.

The total output is

0 0 32767327736 7 70 0 3276732772 32772 100 0 3276732772 10 10
A fun3 is attached below to test the execution of the preceding comma from left to right.

void fun3(int x,int y ,int z){      cout<<x<<" "<<y<<" "<<z<<endl;      int a=2,b=3,c=7,p;      x=a*b,c;      y=(p=a*b+z,z=c);      cout<<p<<endl;      cout<<x<<" "<<y<<" "<<z<<endl;}

7. We use % for the remainder operation. % can only be applied to positive integers, right?

In this case, you can pull it by yourself. You can operate on non-zero integers, but not on floating point.

8. domain operator: we often use this operator. Can I reload it?

No. Some operators cannot be overloaded. For example, in this question: And. At the end of the article, we will mention whether reload can be performed.

9. When the operator overload is used as a non-member function overload, must it be defined as a friend function?

No. When the operation member is public, you do not have to define a friend function.

10. Must operators be symbols? (That is, can only be +,-, *,/,:, and so on ?)

No, the article mentioned at the beginning that new/delete is an operator. There are also sizeof, and forced conversion operators.






How to Learn embedded systems?

You know what an embedded system is, and there are many embedded systems, such as industrial control (commonly used: single-chip microcomputer, PLC), and peripheral mobile phones (commonly used: ARM, OMAP ), digital Signal Processing (commonly used: DSP, FPGA. Some embedded Foundation, C language programming, single chip microcomputer principle, digital circuit, analog circuit, etc. Single-Chip Microcomputer entry is relatively simple, to meet the general industrial control is almost the same, learning C language, buy a Single-Chip Microcomputer Development Board (8051, AVR, STM32) to learn for half a year to a year, can also have a small score, if you have a monthly salary of 4-6 K, you can get the price now. The development of mobile phones is also good, but it is best to submit a training course. This course is specialized in a platform, such as IPHONE or ANDROID. If you become an embedded expert, it will be difficult. You must be familiar with digital circuits, analog circuits, and digital signal processing, and the knowledge is the same, like my boss, I am a technical leader of about 50. I started to work on 8051 when I had a single-chip computer. I used to compile the fixed-point LIB for digital signal processing, many algorithms for digital signal processing are well-known, and DSP is very involved. The company just took over a billion-level case and used radio, satellite, DSP, VPN, and industrial PC, the boss is busy telling us about algorithms and architecture. Embedded is quite interesting.

Embedded Learning

You can try to buy an arm9-based Development Board first, and then do a few things.
1. learning to write a bare-metal arm program, that is, without an operating system (such as a lawn lamp or a digital tube), what to learn at this stage, there is no need to go over first, at the same time, I learned to use a simulator to simulate and burn programs.
2. Be familiar with the arm architecture and learn the storage mechanism of arm, including ram, flash, and sdram.
3. Learn bootloader and arm assembly
4. Download the linux embedded video tutorials online. There are a lot of tutorials on the plug-ins. Generally, we talk about porting uboot, driver development, and linux.
5. Finally, I learned how to program linux applications.
Of course you can be a versatile player, but I think it is better to be specific when it comes to your personal energy. As for the application software, you have time to learn it again.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.