Machineboy空

Object-Oriented Data Structures # WEEK03 : Variable storage & Class Destructor 본문

Computer/자료구조

Object-Oriented Data Structures # WEEK03 : Variable storage & Class Destructor

안녕도라 2024. 2. 5. 12:53

3.4 Variable storage

 

In C++, an instance of a variable can be stored directly in memory accessed by pointer, or accessed by reference.

 

  • 3 different ways of storing access to variables
  • 3 different ways of pass variables around function

1. Direct Storage

타입에 따른 메모리 크기

 

By default, variables are stored directly in memory.

  • The type of a variable has no modifiers.
  • The object takes up exactly its size in memory.
Cube c;				// Stores a Cube in memory
int i ;				// Stores an integer in memory
uiuc:HSLAPixel p;	// Stores a pixel in memory

 

2. Storage by Pointer

 

  • The type of variable is modified with an asteristk(*))
  • A pointer takes a "memory address width" of memory (ex: 64 bits on a 64-bit system)
  • The pointner "points" to the allocated space of the object.
Cube *c;	//Pointer to a Cube in memory
int *i;		//Pointer to an integer in memory
uiuc::HSLAPixel *p;		//Pointer to a pixel in memory

 

3. Storage by Reference

  • A reference is an alias(가명) to existing memory and is denoted in the type with an ampersand(&)
  • A reference does not store memory itself, it is only an alias to another variable
  • The alias must be assigned when the variable is initialized.
Cube &c = cube;		//Alias to the variable 'cube'
int &i = count;		//Alias to the variable 'i'
uiuc::HSLAPixel &p	// illegal! Must alias something when vaiable is initialized

Example: Cube Currency(화폐)

Suppose our cubes have a value to them, based on their volume.

 

When we receive money, we want the cube itself- not a copy of the cube.

don't want to do anything else except for transfer cube to someone else.

 

making copy = inflation

*통화량 증가로 화폐가치가 하락하고 모든 상품의 물가가 전반적으로 꾸준히 오르는 경제현상

 

 

Pass by____

Identical to starage, arguments can be passed to functions in three different ways:

  • Pass by value (default)
  • Pass by pointer (modified with *)
  • Pass by reference (modified with &, acts as an alias)

 

Return by____

Similarly, values can be returned all three ways as well:

  • Return by value (default)
  • Return by pointer (modified with *)
  • Return by reference (modified with &, acts as an alias)

Never return a reference to a stack variable created on the stack of your current function!

 

변수를 저장하는 방식에는 3가지가 있다.

value, pointer, reference

이 3가지 방법을 통해 매우 유연할 수 있고 메모리가 복사되는 방식을 정확하게 제어할 수 있다

 


 

3.5 Class Destructor

 

When an instance of a class is cleaned up, the class destructor is the last call in a class's lifecycle.

 

Automatic Default Destuctor

An automatic default destructor is added to your class if no other destructor is defined.

 

The only action of the automatic default destructor is to call the default destructor of all member objects.

 

An destructor should never be called directly. Instead, it is automatically called when the object's memory is being reclaimed by the system:

  • If the object is on the stack, when the function returns
  • If the object is on the heap, when delete is used.

 

The destructors aren't called at compile time, but rather during runtime. The professor is pointing out that the compiler will insert implicit, automatic calls to the destructor in appropriate places where stack objects should be destroyed as they go out of scope, or where heap objects are destroyed with "delete". That's why you don't need to call the destructor explicitly.

 

Custom Destructor

 

To add custom behavior to the end-of-life of the function, a custom destructor can be defined as:

  • A custom destructor is a member function.
  • The function's destructor is the name of the class, preceded by a tilde ~.
  • All destructors have zero arguments and no return type.
Cube::~Cube(); 	// Custom destructor

 

Custom Destructor

A custom destructor is essential when an object allocates an external resource that must be closed or freed when the object is destroyed. Examples:

  • Heap memory
  • Open files
  • Shared memory