C + + Test preparation (i)

Source: Internet
Author: User

1. Determine if the following procedures can be correctly output

#include <stdio.h>#include<string.h>voidNew (Char*p) {P=New Char[5];}voidMain () {Char*p=NULL;    New (P); strcpy (P,"Hello");//requires header files Stdio.h and string.hprintf"%s", p);}

Answer: No. To change to:

void New (Char* &p) {    p=newchar[5];}

Additional knowledge:

New and delete

(1). New () Allocates a size memory space of this type, and initializes the variable with the value in parentheses;

Eg:int *a = new int (5)//Initialize an int pointer of size, pointing to a value of 5

Eg:string *str = new String ("Test new");

(2). new[] Allocates this type of n-size memory space and initializes these variables with the default constructor;

Eg:int **a = new Int[5][6]

(3) When a multidimensional array variable or array object is defined with the new operator, it produces a pointer to the first element of the array, and the returned type retains all dimensions except the leftmost dimension;

Eg:int (*P2) [Ten] = new INT[2][10]; Removing the leftmost dimension, P2 points to a int[10] pointer to a one-dimensional array, int (*) [10].

(4). cout<<*p<<endl; Just the first character of the string that the output P points to!

cout<<p<<endl; The string that the output P points to!

(5). delete [] p; Delete an array of P

Delete p; Delete a single P

Once you have deleted the object that the pointer refers to, set the pointer to 0 immediately

malloc and free

(1) After you have applied for memory space, you must check whether the assignment was successful .
(2) When you do not need to use the requested memory, remember to release; After release, the pointer to this memory should be pointed to null, preventing the program from accidentally using it.
(3) The two functions should be paired. If it is not released after the application is a memory leak, if it is released for no reason, nothing is done. release can only once, if released two times and more than two times an error (releasing null pointer exception, releasing the null pointer is actually equal to nothing, so release the null pointer released how many times no problem).

The difference between malloc and new

(1) New returns a pointer of the specified type, and can automatically calculate the desired size. malloc, however, has to be counted by us and forcibly converted to a pointer of the actual type after the return.

(2) malloc allocates memory and cannot initialize the resulting memory, so the value of the new memory will be random.

In addition to the method of allocation and final release, pointers are obtained through malloc or new and are consistent on other operations.

Why do you want to new/delete with Malloc/free?

(1) malloc and free are standard library functions for c++/c languages, and new/delete are operators of C + +. They can all be used to request dynamic memory and free memory.

(2) for objects with non-intrinsic data types , the light Maloc/free cannot satisfy the requirements of dynamic objects. objects are automatically executed when they are created, and the object executes the destructor automatically before it dies. because Malloc/free is a library function and not an operator, the task of executing constructors and destructors cannot be imposed on malloc/free, not within the control of the compiler.
Therefore, the C + + language requires an operator new that can perform dynamic memory allocation and initialization, and an operator delete that can perform cleanup and deallocation work. Note New/delete is not a library function.
We should not attempt to use Malloc/free to complete the memory management of dynamic object, should use New/delete. Because the "objects" of the internal data types do not have a process of construction and destruction, Malloc/free and new/delete are equivalent to them.
(3) Since the function of new/delete completely covered the Malloc/free, why C + + does not put malloc/free out of the elimination? This is because C + + programs often call C functions, whereas C programs can only use Malloc/free to manage dynamic memory.
If you release the dynamic object created by new with free, the object can cause a program error because it cannot execute the destructor. If you use Delete to release the dynamic memory for malloc requests, the result can also cause the program to fail, but the program is poorly readable. So new/delete must be paired and malloc/free the same.

C + + Test preparation (i)

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.