Why apply for dynamic memory allocation rights: I think I have no experience at all. My program almost did not apply for dynamic memory in the heap, all are built in the stack array, but the length of the array is limited, according to the test shows that the storage capacity of the array is about 400KB, so in the implementation of the encryption and decryption program, only to achieve the requirements, and did not use the dynamic allocation technology. Assuming an integer variable stores a Chinese character, the number of characters that can be stored is approximately 100,000 Chinese characters, so the number of dynamically allocated stores is huge. My program should try to use and know how to release it.
int* number=new int [100000000], can apply 10 of the 8 square integer variable storage space
int number[100000], you can request a variable storage space of 20 of 5 square integers
The gap between the two is very large, but we need to consume the CPU to allocate memory as a precondition:
Here is a comparison of the two in time:
#include <iostream> #include <string.h> #include <time.h>using namespace std;int Main () { //declares the test program start time and end time variable clock_t start,finish1,finish2; start=clock (); // Defines 5,000 pointers to strings string* pstr[5000]; //request memory in the heap, store string variables for (int i=0;i<5000;i++) { pstr[i]=new string; } finish1=clock (); for (i=0;i<5000;i++) { *pstr[i]= "AAA"; } //destroys the pointer and reclaims the heap area's memory for the next use of for ( i=0;i<5000;i++) { //the following two modes of operation, in the end which is the correct //delete[] pstr[i]; //Error delete pstr[i]; } finish2=clock (); cout<<finish1-start<<endl; cout<<finish2-finish1< <endl; return 0;} The test results show that the system consumes 3 milliseconds to create the heap area object and 19 milliseconds to destroy the heap area object///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////#include <iostream> #include <string.h> #include <time.h>using namespace std;int main () { //Declare the variables for the test program start time and end time clock_t start,finish1; Start=clock (); //request memory in the stack, store string variables for (int i=0;i<5000;i++) { string s= " AAAA "; } finish1=clock (); cout<<finish1-start<<endl; return 0;} The test results show that the system consumes 10 milliseconds to create the elements of the stack object, and at the same time, it already contains the times for destroying those objects.
Dynamic memory allocation PK stack allocation (performance or flexibility)