CS/자료구조

Chapter 4.5: Programming Tips

arsenic-dev 2025. 4. 16. 01:05

경희대학교 박제만 교수님의 자료구조 수업을 기반으로 정리한 글입니다.

Call by X

▶ Swap Example (1) - call by value

  • swap1_a = 3
  • swap1_b = 5

▶ Looking into 'Swap1'

▶ Swap Example (2) - call by reference

  • swap2_a = 5
  • swap2_b = 3

▶ Swap Example (3) - call by pointer(address)

  • swap3_a = 5 
  • swap3_b = 3

▶ Swap Example (4)

  • swap1_a = 3
  • swap1_b = 5

 

C/C++ Basics:
it is almost about "copy"

 

How C/C++ Deals with Variables: Copy

copy

  • We learned it as "insert 3 into a" and "insert a into b".
  • From now on, we think them "copy 3 into a" and "copy a into b".

Looking into 'Swap1'

Memory space for swap1_a and swap_1_b are allocated.

swap1_a and swap1_b are passed to swap1() as arguments.

Function call of 'swap1()' generates an activation record.

argv stores arguments (parameters) passed from the caller.

swap1_a and swap1_b are local variables of main()

In swap1(), the values of 'a' and 'b' are swapped.

swap1_a and swap1_b NOT changed!

 

Swap1

  • When the program invokes a function and passes some arguments, all those arguments are "copied".
  • In the function (swap1), it changes the copies.
  • Thus, the original variables in the caller function (main()) are NOT affected.

Looking into 'Swap3'

Memory space for swap3_a and swap3_b are allocated.

p3_a and p3_b point to swap3_a and swap3_b, respectively.

a and b (copied from p3_a/p3_b) point to swap3_a and swap3_b.

Memory space for 'temp' is allocated and initialized.

Copy the value pointed by 'b' into the value pointed by 'a'.

Copy the value temp into the value pointed by 'b'.

Pop the activation record of 'swap3()' (remove local variables).

The values of swap3_a and swap3_b are swapped.

Looking into 'Swap4'

Same as Swap3() example until invoking Swap4().

Copy the value of 'a' into 'temp'.

Copy the value of 'b' into 'a'.

Copy the  value of 'temp' into 'b'.

Pop the activation record of 'swap3()' (remove local variables).

The values of swap3_a and swap3_b are NOT swapped.

Looking into 'Swap2'

별명을 주는 개념이다.

Summary

In general, arguments generate copies of themselves.

Only 'Call by Reference' passes variables that share the same memory space as the original variables.

Return by X

Return by Value

Allocate the memory space for 'b' and copy 'a' into 'b'.

Increase the value of 'b'

Copy 'b' into 'ret' and pop the activation record of 'increment()'

main() prints '4' (the value of 'ret')

 

by value

  • 'Return by Value' explicitly returns the specific type of result (int 'b').
  • The caller gets the returned value.

Return by Reference (1)

It passes 'main_a' as 'a' (both share the same memory, 0x2000)

Increment the value of 'a'

Changes on 'a' automatically update 'main_a' as well, since they share the same memory space.

Return by Reference (2)

The value of 'data[front]' is copied into 'value', thus 'ret'

When We Use 'Return by Reference'?

1. When the return object is too big.

2. When you want to return multiple objects/variables.

3. When you return dynamically allocated objects. -> 메모리 누수 발생하지 않도록 지속적으로 관리 (포인터 항상 존재하게)

Inheritance

Inheritance allows programmer to create a new class that is a specialization of an existing class.

  • e.g., Student class -> SW Con, CSE, etc. departmental classes

The new class is called a derived calss (child class) of the existing class;

the existing class is the base class (parent class) of the new class.

 

Review: Queue Definition

▶ Implementation Level

 

I want to make anotoer class CountedQueue.

  • front = rear + 1일 때, full도 되고 empty되는 문제 해결을 위해 reserved space 사용
  • 위 방법 대신 length라는 변수를 추가해도 해결 가능
    • length = 0 -> empty
    • length = array의 max 크기 -> full

CountedQueue Definition

▶ Implementation Level

  • enqueue 시 length++ 을 해줘야 하기에 재정의 (메서드 오버라이딩)
  • dequeue 시 length-- 를 해줘야 하기에 재정의 (메서드 오버라이딩)
  • 위엔 작성하지 않았지만 isFull, isEmpty도 length로 간단히 할 수 있음

We can use the inheritance of classes.

 

CountedQueue Constructor

CountedQueue enqueue() & dequeue()

They are called 'method overriding'

Overriding vs. Overloading

Function Overriding:

  • The ability of a derived class to provide a specific implementation of a method that is already defined in its base class to customize or extend the behavior of the base class method.
  • It must have the same 'name', 'parameters (number and types)', and 'return type'.

Function OVerloading:

  • The ability to define multiple functions in the same scope with the same name but different parameter lists.
  • It can have different 'parameters (number and types)', and 'return type'
  • Examples:
    • add(int a, int b); add(float a, float b), add(int a, int b, int c), etc.

Polymorphism

  • The ability of a language to have duplicate method names in an inheritance hierarchy and to apply the method that is paaropriate for the object to which the method is applied.
  • Polymorphism(다형성) includes both function overriding and function overloading.

'CS > 자료구조' 카테고리의 다른 글

Chapter 5: Linked Structures (1)  (0) 2025.04.16
Programming Exercise: Lab #3  (1) 2025.04.16
Programming Exercise: Lab #2  (1) 2025.04.06
Chapter 4: Queue  (0) 2025.03.31
Chapter 3: Stack  (1) 2025.03.28