A few discussions about C + +

Source: Internet
Author: User

1#include"string.h"2#include"malloc.h"3 4 5 voidSwapintAintb)6 {7     inttemp;8temp=A;9A=b;Tenb=temp; One } A  - intGet_int (inta) - { the     intI=1+A; -     returni; - } -  + Char*get_memory0 () - { +     Char* P= (Char*) malloc (sizeof(Char)* -); Astrcpy (P,"Hello World"); at     returnp; - } -  - Char*Get_memory1 () - { -     Char* p="Hello World"; in     returnp; - } to Char*Get_memory2 () + { -     Charp[]="Hello World"; the     returnp; * } $ Panax Notoginseng voidMain () - { the     intx=4, y=3; + swap (x, y); A     intz=x-y; theprintf"z=%d\n", z);//Question 1 +  -z=Get_int (z); $printf"z=%d\n", z);//Question 2 $  -     Char* C0 =get_memory0 (); -printf"c0=%s\n", C0);//Question 3 the  -     Const Char* c1=get_memory1 ();Wuyiprintf"c1=%s\n", C1);//Question 4 the  -     Char*ConstC2=Get_memory2 (); Wuprintf"c2=%s\n", C2);//Question 5 -  AboutC1=C2;//6 $*C1=*C2;//7 -C2=C1;//8 -*C2=C1;//9 -}

  • The core of C: functions, variables, and pointers
  • Stacks: Stores that are typically used for variables that can determine the storage size during compilation, are used for the storage of variables that are created within the scope of a function and are automatically destroyed after leaving the scope, are contiguous. Two variables defined next to each other, storage spaces are also next to each other. There is a limit to the size of the stack.
  • Heap: A store that cannot determine the size during compilation, the storage space is not contiguous, usually by malloc () or new () to allocate memory blocks, and need to use free () or delete () to release memory. If it is not released, there is a memory leak problem. Two pointer variables that are defined next to each other are not necessarily the same size as the two block of memory that is pointing to malloc.
  • Global/Static storage: Global variables and static variables that are visible throughout the program's run.
  • Constant storage: Constants in the store are also globally visible, and they contain constants that are not allowed to be modified.
  • Each runner has its own run-time "stack", and all of its functions get a chunk of storage allocated to him at run time from the program stack. Starts with the function and ends with the function ending, and the store is freed automatically when the function ends.
  • Parameter passing is the process by which the system initializes the function parameter store with a function argument.
  • Parameter pass-through and parameter address.
  • An understanding of the return value of a function. The return value of a function has both a value and a pass. How can a parameter's return parameter be a local variable? Are local variables automatically destroyed after they leave the scope? The return value is a function that returns a temporary variable at the return of the function to hold a copy of the local variable to be returned.
  • A temporary variable is not named, and its value (rvalue) is stored in the caller's "stack", which is assigned an lvalue as the right value when the function returns.
  • The 21st line of the program means allocating a chunk of memory and making the variable p point to this heap of memory, 22 rows to copy the string into the heap memory pointed to p, and 23 lines to indicate that the lvalue of P is a local pointer, stored on the stack of the function, and the value of the heap address is "Hello World", and the string is stored in
  • The function char * get_memory () is a function that returns a pointer to a value.
  • In general, the meaning of a variable is that it provides a name to a memory store, which allows the program to read and write to the memory. The variable contains two values, an lvalue and a right value. The left value is the name of the memory store, and the right value is the value stored in the store.
  • The problem 5,c2= unknown.
  • Constant pointer: A pointer to a constant in which the contents of the address pointed to by the pointer are non-modifiable. such as Char const *p;7 is wrong.
  • Pointer constant: The constant of the pointer, which is a pointer to an immutable address, but can be modified to what it points to. such as Char *const p;8 is wrong.

A few discussions about C + +

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.