// File: /workspace/github_cpp_makefile_star2_O3/iZhangHui[P]CppPrimer/CH12_DynamicMemory/examples/oknew.cc
// Function: main
int main()
{
	// if allocation fails, new returns a null pointer
	int *p1 = new int; // if allocation fails, new throws std::bad_alloc
	int *p2 = new (nothrow) int; // if allocation fails, new returns a null pointer

	int i0;                  // named, uninitialized int variable

	int *p0 = new int;       // pi points to a dynamically allocated,
	                         // unnamed, uninitialized int
	delete p0;  // frees the memory to which pi points

	// named initialized variables
	int i(1024);             // value of i is 1024
	string s(10, '9');       // value of s is "9999999999"
	vector<int> v = {0,1,2,3,4,5,6,7,8,9};

	// unnamed, initialized dynamically allocated objects
	int *pi = new int(1024); // object to which pi points has value 1024
	string *ps = new string(10, '9');   // *ps is "9999999999"
	// vector with ten elements with values from 0 to 9
	vector<int> *pv = new vector<int>{0,1,2,3,4,5,6,7,8,9};

	cout << "*pi: " << *pi
	     << "\ti: " << i << endl
	     << "*ps: " << *ps
	     << "\ts: " << s << endl;

	for (auto b = pv->begin(); b != pv->end(); ++b)
		cout << *b << " ";
	cout << endl;

	// when we're done using the memory must delete the pointers
	delete pi;  // frees the memory to which pi points
	delete ps;  // frees the string to which ps points
	            // the string destructor frees the space used by its data
	delete pv;  // frees the memory for the vector
	            // which also destroys the elements in that vector

	// call get_size to determine how many ints to allocate
	int *pia = new int[get_size()]; // pia points to the first of these ints
	delete [] pia; // brackets used to delete pointer to element in an array
	typedef int arrT[42];  // arrT names the type array of 42 ints
	int *p = new arrT;     // allocates an array of 42 ints; p points to the first one
	delete [] p;           // brackets are necessary because we allocated an array

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
leaq -0x40(%rbp), %rax
movq %rax, -0x10(%rax)
leaq -0x50(%rbp), %rdi
movl $0xa, %esi
movl $0x39, %edx
callq 0x10d0
movups 0xd6a(%rip), %xmm0      # 0x2014
leaq -0xa0(%rbp), %rsi
movaps %xmm0, 0x10(%rsi)
movups 0xd48(%rip), %xmm0      # 0x2004
movaps %xmm0, (%rsi)
movabsq $0x900000008, %r15      # imm = 0x900000008
movq %r15, 0x20(%rsi)
leaq -0x78(%rbp), %rdi
leaq -0x29(%rbp), %rcx
movl $0xa, %edx
callq 0x158e
movl $0x20, %edi
callq 0x10a0
movq %rax, %rbx
movq %rax, %r13
addq $0x10, %r13
movq %r13, (%rax)
movl $0xa, %esi
movq %rax, %rdi
movl $0x39, %edx
callq 0x10d0
movl $0x18, %edi
callq 0x10a0
movq %rax, %r14
leaq -0xa0(%rbp), %rsi
movups 0xcf1(%rip), %xmm0      # 0x2014
movaps %xmm0, 0x10(%rsi)
movups 0xcd6(%rip), %xmm0      # 0x2004
movaps %xmm0, (%rsi)
movq %r15, 0x20(%rsi)
leaq -0x29(%rbp), %rcx
movl $0xa, %edx
movq %rax, %rdi
callq 0x158e
movq 0x2c8b(%rip), %rdi      # 0x3fd8
leaq 0xcd8(%rip), %rsi       # 0x202c
movl $0x5, %edx
callq 0x10c0
movq 0x2c73(%rip), %r15      # 0x3fd8
movq %r15, %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x1100
movq %rax, %r12
leaq 0xcb6(%rip), %rsi       # 0x2032
movl $0x4, %edx
movq %rax, %rdi
callq 0x10c0
movq %r12, %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x1100
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x1090
movsbl %al, %esi
movq %r12, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1060
movq %rax, %r12
leaq 0xc6d(%rip), %rsi       # 0x2037
movl $0x5, %edx
movq %rax, %rdi
callq 0x10c0
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
movq %r12, %rdi
callq 0x10c0
movq %rax, %r12
leaq 0xc4d(%rip), %rsi       # 0x203d
movl $0x4, %edx
movq %rax, %rdi
callq 0x10c0
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq %r12, %rdi
callq 0x10c0
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x1090
movsbl %al, %esi
movq %r12, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1060
movq %r13, -0x58(%rbp)
movq %rbx, -0x60(%rbp)
movq (%r14), %rbx
cmpq 0x8(%r14), %rbx
je 0x147a
movq 0x2b89(%rip), %r12      # 0x3fd8
leaq 0xbdf(%rip), %r13       # 0x2035
movl (%rbx), %esi
movq %r12, %rdi
callq 0x1100
movl $0x1, %edx
movq %rax, %rdi
movq %r13, %rsi
callq 0x10c0
addq $0x4, %rbx
cmpq 0x8(%r14), %rbx
jne 0x1456
movq (%r15), %rax
addq -0x18(%rax), %r15
movq %r15, %rdi
movl $0xa, %esi
callq 0x1090
movq -0x60(%rbp), %rbx
movq -0x58(%rbp), %r15
movsbl %al, %esi
movq 0x2b38(%rip), %rdi      # 0x3fd8
callq 0x1030
movq %rax, %rdi
callq 0x1060
movq (%rbx), %rdi
cmpq %r15, %rdi
je 0x14c0
movq (%r15), %rsi
incq %rsi
callq 0x10b0
movl $0x20, %esi
movq %rbx, %rdi
callq 0x10b0
movq (%r14), %rdi
testq %rdi, %rdi
je 0x14e1
movq 0x10(%r14), %rsi
subq %rdi, %rsi
callq 0x10b0
movl $0x18, %esi
movq %r14, %rdi
callq 0x10b0
movq -0x78(%rbp), %rdi
testq %rdi, %rdi
je 0x1503
movq -0x68(%rbp), %rsi
subq %rdi, %rsi
callq 0x10b0
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x151c
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x10b0
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r15
movl $0x18, %esi
movq %r14, %rdi
jmp 0x1545
movq %rax, %r15
movl $0x20, %esi
movq %rbx, %rdi
callq 0x10b0
jmp 0x1558
jmp 0x1555
movq %rax, %r15
jmp 0x156d
jmp 0x1555
movq %rax, %r15
movq -0x78(%rbp), %rdi
testq %rdi, %rdi
je 0x156d
movq -0x68(%rbp), %rsi
subq %rdi, %rsi
callq 0x10b0
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x1586
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x10b0
movq %r15, %rdi
callq 0x1110