'Windows CE/개발 도구 ( EVC )'에 해당되는 글 2

  1. 2009.07.29 Windows CE(5.0 기준) 에서의 malloc() 사용 시 주의사항 #1
  2. 2008.09.16 입사 후 채워갈 나의 카테고리 입니다.

Windows CE(5.0 기준) 에서의 malloc() 사용 시 주의사항 #1

malloc() 원형은 다음과 같다.

  void *malloc( size_t size ); 
    
  
Parameterssize 
                         Bytes to allocate


return 값에 대한 일반적 사용자들의 기대치는 NULL 이라고 예상된다.
하지만 실제 반환값은 다음과 같이 정의되어 있다.

Return Values
malloc returns a void pointer to the allocated space, or NULL if there is insufficient memory available. To return a pointer to a type other than void, use a type cast on the return value. The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object. If size is 0, malloc allocates a zero-length item in the heap and returns a valid pointer to that item. Always check the return from malloc, even if the amount of memory requested is small.


반환값 중 size 에 대해서 0의 숫자를 들어가면, 예외처리로 NULL 을 반환하는 것이 아니라, vaild pointer(직역: 유효한 포인터) address를 반환한다. 즉 플랫폼의 메모리 정책에 따라서 다른 thread의 메모리를 침범할 수 있는 가능성을 존재한다.


대책으로 사용자는 malloc() 사용시에 size에 대한 검사를 진행해야 한다.

시간 나는대로 실제로 어떤 동작을 하고 어떤 문제가 발생되는지에 대한 실험을 진행해 보도록 하겠다.

 

입사 후 채워갈 나의 카테고리 입니다.

다른 곳보다 많은 내용으로 채워지길 기대하고 있구요.

현실적으로 기술적으로 공유할만한 EMC++ 기법들을 같이
생각해보는 좋은 곳이 되었으면 합니다.

-주인백-