Home  >  Article  >  Backend Development  >  Detailed explanation of C++ function parameters: the particularity of parameter passing in exception handling

Detailed explanation of C++ function parameters: the particularity of parameter passing in exception handling

WBOY
WBOYOriginal
2024-04-26 14:00:021131browse

In exception handling, the C function parameter passing method will affect the abnormal behavior: value passing: local variables and value passing parameters will be destroyed without affecting the original data. Passing by reference: Exceptions will affect the original data, leading to undefined behavior. Pointer transfer: The original data will not be affected, but the data pointed to by the pointer may be modified.

C++ 函数参数详解:异常处理中参数传递的特殊性

C Detailed explanation of function parameters: The particularity of parameter passing in exception handling

There are three ways to pass function parameters in C: value passing, reference passing and Pointer passing. These three methods exhibit different behaviors in exception handling.

Value passing

Value passing is the default parameter passing method. Function parameters are treated as local variables within the function. When an exception occurs in a function, the memory space allocated to the parameters at the calling function will be destroyed. In this case, the exception does not affect the original data of the calling function.

Practical case:

void func(int x) {
  // ...
  throw std::exception();
  // ...
}

int main() {
  int y = 10;
  try {
    func(y);
  } catch (std::exception& e) {
    std::cout << e.what() << std::endl;
  }
  std::cout << y << std::endl; // 输出:10
}

Passing by reference

Passing by reference passes the reference of the parameter at the calling function to the inside of the function. When an exception occurs in a function, the exception will still affect the original data in the calling function.

Practical case:

void func(int& x) {
  // ...
  throw std::exception();
  // ...
}

int main() {
  int y = 10;
  try {
    func(y);
  } catch (std::exception& e) {
    std::cout << e.what() << std::endl;
  }
  std::cout << y << std::endl; // 输出:未定义行为
}

Pointer passing

Pointer passing passes the pointer of the parameter at the calling function to the inside of the function. When an exception occurs in a function, the exception will not affect the original data in the calling function. But the data pointed to by the pointer may be modified.

Practical case:

void func(int* x) {
  // ...
  *x = 20; // 修改指针所指向的数据
  throw std::exception();
  // ...
}

int main() {
  int y = 10;
  try {
    func(&y);
  } catch (std::exception& e) {
    std::cout << e.what() << std::endl;
  }
  std::cout << y << std::endl; // 输出:20
}

Notes

In exception handling, when an exception occurs, it is mainly function local variables and values ​​passed by The memory space occupied by the parameters will be destroyed. Parameters passed by reference or pointer will not be destroyed, and the corresponding original data still exists. It should be noted that for pointer transfer, the data pointed to by the pointer may be modified.

The above is the detailed content of Detailed explanation of C++ function parameters: the particularity of parameter passing in exception handling. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn