์ƒˆ์†Œ์‹

Languages/C++

[C++] ๋™์  ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น | new delete | ๊ฐ์ฒด ๋™์  ์ƒ์„ฑ

  • -
๋ฐ˜์‘ํ˜•

๋™์  ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น

 

์ •์  ํ• ๋‹น

 

๋ณ€์ˆ˜ ์„ ์–ธ์„ ํ†ตํ•ด ํ•„์š”ํ•œ ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น

 

๋™์  ํ• ๋‹น

 

ํ•„์š”ํ•œ ์–‘์ด ์˜ˆ์ธก๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ. ํ”„๋กœ๊ทธ๋žจ ์ž‘์„ฑ์‹œ ํ• ๋‹น ๋ฐ›์„ ์ˆ˜ ์—†์Œ

์‹คํ–‰์ค‘์— ํž™ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ํ• ๋‹น

-> ํž™์œผ๋กœ๋ถ€ํ„ฐ ํ• ๋‹น ๋ฐ›๋Š”๋‹ค.

 

 

 

C ์–ธ์–ด์˜ ๋™์  ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น

 

 

ํ• ๋‹น : malloc

 

ํ•ด์ œ : free

 

์œ„ ๋‘๊ฐœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

 

C++์˜ ๋™์  ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น/ ๋ฐ˜ํ™˜

 

new ์—ฐ์‚ฐ์ž

 

๊ธฐ๋ณธํƒ€์ž… ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น, ๋ฐฐ์—ด ํ• ๋‹น, ๊ฐ์ฒด ํ• ๋‹น, ๊ฐ์ฒด๋ฐฐ์—ด ํ• ๋‹น

๊ฐ์ฒด์˜ ๋™์  ์ƒ์„ฑ - ํž™ ๋ฉ”๋ชจ๋ฆฌ๋กœ๋ถ€ํ„ฐ ๊ฐ์ฒด๋ฅผ ์œ„ํ•œ ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น ์š”์ฒญ

๊ฐ์ฒดํ• ๋‹น์‹œ ์ƒ์„ฑ์ž ํ˜ธ์ถœ

 

delete ์—ฐ์‚ฐ์ž

 

new๋กœ ํ• ๋‹น๋ฐ›์€ ๋ฉ”๋ชจ๋ฆฌ ๋ฐ˜ํ™˜

๊ฐ์ฒด์˜ ๋™์  ์†Œ๋ฉธ - ์†Œ๋ฉธ์ž ํ˜ธ์ถœ ๋’ค ๊ฐ์ฒด๋ฅผ ํž™์— ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

 

 

 

 

+ ๋ฉ”๋ชจ๋ฆฌ

 

1. ์ฝ”๋“œ์˜์—ญ : ์ฝ”๋“œ

2. ๋ฐ์ดํ„ฐ ์˜์—ญ: ์ „์—ญ๋ณ€์ˆ˜, static ๋ณ€์ˆ˜( ํ”„๋กœ๊ทธ๋žจ ์‹œ์ž‘์‹œ ํ• ๋‹น, ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ์‹œ ์†Œ๋ฉธ)

3. ํž™(heap)์˜์—ญ : ๋™์ ํ• ๋‹น์˜์—ญ(new๋กœ ํ• ๋‹น, delete ๋กœ ํ•ด์ œ)

4. ์Šคํƒ(stack) ์˜์—ญ : ๋งค๊ฐœ๋ณ€์ˆ˜, ์ง€์—ญ๋ณ€์ˆ˜(ํ•ด๋‹น ํ•จ์ˆ˜ ์‹คํ–‰ ์‹œ ํ• ๋‹น, ํ•จ์ˆ˜ ์ข…๋ฃŒ ์‹œ ํ•ด์ œ)

 

 

 

 

๋ณ€์ˆ˜์™€ ํด๋ž˜์Šค ๋ฐฐ์—ด ๋“ฑ์€ stack ์˜์—ญ(์ •์ ํ• ๋‹น, ์ปดํŒŒ์ผ์‹œ์— ํ• ๋‹น)

๋™์ ํ• ๋‹น์€ ๋Ÿฐ(์‹คํ–‰)์‹œ์— ํ• ๋‹นํ•˜๊ณ  ์ง์ ‘ ์ ‘๊ทผ ๋ถˆ๊ฐ€.

 

๊ทธ๋Ÿฌ๋ฏ€๋กœ ์Šคํƒ์˜์—ญ์— ํฌ์ธํ„ฐ ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ๋†“๊ณ  ์ ‘๊ทผํ•œ๋‹ค.

 

 

 

 

new ์™€ delete ์—ฐ์‚ฐ์ž

 

C++ ์˜ ๊ธฐ๋ณธ ์—ฐ์‚ฐ์ž์ด๋‹ค.

 

new/delete ์—ฐ์‚ฐ์ž ์‚ฌ์šฉํ˜•์‹

 

 

new

๋ฐ์ดํ„ฐํƒ€์ž… *ํฌ์ธํ„ฐ๋ณ€์ˆ˜ = new ๋ฐ์ดํ„ฐ ํƒ€์ž…;

 

delete

delete ํฌ์ธํ„ฐ๋ณ€์ˆ˜

 

 

new/delete์˜ ์‚ฌ์šฉ

 

int *plnt = new int;

 

delete pInt;

 

 

 

 

 

C++ ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น ์˜ˆ์ œ

 

#include <iostream>
#include <string>
using namespace std;



int main() {
	int* p;

	p = new int[5];
	int n;
	if (!p) {
		cout << "๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น ๋ถˆ๊ฐ€";
		return 0;
	}

	for (int i = 0; i < 5; i++) {
		p[i] = i+1;
		n = p[i];
		cout << "*p = " << p[i] << '\n';
		cout << "n = " << n << '\n';
	}
	

	delete []p;
}

 

 

 

 

 

 

 

 

์•„๋ž˜์™€ ๊ฐ™์ด ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 

int main() {
	cout << "์ž…๋ ฅํ•  ์ •์ˆ˜ ๊ฐœ์ˆ˜? : ";
	int n;
	cin >> n; // ์ •์ˆ˜ ๊ฐœ์ˆ˜ ์ž…๋ ฅ
	if (n <= 0) return 0;
	int* p = new int[n];
	if (!p) {
		cout << "๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น ๋ถˆ๊ฐ€";
		return 0;
	}

	for (int i = 0; i < n; i++) {
		cout << i + 1 << "๋ฒˆ์งธ ์ •์ˆ˜ : " ;
		cin >> p[i];
	}

	int sum = 0;
	for (int i = 0; i < n; i++) {
		sum += p[i];
	}
	cout << "ํ‰๊ท  : " << sum / n << endl;
	delete []p;
		
	
}

 

 

 

 

 

๊ฐ์ฒด์˜ ๋™์  ์ƒ์„ฑ ๋ฐ ๋ฐ˜ํ™˜ ํ˜•์‹

 

 

๋™์ ํ• ๋‹น

ํด๋ž˜์Šค์ด๋ฆ„ *ํฌ์ธํ„ฐ๋ณ€์ˆ˜ = new ํด๋ž˜์Šค ์ด๋ฆ„;
ํด๋ž˜์Šค์ด๋ฆ„ * ํฌ์ธํ„ฐ๋ณ€์ˆ˜ = new ํด๋ž˜์Šค ์ด๋ฆ„(์ƒ์„ฑ์ž๋งค๊ฐœ๋ณ€์ˆ˜๋ฆฌ์ŠคํŠธ);

 

 

ํ•ด์ œ

delete ํฌ์ธํ„ฐ๋ณ€์ˆ˜;

 

 

 

 

๊ฐ์ฒด ๋™์ ์ƒ์„ฑ ์˜ˆ์ œ

 

์›์˜ ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ  Circle ๋ฐฐ์—ด์„ ๋™์ ์ƒ์„ฑํ•˜๋ผ.

๋ฐ˜์ง€๋ฆ„ ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›์•„ Circle ๋ฐฐ์—ด์— ์ €์žฅํ•˜๊ณ , ๋ฉด์ ์ด 100์—์„œ 200 ์‚ฌ์ด์ธ ์›์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋ผ.

 

 

 

#include <iostream>
#include <string>
using namespace std;

class Circle {
	int radius;
public:
	Circle();
	~Circle() {};
	void setRadius(int r) { radius = r; };
	double getArea() { return 3.14 * radius * radius; };
};

Circle::Circle() {
	radius = 1;
}

int main() {
	cout << "์ƒ์„ฑํ•˜๊ณ ์ž ํ•˜๋Š” ์›์˜ ๊ฐœ์ˆ˜? : ";
	int n, radius;
	cin >> n; // ์›์˜ ๊ฐœ์ˆ˜ ์ž…๋ ฅ

	Circle* pArray = new Circle[n]; //n๊ฐœ์˜ Circle ๋ฐฐ์—ด ์ƒ์„ฑ
	for (int i = 0; i < n; i++) {
		cout << "์› " << i + 1 << ": ";
		cin >> radius; //๋ฐ˜์ง€๋ฆ„ ์ž…๋ ฅ
		pArray[i].setRadius(radius);
	}

	int count = 0; //์นด์šดํŠธ ๋ณ€์ˆ˜
	Circle* p = pArray;
	for (int i = 0; i < n; i++) {
		cout << p->getArea()<<' '; //์›์˜ ๋ฉด์  ์ถœ๋ ฅ
		if (p->getArea() >= 100 && p->getArea() <= 200) {
			count++;
		}
		p++;
	}
	cout << endl << "๋ฉด์ ์ด 100์—์„œ 200 ์‚ฌ์ด์ธ ์›์˜ ๊ฐœ์ˆ˜๋Š” " << count << endl;
	delete []pArray;
}

 

 

 

 

 

 

 

find()

 

์ฐพ์„ ๋ฌธ์ž์—ด๊ณผ ์–ด๋””์„œ๋ถ€ํ„ฐ ์ฐพ์„ ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•œ ์ธ๋ฑ์Šค๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š” ํ•จ์ˆ˜์ด๋‹ค.

 

 

 

#include <iostream>
#include <string>
using namespace std;

int main() {
	char c = 'A';
	string str = "Apple pear";
	int x = str.find("p", 0);
    int y = str.find("p", 3);
    
    cout << x << endl;
    cout << y << endl;
    
    return 0;
 }

 

 

 

 

 

 

 

find ์˜ ํ™œ์šฉ

 

 

#include <iostream>
#include <string>
using namespace std;




// ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ ํŠน์ • ๋ฌธ์ž๊ฐ€ ๋ช‡ ๊ฐœ ์žˆ๋Š”์ง€?
int main() {
	
	char a;
	string str4;
	cout << "์˜์–ด๋กœ ๋ฌธ์žฅ์„ ์ž…๋ ฅํ•˜์„ธ์—ฌ >> ";
	getline(cin, str4);
	cout << "์ฐพ์„ ๋ฌธ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์—ฌ >> ";
	cin >> a;
	int startindex = 0;
	int x = 0;
	int count = 0;
	while (true) {
		x = str4.find(a, startindex);
		if (x != -1) {
			startindex=x+1;
			++count;
		}
		else {
			break;
		}
	}
	cout << endl << "'" << a << "' ๋ฌธ์ž๋Š” " << count << "๊ฐœ ์žˆ์Šต๋‹ˆ๋‹ค.";
}

 

 

 

 

 

๋ฐ˜์‘ํ˜•
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.