프로그래밍/CPP

구조체 안에 string을 선언 시 주의점

스스배 2023. 3. 24. 10:23

구조체 안에 string을 선언하고 외부에서

	typedef struct test
	{
		int index;
		string Path;

	} TEST;

TEST test;

 

const char* path = "sss"

test.Path = path;

를 하면 string이 해제가 안되어서 메모리 릭이 발생할 경우가 있음

 

그럴땐 

	typedef struct test
	{
		int index;
		char Path[255];

	} TEST;

위와 같이 구조체를 정의하고

 

strcpy(test.Path, path);

로 문자열 복사를 해주면 메모리 릭이 해결됨