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

相关文章

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: 转换数据类型场景

C# dynamic类型使用详解

《C#dynamic类型使用详解》C#中的dynamic类型允许在运行时确定对象的类型和成员,跳过编译时类型检查,适用于处理未知类型的对象或与动态语言互操作,dynamic支持动态成员解析、添加和删... 目录简介dynamic 的定义dynamic 的使用动态类型赋值访问成员动态方法调用dynamic 的

零基础学习Redis(10) -- zset类型命令使用

zset是有序集合,内部除了存储元素外,还会存储一个score,存储在zset中的元素会按照score的大小升序排列,不同元素的score可以重复,score相同的元素会按照元素的字典序排列。 1. zset常用命令 1.1 zadd  zadd key [NX | XX] [GT | LT]   [CH] [INCR] score member [score member ...]

自定义类型:结构体(续)

目录 一. 结构体的内存对齐 1.1 为什么存在内存对齐? 1.2 修改默认对齐数 二. 结构体传参 三. 结构体实现位段 一. 结构体的内存对齐 在前面的文章里我们已经讲过一部分的内存对齐的知识,并举出了两个例子,我们再举出两个例子继续说明: struct S3{double a;int b;char c;};int mian(){printf("%zd\n",s

【编程底层思考】垃圾收集机制,GC算法,垃圾收集器类型概述

Java的垃圾收集(Garbage Collection,GC)机制是Java语言的一大特色,它负责自动管理内存的回收,释放不再使用的对象所占用的内存。以下是对Java垃圾收集机制的详细介绍: 一、垃圾收集机制概述: 对象存活判断:垃圾收集器定期检查堆内存中的对象,判断哪些对象是“垃圾”,即不再被任何引用链直接或间接引用的对象。内存回收:将判断为垃圾的对象占用的内存进行回收,以便重新使用。

flume系列之:查看flume系统日志、查看统计flume日志类型、查看flume日志

遍历指定目录下多个文件查找指定内容 服务器系统日志会记录flume相关日志 cat /var/log/messages |grep -i oom 查找系统日志中关于flume的指定日志 import osdef search_string_in_files(directory, search_string):count = 0