C/Cxx-静态数据类型

2024-01-17 05:52
文章标签 类型 静态数据 cxx

本文主要是介绍C/Cxx-静态数据类型,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

静态局部变量相比于普通的局部变量有什么区别?
近期自学Programming Abstractions in C++ pp.104时的困惑。经典使用

/** Implementation notes: initRandomSeed* ------------------------------------* The initRandomSeed function declares a static variable that keeps track* of whether the seed has been initialized. The first time initRandomSeed* is called, initialized is false, so the seed is set to the current time.*/
void initRandomSeed()
{static bool initialized = false;if(!initialized){srand(int (time(NULL)));initialized = true;}
}
  1. What is the difference between a static local variable and a regular local variable?

In C and C++, the key differences between a static local variable and a regular (non-static) local variable lie in their lifetime, scope, and initialization. Here’s a detailed comparison:
Lifetime
Static Local Variable:
Has a lifetime that extends across the entire runtime of the program.
It is initialized only once, the first time the control flow passes through its declaration, and retains its value between function calls.
Regular Local Variable:
Has a lifetime that is limited to the function call in which it is defined.
It is created when the function is called and destroyed when the function exits.
Each time the function is called, the variable is reinitialized.
Scope
Both static and regular local variables:
Have the same scope, which is limited to the function (or block) in which they are defined.
Cannot be accessed outside of that function.
Initialization
Static Local Variable:
If not explicitly initialized, it is automatically initialized to zero (for basic data types) or default-initialized (for class instances).
Regular Local Variable:
If not initialized, it contains an indeterminate value (usually garbage) unless it’s an object of a class with a constructor.
Storage
Static Local Variable:
Stored in a static or global data area, not on the stack.
Regular Local Variable:
Stored on the stack (unless optimized otherwise by the compiler).
Example

void function() {static int staticVar = 0; // Static local variableint regularVar = 0;       // Regular local variablestaticVar++;regularVar++;std::cout << "Static: " << staticVar << ", Regular: " << regularVar << std::endl;
}
// Calling function multiple times
function(); // Output: Static: 1, Regular: 1
function(); // Output: Static: 2, Regular: 1
function(); // Output: Static: 3, Regular: 1

In this example, each call to function() increments both staticVar and regularVar. However, staticVar retains its value between calls (since it’s static), while regularVar is reinitialized to 0 each time the function is entered.
Use Cases
Static Local Variables: Useful for maintaining state within a function across multiple calls, such as for caching results, counting the number of times a function is called, or managing resources that need to persist across function calls.
Regular Local Variables: Used for calculations and operations that do not require persistence between different calls to the function.
In summary, the choice between a static local variable and a regular local variable depends on whether you need the variable to retain its value between function calls and exist for the duration of the program.

这篇关于C/Cxx-静态数据类型的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/615047

相关文章

Python如何在Word中生成多种不同类型的图表

《Python如何在Word中生成多种不同类型的图表》Word文档中插入图表不仅能直观呈现数据,还能提升文档的可读性和专业性,本文将介绍如何使用Python在Word文档中创建和自定义各种图表,需要的... 目录在Word中创建柱形图在Word中创建条形图在Word中创建折线图在Word中创建饼图在Word

SpringBoot接收JSON类型的参数方式

《SpringBoot接收JSON类型的参数方式》:本文主要介绍SpringBoot接收JSON类型的参数方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、jsON二、代码准备三、Apifox操作总结一、JSON在学习前端技术时,我们有讲到过JSON,而在

Rust中的BoxT之堆上的数据与递归类型详解

《Rust中的BoxT之堆上的数据与递归类型详解》本文介绍了Rust中的BoxT类型,包括其在堆与栈之间的内存分配,性能优势,以及如何利用BoxT来实现递归类型和处理大小未知类型,通过BoxT,Rus... 目录1. Box<T> 的基础知识1.1 堆与栈的分工1.2 性能优势2.1 递归类型的问题2.2

Python如何计算两个不同类型列表的相似度

《Python如何计算两个不同类型列表的相似度》在编程中,经常需要比较两个列表的相似度,尤其是当这两个列表包含不同类型的元素时,下面小编就来讲讲如何使用Python计算两个不同类型列表的相似度吧... 目录摘要引言数字类型相似度欧几里得距离曼哈顿距离字符串类型相似度Levenshtein距离Jaccard相

Go语言中三种容器类型的数据结构详解

《Go语言中三种容器类型的数据结构详解》在Go语言中,有三种主要的容器类型用于存储和操作集合数据:本文主要介绍三者的使用与区别,感兴趣的小伙伴可以跟随小编一起学习一下... 目录基本概念1. 数组(Array)2. 切片(Slice)3. 映射(Map)对比总结注意事项基本概念在 Go 语言中,有三种主要

Redis的Zset类型及相关命令详细讲解

《Redis的Zset类型及相关命令详细讲解》:本文主要介绍Redis的Zset类型及相关命令的相关资料,有序集合Zset是一种Redis数据结构,它类似于集合Set,但每个元素都有一个关联的分数... 目录Zset简介ZADDZCARDZCOUNTZRANGEZREVRANGEZRANGEBYSCOREZ

IDEA如何将String类型转json格式

《IDEA如何将String类型转json格式》在Java中,字符串字面量中的转义字符会被自动转换,但通过网络获取的字符串可能不会自动转换,为了解决IDEA无法识别JSON字符串的问题,可以在本地对字... 目录问题描述问题原因解决方案总结问题描述最近做项目需要使用Ai生成json,可生成String类型

Mysql 中的多表连接和连接类型详解

《Mysql中的多表连接和连接类型详解》这篇文章详细介绍了MySQL中的多表连接及其各种类型,包括内连接、左连接、右连接、全外连接、自连接和交叉连接,通过这些连接方式,可以将分散在不同表中的相关数据... 目录什么是多表连接?1. 内连接(INNER JOIN)2. 左连接(LEFT JOIN 或 LEFT

Redis的Hash类型及相关命令小结

《Redis的Hash类型及相关命令小结》edisHash是一种数据结构,用于存储字段和值的映射关系,本文就来介绍一下Redis的Hash类型及相关命令小结,具有一定的参考价值,感兴趣的可以了解一下... 目录HSETHGETHEXISTSHDELHKEYSHVALSHGETALLHMGETHLENHSET

Python中异常类型ValueError使用方法与场景

《Python中异常类型ValueError使用方法与场景》:本文主要介绍Python中的ValueError异常类型,它在处理不合适的值时抛出,并提供如何有效使用ValueError的建议,文中... 目录前言什么是 ValueError?什么时候会用到 ValueError?场景 1: 转换数据类型场景