Enter any number of integers to end with EOF;
Purpose: The memory allocated to the array is exactly the total number of input integers +1, where the first element is the total number of input integers, followed by the input integer;
Such as
Input:
1 3 5 4 6 7
Output:
6 1 3 5 4 6 7
There is some basic knowledge about heaps and stacks:
The function body declares the array to be around 2M, while using dynamic allocation does not have this limit!!!
Heap Stack (stack)
I. Preliminary knowledge-memory allocation of the programThe memory used by a program compiled by C + + is divided into the following sections1. Stack (stack)-Automatically allocated by the compiler to release, store the function parameter value, local variable value and so on. It operates in a manner similar to a stack in a data structure. 2, Heap area (heap)-Generally by the programmer assigned to release, if the programmer does not release, the end of the program may be recycled by the OS. Note that it is not the same as the heap in the data structure, the distribution is similar to the list, hehe. 3, Global Zone (Static)-, the storage of global variables and static variables is placed in a block, initialized global variables and static variables in an area, uninitialized global variables and uninitialized static variables in another area adjacent. -System release after the program is finished4, literal constant area-the constant string is put here. Released by the system after the program is finished5. Program code area-binary code that holds the function body.
Example Program//main.cppint a = 0; global initialization zonechar *p1; global uninitialized zoneMain (){ int b; Stackchar s[] = "abc"; stackchar *p2; stackchar *p3 = "123456"; 123456\0 in the constant area, p3 on the stack. static int c = 0; global (static) initialization zoneP1 = (char *) malloc ();P2 = (char *) malloc ();areas that are allocated 10 and 20 bytes are in the heap area. strcpy (P1, "123456"), 123456\0 is placed in a constant area, and the compiler may optimize it with the "123456" that P3 points to as a place. }
attached code:
#include <cstdio> #include <cstdlib> #include <cmath> #include <map> #include <queue># include<stack> #include <vector> #include <algorithm> #include <cstring> #include <string > #include <iostream> #define MS (x, y) memset (x,y,sizeof) const int Maxn=100;const int inf=1<<30;using Namespace Std;int *input () {int *p = (int *) malloc (MAXN * sizeof (int));//First assign MAXN int if (p = = NULL) return null;int count = 1;while (scanf ("%d", P+count) ==1) {count++;if (Count >= maxn) {//Input quantity >=maxn, add 1 INTP = (int *) realloc at the end of the original memory block (p, ( count+1) * sizeof (int)); if (p = = null) return NULL;}} if (Count < MAXN)//Input quantity <maxn, remove excess memory at the end of the original memory block p = (int *) ReAlloc (p, Count * sizeof (int));p [0] = Count-1;return p;} int main () {//freopen ("In.txt", "R", stdin), int *p = input (), if (p = = NULL) {printf ("malloc failure\n"); exit (1);} printf ("%d", p[0]), for (int i=0; i<p[0]; i++) {printf ("% d", p[i+1]);} printf ("\ n"); return 0;}
Allocate memory by input integer number