두 개의 분리 된 RAM 메모리 영역이있는 임베디드 시스템에서 두 개의 고유 한 힙이 있습니다 (하나는 FreeRTOS의 하위 메모리 영역에서의 사용자 정의 구현이고 다른 하나는 GCC에서 상위 메모리 영역에 생성 된 힙입니다). 새로운 힙을 선택할 수 있어야합니다.여러 힙에 대해 새 연산자를 어떻게 오버로드 할 수 있습니까?
답변
메모리를 할당 할 메모리 영역을 알려주는 두 번째 인수를 허용하는 operator new
오버로드를 제공 할 수 있습니다. 당신은 operator new
에 인자를 넣을 수 있습니다. 인자를 new-expression의 타입 앞에 괄호 안에 넣으면됩니다. 이것은 대개 이미 할당 된 저장소에 객체를 넣기 위해 사용됩니다 (표준 라이브러리에서 제공하는 오버로드이므로). 아무 것도 거기에 전달되어 operator new
으로 전달됩니다.
enum MemoryArea {
LOWER,
UPPER
};
void* operator new(std::size_t sz, MemoryArea seg) {
if (seg == LOWER) {
return allocateMemoryInLowerMemoryArea(sz);
} else {
return allocateMemoryInUpperMemoryArea(sz);
}
}
void operator delete(void* p) {
if (pointerIsInLowerMemoryArea(p)) {
freeMemoryFromLowerMemoryArea(p);
} else {
freeMemoryFromUpperMemoryArea(p);
}
}
int main() {
Foo* p = new (LOWER) Foo;
Foo* b = new (UPPER) Foo;
delete p;
delete b;
}
편집 : 허용 된 대답을 참조하십시오. UseUpperMemoryNew는 MyClass의 할당에 영향을 주며 MyClass의 함수에는 할당되지 않습니다. 학습/후배/댓글을 남겨주세요. 상위 메모리 영역에 대한
#include <new>
#undef new
void* operator new (std::size_t size) throw (std::bad_alloc) { ... }
void* operator new (std::size_t size, const std::nothrow_t& nothrow_constant) { ... }
void* operator new[] (std::size_t size) throw (std::bad_alloc) { ... }
void* operator new[] (std::size_t size, const std::nothrow_t& nothrow_constant) throw() { ... }
void operator delete (void* ptr) throw() { ... }
void operator delete (void* ptr, const std::nothrow_t& nothrow_constant) throw() { ... }
void operator delete[] (void* ptr) throw() { ... }
void operator delete[] (void* ptr, const std::nothrow_t& nothrow_constant) throw() { ... }
글로벌 네임 스페이스의 낮은 메모리 영역에 대한
, 그리고
void* new2 (std::size_t size) throw (std::bad_alloc) { ... }
void* new2 (std::size_t size, const std::nothrow_t& nothrow_constant) { ... }
void delete2 (void* ptr) throw() { ... }
void delete2 (void* ptr, const std::nothrow_t& nothrow_constant) throw() { ... }
#define UseUpperMemoryNew \
void* operator new (std::size_t size) throw (std::bad_alloc) { return new2(size); }\
void* operator new (std::size_t size, const std::nothrow_t& nothrow_constant) { return new2(size, nothrow_constant); }\
void* operator new[] (std::size_t size) throw (std::bad_alloc) { return new2(size); }\
void* operator new[] (std::size_t size, const std::nothrow_t& nothrow_constant) throw() { return new2(size, nothrow_constant); }\
void operator delete (void* ptr) throw() { delete2(ptr); }\
void operator delete (void* ptr, const std::nothrow_t& nothrow_constant) throw() { delete2(ptr, nothrow_constant); }\
void operator delete[] (void* ptr) throw() { delete2(ptr); }\
void operator delete[] (void* ptr, const std::nothrow_t& nothrow_constant) throw() { delete2(ptr, nothrow_constant); }
, 낮은 메모리는 상위 메모리는 클래스 수준에서 선택할 수있다 기본값은 다음과 같습니다
class MyClass
{
public:
UseUpperMemoryArea
void someFunction(); // new operator use in this function uses upper memory area
};
전역 이름 공간 외부에서 새 클래스를 다시 정의 할 수 없음을 발견했습니다. 클래스 수준 o 언로드가 유일한 옵션입니다.
참고로, 'throw (...)'예외 사양은 [더 이상 사용되지 않습니다.] (http://en.cppreference.com/w/cpp/language/except_spec)입니다. – jaggedSpire
클래스 별 '연산자 new'오버로드가 작동하는 방식이 아닙니다. 'UseUpperMemoryNew' 오버로드는'MyClass'에서 사용되지 않을 것이고, 그들은 새로운'MyClass' 객체를 할당하는 데 사용될 것입니다. –
자급 답변을 올리려면 이보다 조금 더 할 필요가 있습니다. –
- 1. [] 연산자를 어떻게 오버로드 할 수 있습니까?
- 2. 문자열에 ~ 연산자를 오버로드 할 수 있습니까?
- 3. 연산자를 "두 배 오버로드"할 수 있습니까?
- 4. Python에서 ++ 연산자를 오버로드 할 수 있습니까?
- 5. + = "plus equals"연산자를 어떻게 오버로드 할 수 있습니까?
- 6. 왜 소수의 연산자를 오버로드 할 수 없습니까?
- 7. 오버로드 된 연산자를 어떻게 적용합니까?
- 8. Java는 연산자를 오버로드 할 수 없습니다. 왜?
- 9. "<<"연산자를 오버로드 할 수 없습니다.
- 10. C++ - 할당 연산자를 오버로드 할 수 없습니다.
- 11. 클래스에서 정적 연산자를 오버로드 할 수 있습니까? C#에서
- 12. 10 진수 형식의 + 연산자를 오버로드 할 수 있습니까?
- 13. Delphi 레코드에 대한 대입 연산자를 오버로드 할 수 있습니까?
- 14. 추상 팩토리를 사용하지 않으려면 new 연산자를 오버로드 할 수 있습니까?
- 15. gdb에서 오버로드 된 연산자를 어떻게 분해합니까?
- 16. C++ 템플릿에 = 연산자를 오버로드
- 17. C++에서 참조로 복사 할 연산자를 어떻게 오버로드 할 수 있습니까?
- 18. 연산자를 재정의 할 수 있습니까?
- 19. 정적 클래스에서 연산자를 왜 오버로드 할 수 없습니까?
- 20. Julia에서 이진 연산자를 어떻게 오버로드/재정의합니까?
- 21. 오버로드 + = +, ==와 = 연산자를
- 22. C에서 간접 연산자를 오버로드
- 23. C#을 사용하면 한 번에 모든 연산자를 어떻게 오버로드 할 수 있습니까?
- 24. C++에서 오버로드 << 연산자를 호출 할 수 없습니다.
- 25. 파이썬 할당을 오버로드 할 수 있습니까?
- 26. 이중 새 연산자를 변환 할 수 없습니다.
- 27. %% 연산자를 정의 할 수 있습니까?
- 28. 연산자를 오버로드 할 수 있도록 포인터 래퍼가 필요합니까?
- 29. smlnj에서 기본값이 아닌 오버로드 된 연산자를 사용합니다.
- 30. 은 C++에서 연산자를 오버로드 할 수 없습니다
그건 새로운 배치가 아닙니다. 배치 새로 만들기는 객체를 현재 위치에 인스턴스화하고 메모리를 할당하지 않습니다. –
@CaptainObvlious 새로운 배치와 동일한 구문을 사용하며 동일한 메커니즘을 사용하지만 사용자가 옳다고 생각합니다. 나는 그 말을 다시 할 것이다. –
_ "동일한 구문"- 그러나 더 중요한 의미와 동일하지는 않습니다. –