Summary of C/C++ Language Fragmentary Knowledge Points (1)

Summary of C/C++ Language Fragmentary Knowledge Points (1)

  In the past three years of university study, I have encountered a lot of knowledge and problems scattered, but some will be forgotten after a long time. In order to overcome this problem, I thought that I could use a blog to keep records, which is convenient for vagueness. When I go back and learn again.

First, look at the code:
	# include <stdio.h>
		
		int main(void){
    
    
		
			char side_a[] = "Side A";
			char dont[] = {
    
     'w','o','w','!'};	
			char side_b[] = "Side B";
			
			puts(dont);
			return 0;
			
		}

Insert picture description here
  Would you be a little surprised at the results of the operation, don’t you think that the output of the puts function should not be the "wow!" string in dont? Why is there still Side A, and why can’t it be Side B behind?

  Don't worry, the blogger will answer for you.

  Knowledge point 1: puts() function analysis:

  I believe that the pretty girls who passed by this blog post at this moment must already be familiar with the correct use of the puts() function. If you don’t understand it, it’s okay. The blogger is very careful and has attached a learning link.

  1. puts() function Baidu Encyclopedia
  2. C language puts() function usage details

  Let's return to the topic, yes, the most important point of the puts() function is that the puts function will not end until it encounters the terminator "\0", otherwise, the computer will look down in the memory until it encounters an empty space. The character ends, so if there is no end sign, the puts function may output garbled characters.

  Knowledge point 2: The storage area of ​​each variable of C/C++ is different.

  The storage area of ​​each variable in the C/C++ program in the memory:

  1. Memory stack area: store local variable names
   2. Memory heap area: store new or malloc objects;
   3. Constant area: store the value of local variables or global variables;
   4. Static area: store global variables or static Variable;
   5. Code area: binary code.

  In addition, C/C++ does not have a garbage collection mechanism. Therefore, the heap data needs to be destroyed and recycled in time to prevent memory leaks. It is the familiar free() and delete() functions, and the stack memory is dynamically released. .
  Okay, lovely, after the above brief introduction, you must have guessed the reason for the result of the code, hahaha, let me reveal the answer together.

Insert picture description here

  First of all, all declared are local variables, so they are all allocated space in the memory stack. And there is no string terminator'\0' in the dont array, so after using the puts() function, it will search backwards in the memory stack for strings containing the terminator'\0' and output them together.
  Also, because the side_a variable is pushed onto the stack before the dont variable, side_a is located after dont, and the pointer p just has the terminator'\0' when looking backwards to side_a, so the puts() function runs to side_a when it is the real end. .
  Okay, the blog post has been summed up intermittently. Today is also New Year’s Eve, I wish you all a happy Chinese New Year, and let the "cow" turn the world! !

Guess you like

Origin blog.csdn.net/qq_43515862/article/details/113756677