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(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

两个月冲刺软考——访问位与修改位的题型(淘汰哪一页);内聚的类型;关于码制的知识点;地址映射的相关内容

1.访问位与修改位的题型(淘汰哪一页) 访问位:为1时表示在内存期间被访问过,为0时表示未被访问;修改位:为1时表示该页面自从被装入内存后被修改过,为0时表示未修改过。 置换页面时,最先置换访问位和修改位为00的,其次是01(没被访问但被修改过)的,之后是10(被访问了但没被修改过),最后是11。 2.内聚的类型 功能内聚:完成一个单一功能,各个部分协同工作,缺一不可。 顺序内聚:

Mysql BLOB类型介绍

BLOB类型的字段用于存储二进制数据 在MySQL中,BLOB类型,包括:TinyBlob、Blob、MediumBlob、LongBlob,这几个类型之间的唯一区别是在存储的大小不同。 TinyBlob 最大 255 Blob 最大 65K MediumBlob 最大 16M LongBlob 最大 4G

Oracle type (自定义类型的使用)

oracle - type   type定义: oracle中自定义数据类型 oracle中有基本的数据类型,如number,varchar2,date,numeric,float....但有时候我们需要特殊的格式, 如将name定义为(firstname,lastname)的形式,我们想把这个作为一个表的一列看待,这时候就要我们自己定义一个数据类型 格式 :create or repla

MyBatis 切换不同的类型数据库方案

下属案例例当前结合SpringBoot 配置进行讲解。 背景: 实现一个工程里面在部署阶段支持切换不同类型数据库支持。 方案一 数据源配置 关键代码(是什么数据库,该怎么配就怎么配) spring:datasource:name: test# 使用druid数据源type: com.alibaba.druid.pool.DruidDataSource# @需要修改 数据库连接及驱动u

按揭贷款类型

按揭贷款可以根据不同的分类标准分为多种类型。以下是按揭贷款的一些常见分类: 按贷款利率分类: 固定利率按揭(Fixed Rate Mortgage, FRM):在整个贷款期间,利率保持不变,这意味着每月还款额也是固定的。浮动利率按揭(Adjustable Rate Mortgage, ARM):贷款利率随市场利率的变化而调整,通常有一个基准利率加上一定的浮动点数。 按还款方式分类: 等额本息(

PHP7扩展开发之类型处理

前言 这次,我们将演示如何在PHP扩展中如何对类型进行一些操作。如,判断变量类型。要实现的PHP代码如下: <?phpfunction get_size ($value) {if (is_string($value)) {return "string size is ". strlen($value);} else if (is_array($value)) {return "array si