// File: /workspace/github_cpp_makefile_star2_O1/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 $0x88, %rsp
leaq -0x40(%rbp), %rax
movq %rax, -0x10(%rax)
leaq -0x50(%rbp), %rdi
movl $0xa, %esi
movl $0x39, %edx
callq 0x10d0
movups 0xd67(%rip), %xmm0      # 0x2014
leaq -0xb0(%rbp), %rsi
movaps %xmm0, 0x10(%rsi)
movups 0xd45(%rip), %xmm0      # 0x2004
movaps %xmm0, (%rsi)
movabsq $0x900000008, %r15      # imm = 0x900000008
movq %r15, 0x20(%rsi)
leaq -0x80(%rbp), %rdi
leaq -0x29(%rbp), %rcx
movl $0xa, %edx
callq 0x15bc
movl $0x4, %edi
callq 0x10a0
movq %rax, %r13
movl $0x400, (%rax)          # imm = 0x400
movl $0x20, %edi
callq 0x10a0
movq %rax, %rbx
addq $0x10, %rax
movq %rax, -0x68(%rbp)
movq %rax, (%rbx)
movl $0xa, %esi
movq %rbx, %rdi
movl $0x39, %edx
callq 0x10d0
movl $0x18, %edi
callq 0x10a0
movq %rax, %r14
leaq -0xb0(%rbp), %rsi
movups 0xcda(%rip), %xmm0      # 0x2014
movaps %xmm0, 0x10(%rsi)
movups 0xcbf(%rip), %xmm0      # 0x2004
movaps %xmm0, (%rsi)
movq %r15, 0x20(%rsi)
leaq -0x29(%rbp), %rcx
movl $0xa, %edx
movq %rax, %rdi
callq 0x15bc
movq 0x2c74(%rip), %rdi      # 0x3fd8
leaq 0xcc1(%rip), %rsi       # 0x202c
movl $0x5, %edx
callq 0x10c0
movl (%r13), %esi
movq 0x2c58(%rip), %r12      # 0x3fd8
movq %r12, %rdi
callq 0x1100
movq %rax, %r15
leaq 0xca0(%rip), %rsi       # 0x2032
movl $0x4, %edx
movq %rax, %rdi
callq 0x10c0
movq %r15, %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x1100
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x1090
movsbl %al, %esi
movq %r15, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1060
movq %rax, %r15
leaq 0xc57(%rip), %rsi       # 0x2037
movl $0x5, %edx
movq %rax, %rdi
callq 0x10c0
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
movq %r15, %rdi
callq 0x10c0
movq %rax, %r15
leaq 0xc37(%rip), %rsi       # 0x203d
movl $0x4, %edx
movq %rax, %rdi
callq 0x10c0
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq %r15, %rdi
callq 0x10c0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x1090
movsbl %al, %esi
movq %r15, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1060
movq %r13, -0x58(%rbp)
movq %rbx, -0x60(%rbp)
movq (%r14), %rbx
cmpq 0x8(%r14), %rbx
je 0x1490
movq 0x2b73(%rip), %r13      # 0x3fd8
leaq 0xbc9(%rip), %r15       # 0x2035
movl (%rbx), %esi
movq %r13, %rdi
callq 0x1100
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x10c0
addq $0x4, %rbx
cmpq 0x8(%r14), %rbx
jne 0x146c
movq (%r12), %rax
addq -0x18(%rax), %r12
movq %r12, %rdi
movl $0xa, %esi
callq 0x1090
movq -0x60(%rbp), %rbx
movq -0x58(%rbp), %r15
movsbl %al, %esi
movq 0x2b21(%rip), %rdi      # 0x3fd8
callq 0x1030
movq %rax, %rdi
callq 0x1060
movl $0x4, %esi
movq %r15, %rdi
callq 0x10b0
movq (%rbx), %rdi
movq -0x68(%rbp), %rax
cmpq %rax, %rdi
je 0x14e8
movq (%rax), %rsi
incq %rsi
callq 0x10b0
movl $0x20, %esi
movq %rbx, %rdi
callq 0x10b0
movq (%r14), %rdi
testq %rdi, %rdi
je 0x1509
movq 0x10(%r14), %rsi
subq %rdi, %rsi
callq 0x10b0
movl $0x18, %esi
movq %r14, %rdi
callq 0x10b0
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x152b
movq -0x70(%rbp), %rsi
subq %rdi, %rsi
callq 0x10b0
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x1544
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x10b0
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r15
movl $0x18, %esi
movq %r14, %rdi
jmp 0x1570
movq %rax, %r15
movl $0x20, %esi
movq %rbx, %rdi
callq 0x10b0
jmp 0x1585
jmp 0x1582
jmp 0x1582
movq %rax, %r15
jmp 0x159a
jmp 0x1582
movq %rax, %r15
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0x159a
movq -0x70(%rbp), %rsi
subq %rdi, %rsi
callq 0x10b0
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x15b3
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x10b0
movq %r15, %rdi
callq 0x1110
nop