'Development/C++'에 해당되는 글 2건

  1. 2009/11/05 특정 메모리 영역의 포인터를 안전하게 사용하자.
  2. 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



저작자 표시 비영리 동일 조건 변경 허락
Trackback 0 And Comment 0

템플릿 클래스와 멤버함수 깔끔하게 작성하기

|
자주 쓰지는 않지만 기존 작성된 템플릿 클래스를 Wrapping 하다보면 (특히 리스트!) 템플릿 클래스를 사용해야 하는 경우가 많습니다.

하지만 이것 저것 작성하다 보면 템플릿 클래스의 헤더가 복잡해지는 경우가 많은데요.

이는 템플릿 클래스(Template Class)의 멤버함수는 헤더에 정의해야만 하기 떄문입니다
. MFC나 STL에서 디버깅을 하다가 만나는 템플릿 클래스의 헤더들을 보면 구현부가 노출되어 있죠.
(Cpp 파일에 따로 멤버 함수를 정의하면 VC는 링크 오류를 발생시킵니다. <아마도 LNK2014 일 듯 합니다.> export 키워드로 명시적으로 표현할 수 있지만 VC 컴파일러는 이를 인식하지 못합니다.)

한두줄로 구현되는 함수들이야 헤더에 넣어도 문제없겠지만 복잡한 구현부를 가진 함수나, 다수의 함수를 구현하는 경우에는 헤더가 지저분해지기 쉽상입니다.

그래서 많은 분들이 쓰시는 방법은 인라인 함수(inline function) 인데요.
다음과 같이 구현하면 선언부와 구현부를 분리할 수 있어 가독성이 상당히 좋고 헤더도 깔끔해져서 이런 방법으로 구현하곤 합니다.

(1) 기존방법 :

MyTemplateClass.h

template<class T, int size>
class MyTemplateClass
{
public :
MyTemplateClass() { miTotalCount = 0; };
~MyTemplateClass() {};

void Set (T oItem, int index) { moArray[index] = oItem;};
T Get(int index) { return moArray[index]; }
private :
int  miTotalCount;
T   moArray[size];
}

 
(2) 인라인 함수 사용방법 :

MyTemplateClass.h

template<class T, int size>
class MyTemplateClass
{
public :
MyTemplateClass() {};
~MyTemplateClass() {};

void Set (T oItem, int index) {};
T Get(int index) {}
private :
int  miTotalCount;
T   moArray[size];
}

#include " MyTemplateClass.inl"


---------------------------------------------------------------------

MyTemplateClass.inl


template<class T, int size>
inline MyTemplateClass<T, size>::MyTemplateClass()
{
miTotalCount = 0;
}

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)
{
moArray[index] = oItem;
}

template<class T, int size>
inline T MyTemplateClass<T, size>::Get()
{
return moArray[index];
}

 
이렇게 헤더에서 인라인 구현함수 파일(MyTemplateClass.inl)을 포함(include)하게 되면 정의파일과 구현파일로 따로 분리할 수도 있습니다.

함수가 간단해서 후자의 방법이 더 복잡해 보이지만 복잡한 클래스의 경우 이렇게 작성하면 헤더도 간소해지고 가독성도 높아지리라 생각합니다.

* 컴파일을 안돌려봐서 잘 돌아갈지 모르겠네요 ^^;



저작자 표시 비영리 변경 금지
Trackback 0 And Comment 0
prev | 1 | next