본문 바로가기

Programming/C++ 1

[C++] Chapter 11. Pointers and Dynamic Memory Management

반응형
  • Pointer
  • Array & Pointer
  • Dynamic Memory Allocation

  • Pointer
    • Are designed to hold memory addresses as their vlaues
    • a pointer contains the memory address
  • Stack Memory vs Heap Memory
    • Stack memory
      • Compile time
      • Assignment by Computer
    • Heap Memory
      • Runtime
      • Assignment by Programmer

Memory draw

#include<iostream>
using namespace std;
int main()
{
	//Draw Memory contents 
	//on the left hand side : address , in the middle : variable name,
	//on the right hand side : memory contents
	//address of data[0] is 001FEE0

	int data[5] = { 11,12,13,14,15 };
	int a = -200;
	int& ref = a;
	char str[18] = "Dallas";
	short s = 5;
	double d = 78.32;



	return 0;
}

Declare a Pointer

char c='C';
char* pc=&c;

float f=700.5f;
float* pf=&f;

bool b=true;
bool* pb=&b;

short int s=456;
short int* ps=&s;

-> type info is not stored in memory

-> type info is in the pointer var itself

-> have to assign the address of the variable of the same type

 

Dereferencing [역참조]

*pointer
//p points to a
int a=123;
int* p=&a;

//dereferencing
cout<<"*p= "<<*p<<"\n";

*p=789;

//print
cout<<"a= " <<a<<"\n";
cout<<"*p= "<<*p<<"\n";

  • Arrays and pointers
    • array variable without a bracket and a subscript represents the starting address of the array
list+0 list+1 list+2 list+3 list+4 list+5
11 12 13 14 15 16

 

-> *(list+1) = 12

-> *list+2 = 14

 

#include <iostream>
using namespace std;
int main()

{
	double a = 58.2, b = 123.4, c = 32.2;
	double* D[3];
	
	D[0] = &a;//*(D+0) =&a
	*(D + 1) = &b;
	*(D + 2) = &c;
	for (int i = 0; i < 3; i++) {
		//index expression with array name
		cout << D[i] << " ";//print out memory contents of each elements of D, address of a,b,c
		cout << *D[i] << " ";//print out valuse of a,b,c with dereferencing of D, valuse of a,b,c
	}

	for (int i = 0; i < 3; i++) {
		//pointer expression with array name
		cout << *(D+i) <<" ";//print out memory contents of each elements of D, address of a,b,c
		cout << **(D+i) << " ";//print out valuse of a,b,c with dereferencing of D, valuse of a,b,c
	}
	cout << endl << endl;
	return 0;
}

-> swap pass by value, swap pass by reference, swap pass by pointer

 

  • array parameter or pointer parameter
void m(int list[], int size) = void m(int* list, int size)
void m(char c_string[]) = void m(char* c_string)

-> A[1] =*(A+1)

-> A[0] =*A

-> A[2] =*(A+2)

 

Pointer variable pointing an array element

int array[10];
int* p=&arrays[5]; //arrays+5
  • int array
    • int A[5];
    • type A : int*
    • type A[2] : int
  • int* B[5];
    • type B[2] : int*
    • type B : int **
#include <iostream>
using namespace std;

int main()
{
	//1. index expression with array name
	//2. pointer expression with array name, fill in with i*i
	//3. pointer expression with pointer, fill in with i*i*i
	//4. index expression with pointer,  fill in with i*i*i*i
	
	int A[10] = { -999 }; //A[0]is -999, rest of elemnets are 0 not garbage
	int* p = A;//&A[0]

	cout << "1. index expression with array name" << endl;
	cout << endl << endl;
	for (int i = 0; i < 10; i++)
	{
		A[i] = i;
	}
	for (int i = 0; i < 10; i++)
	{
		cout << A[i] << " "<<endl;
	} //fill in part
	

	cout << "2. pointer expression with array name, fill in with i*i" << endl;
	cout << endl << endl;
	for (int i = 0; i < 10; i++)
	{
		*(A + i) = i * i;
	}
	for (int i = 0; i < 10; i++)
	{
		cout << *(A+i) << " "<<endl;
	}

	cout << "3. pointer expression with pointer, fill in with i*i*i" << endl;
	cout << endl << endl;
	for (int i = 0; i < 10; i++)
	{
		*(p + i) = i * i*i;
	}
	for (int i = 0; i < 10; i++)
	{
		cout << *(p+i)<< " "<<endl;
	}

	cout << "4. index expression with pointer,  fill in with i*i*i*i" << endl;

	cout << endl << endl;
	for (int i = 0; i < 10; i++)
	{
		p[i] = i * i*i*i;
	}
	for (int i = 0; i < 10; i++)
	{
		cout <<p[i] << " "<<endl;
	}

	return 0;
}

 

#include<iostream>

using namespace std;

int main_()
{
	int a = 123;
	int* ip = &a;
	cout << "address of a : " << &a << endl;
	cout << "ip : " << ip << endl; //value of ip
	cout << "address of ip: " << &ip << endl; //8byte?
	cout << "ip + 1 : " << ip + 1 << endl;//bigger than ip by 1 *4
	cout << "ip + 4 : " << ip + 4 << endl;//bigger than ip by 4*4

	char c = 'p';
	char* cp = &c;
	cout << "address of c: " << &c << endl;
	cout << "address of c: " << (void*)&c << endl;

	cout << "cp " << &cp << endl; // \0가 없어서 가비지 void*
	cout << "cp " << (void*)&cp << endl; // \0가 없어서 가비지 void*

	cout << "address of cp: " << (void*)&cp << endl;
	cout << "address of cp: " << &cp << endl;
	cout << "cp + 1" << cp + 1 << endl; //Bigger than cp by 1*1
	cout << "cp + 1" << cp + 4 << endl; //Bigger than cp by 4*1
	cout << "cp + 1" << (void*)(cp + 1 )<< endl; //Bigger than cp by 1*1
	cout << "cp + 1" << (void*)(cp+ 4) << endl; //Bigger than cp by 4*1


	short s = 5;
	short* sp = &s; //2바이트

	cout << "address of s: " << &s << endl;
	cout << "sp " << &sp << endl;
	cout << "address of sp: " << &sp << endl;
	cout << "sp + 1" << sp + 1 << endl; //Bigger than sp by 1*2
	cout << "sp + 1" << sp + 4 << endl; //Bigger than sp by 4*2

	return 0;
}

/*
address of a : 0084FE80
ip : 0084FE80
address of ip: 0084FE74
ip + 1 : 0084FE84
ip + 4 : 0084FE90
address of c: p儆儆儆儆€?
address of c: 0084FE6B
cp 0084FE5C
cp 0084FE5C
address of cp: 0084FE5C
address of cp: 0084FE5C
cp + 1儆儆儆儆€?
cp + 1儆儆??
cp + 10084FE6C
cp + 10084FE6F
address of s: 0084FE50
sp 0084FE44
address of sp: 0084FE44
sp + 10084FE52
sp + 10084FE58
*/

 


  • Dynamic Memorry Allocation
    • int* result = new int[6]
    • delete [] result
    • int* p = new int;
    • delete p;
  • Static vs Dynamic memory allocation
int A[100];
A[99] =5000;

int* B;
int n;
cin>>n;
B = new int[n];
B[89] =50; //*(B+89)= 50;

-> Static : Compile time; Stack memeory

-> Dynamic : Run time; Heap memory

#include <iostream>
using namespace std;

int main_()
{
	int size;
	cout << "How many numbers do you want to enter?";
	cin >> size;
	int* ip = new int[size]; //Dynamic memory allocation
	
	cout << "Enter numbers";
	for (int i = 0; i < size; i++) //Use of the memory
	{
		cin >> *(ip + 1); //same expression : cin >>ip[i]
	}
	//print out sum and average
	int sum = 0;
	for (int i = 0; i < size; i++)
	{
		
		sum += *(ip + 1);
	}


	double Average = (double)sum / size;
	cout << "Sum=" << sum << " " << " Average :="<< Average << endl;

	delete[]ip; ip = NULL; //relase the memory

	return 0; //this func is all of use relase 
}

 

 

#include <iostream>
using namespace std;

int* func(int num); //prototype of function, pass by value

int main()
{
	int size;
	cout << "How many numbers do you want to enter?";
	cin >> size;
	int* ip = func(size); //Memory allocation is done in func()


	cout << "Enter numbers";
	for (int i = 0; i < size; i++) //Use of the memory
	{
		cin >> *(ip + 1); //same expression : cin >>ip[i]
	}
	//print out sum and average
	int sum = 0;
	for (int i = 0; i < size; i++)
	{

		sum += *(ip + 1);
	}


	double Average = (double)sum / size;
	cout << "Sum=" << sum << " " << " Average :=" << Average << endl;
	
	cout << "before delete ip: " << ip << endl;
	cout << "before delete *ip : " << *ip << endl;

	delete[]ip; ip = NULL; //relase the memory

	cout << "after delete ip: " << ip << endl;
	cout << "after delete *ip : " << *ip << endl; //찾을수없다


	return 0; 
}

int* func(int num)
{
	int* ret;
	ret = new int[num];
	return ret;
}

ReverseString

#include <iostream>
using namespace std;
char ReverseString(const char* src, int len)
{
	char* reverse = new char[len + 1];

	for (int i = 0; i < len; ++i)
	{
		reverse[i] = src[len - i - 1];
	}
	reverse[len] = NULL;
	return reverse;
}

int main()
{
	char original[] = "NEMODORI";
	char* copy = ReverseString(original, 8);

	cout << original << "\n";
	cout << copy << "\n";

	delete[] copy;
	copy = NULL;
	return 0;
}

ShiftString

#include <iostream>  
using namespace std;

char* ShiftRightString(const char* src, int len)
{
    char* ShiftRight = new char[len + 1];

    for (int i = 0; i < len; ++i)
    {
        ShiftRight[i] = src[i + 1];
    }
    ShiftRight[len - 1] = src[0];
    ShiftRight[len] = NULL;

    return ShiftRight;
}



int main()
{
    char original[] = "NEMODORI";
    
    char* copy = ShiftRightString(original, 8);
    char* copy2 = ShiftRightString(copy, 8); 
    char* copy3 = ShiftRightString(copy2, 8);
    char* copy4 = ShiftRightString(copy3, 8);
    char* copy5 = ShiftRightString(copy4, 8);
    char* copy6 = ShiftRightString(copy5, 8);
    char* copy7 = ShiftRightString(copy6, 8);
    char* copy8 = ShiftRightString(copy7, 8);

    cout << original << endl;
    cout << copy << endl;
    cout << copy2 << endl;    
    cout << copy3 << endl;
    cout << copy4 << endl;
    cout << copy5<< endl;
    cout << copy6 << endl;
    cout << copy7 << endl;
    cout << copy8 << endl;

    delete[]copy;
    delete[]copy2;   
    delete[]copy3;
    delete[]copy4;
    delete[]copy5;
    delete[]copy6;
    delete[]copy7;
    delete[]copy8;

    copy = NULL;
    copy2 = NULL;
    copy3 = NULL;
    copy4 = NULL;
    copy5 = NULL;
    copy6 = NULL;
    copy7 = NULL;
    copy8 = NULL;

    return 0;
}
반응형

'Programming > C++ 1' 카테고리의 다른 글

[C++] ShiftString  (0) 2021.04.07
[C++] ReverseString  (0) 2021.04.07
[C++] Swap Function  (0) 2021.04.04
[C++] Chapater 7. Single-Dimensional Arrays and C-Strings  (0) 2021.04.04
[C++] Chapter 6. Functions  (0) 2021.04.04