'Development/C++'에 해당되는 글 2건
- 2009/11/05 특정 메모리 영역의 포인터를 안전하게 사용하자.
- 2009/04/11 템플릿 클래스와 멤버함수 깔끔하게 작성하기
오랜만에 코드 관련 포스팅입니다.
종종
오늘은 특정 메모리 영역의 포인터에 접근하고자 할때 안전하게 사용하는 몇 가지 Win32 API를 소개하려 합니다.
매우 기초지식이지만, 자주 사용하는 놈이 아니다 보니 벌써 잊었네요.
(1) IsBadCodePtr
Determines whether the calling process has read access to the memory at the specified address.
현재의 프로세스가 특정 주소가 가리키는 영역을 읽을 수 있는지 여부를 확인합니다.
(2) IsBadReadPtr
Verifies that the calling process has read access to the specified range of memory.
현재의 프로세스가 특정 주소가 가리키는 영역을 읽을 수 있는지 여부를 확인합니다. (영역 지정 가능)
(3) IsBadStringPtr
Verifies that the calling process has read access to the specified range of memory.
현재의 프로세스가 특정 주소가 가리키는 영역을 문자열로 읽을 수 있는지 여부를 확인하며, 시작 포인터로부터 null을 만날 때까지 유효성을 확인합니다.
(4) IsBadWritePtr
Verifies that the calling process has write access to the specified range of memory.
현재의 프로세스가 특정 주소가 가리키는 영역에 쓸 수 있는지 여부를 확인합니다.
Important This function is obsolete and should not be used. Despite its name, it does not guarantee that the pointer is valid or that the memory pointed to is safe to use. For more information, see Remarks on this page.
물론 빠른 성능을 고려해야 하는 시스템에 도입은 힘들겠지만, 활발한 입/출력의 포인터 제어시에 유용하겠네요.
오늘 기초지식이 없어 고생 좀 했습니다 :D
그리고 추가 팁! 메모리 Leak을 체크하는 방법이 MSDN에 있네요! 다들 아시겠지만요 ㅠ
http://msdn.microsoft.com/en-us/library/x98tx3cf(VS.71).aspx
하지만 이것 저것 작성하다 보면 템플릿 클래스의 헤더가 복잡해지는 경우가 많은데요.
이는 템플릿 클래스(Template Class)의 멤버함수는 헤더에 정의해야만 하기 떄문입니다. MFC나 STL에서 디버깅을 하다가 만나는 템플릿 클래스의 헤더들을 보면 구현부가 노출되어 있죠.
(Cpp 파일에 따로 멤버 함수를 정의하면 VC는 링크 오류를 발생시킵니다. <아마도 LNK2014 일 듯 합니다.> export 키워드로 명시적으로 표현할 수 있지만 VC 컴파일러는 이를 인식하지 못합니다.)
한두줄로 구현되는 함수들이야 헤더에 넣어도 문제없겠지만 복잡한 구현부를 가진 함수나, 다수의 함수를 구현하는 경우에는 헤더가 지저분해지기 쉽상입니다.
그래서 많은 분들이 쓰시는 방법은 인라인 함수(inline function) 인데요.
다음과 같이 구현하면 선언부와 구현부를 분리할 수 있어 가독성이 상당히 좋고 헤더도 깔끔해져서 이런 방법으로 구현하곤 합니다.
MyTemplateClass.h
template<class T, int size>
class MyTemplateClass
{
~MyTemplateClass() {};
void Set (T oItem, int index) { moArray[index] = oItem;};
T Get(int index) { return moArray[index]; }
MyTemplateClass.h
template<class T, int size>
class MyTemplateClass
{
~MyTemplateClass() {};
void Set (T oItem, int index) {};
T Get(int index) {}
---------------------------------------------------------------------
MyTemplateClass.inl
template<class T, int size>
inline MyTemplateClass<T, size>::MyTemplateClass()
{
template<class T, int size>
inline void MyTemplateClass<T, size>::~MyTemplateClass()
{
}
template<class T, int size>
inline void MyTemplateClass<T, size>::Set(T oItem, int index)
template<class T, int size>
inline T MyTemplateClass<T, size>::Get()
{
함수가 간단해서 후자의 방법이 더 복잡해 보이지만 복잡한 클래스의 경우 이렇게 작성하면 헤더도 간소해지고 가독성도 높아지리라 생각합니다.
* 컴파일을 안돌려봐서 잘 돌아갈지 모르겠네요 ^^;


