C C++ R - C Memory Management

合集下载

memory 坏块管理

memory 坏块管理

memory 坏块管理Memory(内存)是计算机中的重要组件之一,用于存储程序和数据。

然而,由于长时间使用或其他原因,内存中的块可能会变得损坏。

因此,对于系统管理员和程序员来说,对内存中的坏块进行管理是非常重要的。

我们需要了解什么是内存坏块。

内存坏块是指内存中的一个或多个连续的存储单元出现了物理损坏,导致无法正常读取或写入数据。

这些内存坏块可能是由于硬件故障、电压问题、过热或长时间使用等原因引起的。

内存坏块对计算机系统的稳定性和性能都会产生负面影响。

首先,坏块可能会导致系统崩溃或运行变慢。

当程序试图读取或写入损坏的内存块时,可能会发生错误,导致程序崩溃或运行变得缓慢。

其次,坏块还可能导致数据丢失或损坏。

如果损坏的内存块中存储了重要的数据,那么这些数据可能会永久丢失或变得无法正常使用。

为了管理内存中的坏块,我们可以采取一些措施。

首先,可以使用内存测试工具来检测内存中的坏块。

这些工具可以扫描整个内存,检测并标记出坏块的位置。

一旦发现坏块,我们可以将其标记为不可用,以避免在程序中使用这些坏块。

其次,我们可以通过重新分配内存来规避坏块。

当发现坏块时,我们可以将其替换为可用的内存块。

这可以通过操作系统或程序来实现。

最后,定期维护内存也是管理坏块的重要步骤。

定期检查内存的物理状态,清理内存中的垃圾数据,可以减少坏块的发生。

除了上述措施,我们还可以采取一些预防措施来避免内存坏块的发生。

首先,要确保计算机的电压稳定。

电压过高或过低都可能导致内存损坏。

其次,要注意内存的散热问题。

过热可能导致内存损坏,因此要确保计算机的散热系统正常工作。

此外,定期清理内存中的垃圾数据也是预防坏块的重要步骤。

在实际应用中,内存坏块管理是一个复杂而重要的任务。

系统管理员和程序员需要密切关注内存的状态,及时检测和修复坏块。

对于关键系统和数据,还可以考虑使用冗余内存或备份来提高系统的稳定性和容错性。

另外,注意及时更新和升级硬件和软件也是防止坏块的一种有效措施。

DCS 常用词汇

DCS 常用词汇

DCS 常用词汇英文中文缩写Distributed Control System 分布控制系统 DCS Process Control System 过程控制系统Analog Control 模拟控制Close Control Loop 闭环控制回路Open Control Loop 开环控制回路Analog Input Channel 模拟输入通道 AIAnalog Output Channel 模拟输出通道 AOAnalog Control Station 模拟控制站 SACDigital Control 数字控制Two Position Control 两位式控制 On/Off Digital Input Channel 数字输入通道 DIDigital Output Channel 数字输出通道 DOPulse Input Channel 脉冲输入通道 PIDigital Logic Station 数字逻辑站 DLS Programming Logic Controller 可编程控制器 PLC Control Output 控制输出 COCommunication System 通信系统Communication Network 通信网络Control Network 控制网络 CnetCentral Ring 中心环Slave Ring 子环Remote Ring 远程环Control Way 控制通道 C.WModule Bus 模件总线 M.BExpander Bus 扩展总线 Ex.busField Bus 现场总线Communication Protocol 通信协议Store and Forward 存储转发Contention Detect 冲突检测Broadcast Protocol 广播协议Peer to Peer Communication 对等通信Point to Point 点对点Field Bus Protocol 现场总线协议Series Port 串行口SCSI 小型机系统接口Synchronous 同步 SYNAsynchronous 异步Timing 定时Ethernet 以太网Internet 因特网Data Communication Equipment 数据通信设备 DCEData Termination Equipment 数据终端设备 DTEBit per Second 位/秒 BpsNode 节点Cyclic Redundancy Code 循环冗余码 CRCProcess Control Unit 过程控制单元 PCUHuman System Interface 人系统接口 HISComputer Interface Unit 计算机接口单元 CIUModule Mounting Unit 模件安装单元 MMUCabinet 机柜 CABNetwork Interface Module 网络接口模件 NISNetwork Processing Module 网络处理模件 NPMLoop Address 环路地址Node Address 节点地址Controller 控制器Master Module 主模件Multi-Function Processor 多功能处理器 MFPBridge Controller 桥控制器 BRCMachine Fault Timer 机器故障计时器 MFTDirect Memory Access 【'ækses] 直接存储器存取 DMA Redundancy Link 冗余链DCS Link 站链Reset 复位Module Address 模件地址Power Fault Interruption 电源故障中断 PFITermination Unit 端子单元 TUDip shunt 跨接器Jumper 跳线器Setting and Installation 设置与安装Address Selection Switch 地址选择开关Slave Module 子模件Analog Input Module 模拟输入模件 ASIAnalog Output Module 模拟输出模件 ASODigital Input Module 数字输入模件 DSIDigital Output Module 数字输出模件 DSOPulse Input Module 脉冲输入模件 DSMControl I/O Module 控制I/O模件 CISModular Power System 模件电源系统 MPS Thermocouple 热电偶 TCMillivolt 毫伏 mVRTD 热电阻High Level 高电平Low Level 低电平Distributed Sequence of Event 分布顺序事件 DSOE Time Information 时钟信息Time Link 时钟链Time Synchronous 时钟同步Sequence of Event Master 顺序事件主模件 SEM Sequence of Event Digital 顺序事件数字模件 SED Time Salve Termination 时间子模件端子 TST Operator Interface Station 操作员接口站 OIS Operation System 操作系统Tag 标签Operator Windows 操作员窗口Mini Alarm Windows 最小报警窗口Summer Display 总貌画面Group Display 组画面Alarm Acknowledge 报警确认 ACKAlarm Non Acknowledge 报警非确认 NAKStation Display 站画面Annunciator Display Panel 警告显示盘 ADPQuick Key 快捷键Engineering work Station 工程工作站 EWS Configuration 组态Project Tree 项目树Automation Architect 自动化结构Object Exchange 对象交换Function Code 功能码 FCFunction Block 功能块 FBBlock Address 块地址Block Number 块号Exception Report 例外报告Significant Change 有效变化量Minimum Exception Report Time 最小例外报告时间 tmin Maximum Exception Report Time 最大例外报告时间 tmax High Alarm Limit 高报警限Low Alarm Limit 低报警限Alarm Deadband 报警死区ABC Automatic boiler control 锅炉自动控制AC Alternating current 交流(电)ACC Automatic combustion control 燃烧自动控制ACP Auxiliary control panel 辅助控制盘ACS Automatic control system 自动控制系统ACT actuator 执行机构A/D Analog /digital(conversion) 模/数(转换)ADP Annunciation display panel 报警显示板AEH Analog electro- 模拟式电液调节AFC Air flow control `送风控制AGC Automatic generation control 自动发电量控制AI Analog input 模拟量输入A/M Automatic/manual 自动/手动AO Analog output 模拟量输出APC Automatic plant control 电厂自动控制ASS Automatic synchronized system 自动同期系统ARP Auxiliary relay panel 辅助继电器盘ATC Automatic turbine startup or shutdown control system 汽轮机自启停系统BCS Burner control system 燃烧器控制系统BF Boiler follow 锅炉跟踪BFC Boiler fuel control 锅炉燃料控制BPS By-pass control system 旁路控制系统BTG Boiler turbine generator(panel) 锅炉、汽轮机、发电机(控制盘)CCR Central control room 单元(中央)控制室CHS Coal handing system 输煤控制系统CJC Cold junction compensator 冷端补偿器CPU Central processing unit 中央处理器CRT Cathode-ray tube 阴极射线管屏幕显示器CEMS continuous emission monitor system 连续排放监控系统D/A Digital/analog(conversion) 数/模(转换)DAS Data acquisition system 计算机监视系统或数据采集系统DC Direct current 直流(电)DCE Data circuit-terminating equipment 数据电路终端设备DCS Distributed control system 分散控制系统DDC Direct digital control 直接数字控制DDP Distributed data processing 分散数据处理DEH Digital electro-hydraulic control system 数字式电液控制系统DI Digital input 数字量输入DMP Damper 挡板、风门DO Digital output 数字量输出DSB Distributed switch-board 配电盘DTE Data terminal equipment 数据中端设备EEPROM Electrically-erasable programmable read only memory 电可擦写只读存储器E/P Electro/pneumatic(converter) 电/气(转换器)EPROM Electrically programmable read only memory 光可编程只读存储器ES Expert system 专家系统ETS Emergency trip system 紧急停机系统EWS Engineer wok station 工程师工作站FA Full arc 全周进汽FB Field bus 现场总线FCB Fast cut back (机组)快速甩负荷FDC Furnace draft control 炉膛压力控制FSS Furnace safety system 炉膛安全系统FSSS Furnace safeguard supervisory system 锅炉炉膛安全监控系统GV Governor valve 调节阀门HBP High-pressure by-pass valve 高压旁路I&C Instrumentation &control 仪表与控制INT Interlock 连锁I/O Input/output 输入/输出IDP Integrated data processing 集中数据处理KB Keyboard 键盘LBP Low-pressure by-pass valve 低压旁路LCD Liquid-crystal display 液晶显示器LED Light emitting diode 发光二极管LS Limit switch 限位开关LS Level switch 液位开关M/A Manual/automatic 手动/自动MAX Maximum 最大值MCC Motor control center 电动机控制中心MCR Maximum continuous rating 最大连续运行负荷MCS Modulating control system 模拟量控制系统MEH (BFTP)micro-electro-hydraulic control system (锅炉给水泵汽轮机)电液控制系统MFT Master fuel trip 总燃料跳闸MHC Mechanical hydraulic control 机械液压式控制MIN Minimum 最小值MIS Management information system 管理信息系统MTBF Mean time between failures 平均无故障工作时间MTTF Mean time to failure 失效(故障)前平均工作时间MTTR Mean time to repair 平均故障修复时间NC Normally Closed 常闭NO Normally open 常开OCS On-off control system 开关量控制系统OEI Optic electric interface 光电接口OFT Oil fuel trip 燃油跳闸OPC Overspeed protection CONTROL 超速保护控制OS Operator station 操作员站PA Partial arc 部分进汽PC Programmable controller 可编程控制器PCS Pulverizer control system 磨煤机控制系统PI Purse input 脉冲量输入PID Proportional integral derivative 比例-积分-微分PLC Programmable logic controller 可编程序逻辑控制器PO Pulse output 脉冲量输出RAM Random access memory 随机存取存储器RB Run back (辅机故障)快速甩负荷ROM Read only memory 只读存储器RTC Reheat steam temperature control 再热气温控制SBC Soot blower control system 吹灰控制系统SCM Single chip microcomputer 单片机SCS Sequence control system 顺序控制系统SER Sequence events recorder 事件顺序记录仪SOE Sequence of events 事件顺序记录ST Smart transmitter 智能变送器STC Superheated steam temperature control 过热气温控制TAS Turbine automatic system 汽轮机自动控制系统TBP Turbine by-pass system 汽轮机旁路系统TCS Turbine control system 汽轮机控制系统TF Turbine follow 汽轮机跟踪TSI Turbine supervisory instrument 汽轮机监视仪表UCC Unit coordinated control 机组协调控制ULD Unit load demand(command) 机组负荷指令UPS Uninterrupted power system 不间断电源WTS Water treatment control system 水处理控制系统AGC Automatic Generation Control 自动发电控制AMR Automatic Message Recording 自动抄表ASS Automatic Synchronized System 自动准同期装置ATS Automatic Transform System 厂用电源快速切换装置AVR Automatic Voltage Regulator 自动电压调节器BCS Burner Control System 燃烧器控制系统BMS Burner Management System 燃烧器管理系统CCS Coordinated Control System 协调控制系统CIS Consumer Information System 用户信息系统CRMS Control Room Management System 控制室管理系统CRT Cathode Ray Tube 阴极射线管DA Distribution Automation 配电自动化DAS Data Acquisition System 数据采集与处理系统DCS Distributed Control System 分散控制系统DDC Direct Digital Control 直接数字控制(系统)DEH Digital Electronic Hydraulic Control 数字电液(调节系统)DMS Distribution Management System 配电管理系统DPU Distributed Processing Unit 分布式处理单元DSM Demand Side Management 需求侧管理EMS Energy Management System 能量管理系统ETS Emergency Trip System 汽轮机紧急跳闸系统EWS Engineering Working Station 工程师工作站FA Feeder Automation 馈线自动化FCS Fieldbus Control System 现场总线控制系统FSS Fuel Safety System 燃料安全系统FSSS Furnace Safeguard Supervisory System 炉膛安全监控系统FTU Feeder Terminal Unit 馈线远方终端GIS Gas Insulated Switchgear 气体绝缘开关设备GPS Global Position System 全球定位系统HCS Hierarchical Control System 分级控制系统LCD Liquid Crystal Display 液晶显示屏LCP Local Control Panel 就地控制柜MCC Motor Control Center (电动机)马达控制中心MCS Modulating Control System 模拟量控制系统MEH Micro Electro Hydraulic Control System 给水泵汽轮机电波控制系统NCS Net Control System 网络监控系统OIS Operator Interface Station 操作员接口站OMS Outage Management System 停电管理系统PAS Power Application Software 电力应用软件PID Proportion Integration Differentiation 比例积分微分PIO Process Input Output 过程输入输出(通道)PLC Programmable Logical Controller 可编程逻辑控制器PSS Power System Stabilizator 电力系统稳定器RTU Remote Terminal Unit 站内远方终端SA Substation Automation 变电站自动化SCADA Supervisory Control And Data Acquisition 数据采集与监控系统SCC Supervisory Computer Control 监督控制系统SIS Supervisory Information System 监控信息系统TDCS(TDC) Total Direct Digital Control 集散控制系统WMS Work Management System 工作管理系统。

ecc memory scrubbing机制

ecc memory scrubbing机制

ecc memory scrubbing机制在计算机系统中,内存是一个非常重要的组成部分,它存储了运行中的程序和数据。

然而,内存中的位翻转和数据错误可能会导致系统崩溃或数据丢失。

为了解决这个问题,ECC (Error-Correcting Code) 内存模块被引入。

ECC内存模块通过一种称为ECC Memory Scrubbing的机制,提供了对内存错误的检测和纠正能力。

ECC Memory Scrubbing是一种用于检测和修复内存错误的自动化机制。

它通过在内存中定期执行一系列的检查和修复操作来确保数据的完整性。

这个机制通常由硬件控制器和内存模块中的特殊电路组成。

ECC Memory Scrubbing机制的工作原理如下:1. 内存检测:ECC内存模块中的硬件控制器定期对内存中的数据进行检测。

它使用一种称为Hamming Code的错误检测和纠正编码。

Hamming Code是一种能够检测和纠正单位错误的编码技术。

当硬件控制器检测到内存中的位翻转或数据错误时,它会使用Hamming Code来定位和修复错误的位。

2. 错误修复:一旦错误位被定位,ECC内存模块会自动修复这些错误。

它使用纠正编码技术来重新计算受影响位的正确值,并将其写回到内存中。

3. 擦除操作:为了进一步保证内存数据的完整性,ECC内存模块还会定期执行擦除操作。

擦除操作会将内存中的所有位设置为特定的值,以清除潜在的位翻转和数据错误。

ECC Memory Scrubbing机制的优点包括:1. 高可靠性:ECC内存模块能够检测和纠正内存中的位翻转和数据错误,提供了比普通内存模块更高的数据完整性和可靠性。

2. 自动化操作:ECC内存模块中的硬件控制器会自动执行内存检测、错误修复和擦除操作,无需人工干预。

3. 透明性:对于操作系统和应用程序来说,ECC内存模块与普通内存模块没有太大的区别。

它们可以无缝地集成到现有的系统中,而不会对系统性能和稳定性产生明显影响。

CMOS中关于内存的设置

CMOS中关于内存的设置

CMOS中关于内存的设置比较多,但不少选项是比较专业的,因此,如果设置后出现问题,请选择“LOAD BIOS DEFAULTS”,恢复可靠的设置参数。

下面逐条说明相关内存设置。

1、Above 1MB Memory Test:设置开机自检时是否检测1M以上内存。

该选项已经在新的BIOS中被淘汰。

由于内存价格下跌,电脑用户安装内存容量陡然增加,开机时的大容量内存自检时间太长,今后,即使一遍的内存检测可能也会出现允许/禁止开关。

2、Auto Configuration:设置为允许时,BIOS按照最佳状态设置。

BIOS可以自动设置内存定时,因此会禁止一些对内存设置的修改,建议选择允许方式。

3、Memory Test Tick Sound:是否发出内存自检的滴嗒声。

如果您闲它烦,可以关闭它们。

4、Memory Parity Error Check:设置是否要设置内存奇偶校验。

多在30线内存条使用时代,已经被淘汰。

但把非奇偶校验内存强行进行奇偶校验设置会使电脑无法开机。

5、Cache Memory Controller:是否使用高速缓存。

不在流行的A ward BIOS中使用。

6、Shadow RAM Option:设置系统BIOS或显示卡BIOS是否映射到常规内存中。

可以加快速度,但也可能造成死机。

7、Internal Cache Memory:是否使用CPU内部缓存(一级缓存)。

可以提高系统性能。

8、External Cache Memory:是否使用CPU外部缓存(主板上的二级缓存)。

可以提高系统性能。

AMD新的具有两级缓存的CPU的出现,使主板上的二级缓存退居成叁级缓存。

9、Concurrent Refresh:直译是同时发生的刷新。

设置CPU在对其它I/O操作时对内存同时刷新,可以提高系统性能。

10、DRAM Read Wait State:设置CPU从内存读数据时的等待时钟周期。

在内存比CPU 慢时可以设置更多的等待。

ecc_memory_scrubbing机制_概述说明

ecc_memory_scrubbing机制_概述说明

ecc memory scrubbing机制概述说明1. 引言1.1 概述ECC (Error Correcting Code) memory scrubbing机制是一种用于提高计算机系统内存可靠性的技术。

随着计算机应用场景的不断扩大和发展,内存错误对于数据完整性和系统稳定性产生了越来越大的威胁。

ECC memory scrubbing机制通过在内存中添加冗余信息以及周期性地检测和修复内存错误,可以有效降低硬件故障率和数据丢失风险,提高系统性能和可靠性表现。

1.2 文章结构本文将详细介绍ECC memory scrubbing机制的原理、工作流程、优势与应用场景,并对未来发展方向进行展望。

具体结构如下:第2部分:ECC memory scrubbing机制2.1 ECC内存概述2.2 内存误码纠正技术2.3 Scrubbing技术原理第3部分:ECC memory scrubbing机制的工作流程3.1 初始化和配置参数设置3.2 定期读取和校验内存数据3.3 检测和修复内存错误第4部分:ECC memory scrubbing机制的优势与应用场景4.1 降低硬件故障率和数据丢失风险4.2 提高系统性能和可靠性表现4.3 适用于高要求的应用领域第5部分:结论与展望5.1 主要观点总结5.2 未来发展方向1.3 目的本文的目的是对ECC memory scrubbing机制进行全面概述说明,帮助读者了解这一技术的原理、工作流程以及在计算机系统中的应用价值。

通过深入了解ECC memory scrubbing机制,读者可以更好地评估该技术对系统性能和可靠性的影响,并在合适的场景下选择使用。

此外,文章还将探讨未来该技术可能的发展方向,为读者提供对未来趋势有所把握的参考。

2. ECC memory scrubbing机制2.1 ECC内存概述ECC(Error Correcting Code)内存是一种具备纠错功能的内存技术,它能够检测并纠正在数据传输和存储过程中可能出现的错误。

Memory Management

Memory Management

– JIT/compiler generates a map for every program point where a GC may occur – Can constrain optimizations (derived pointers) – Required for type-accurate GC
• Garbage Collection
– Discriminating live objects and garbage
Garbage Collection
GC: How?
• Automatically collect dead objects • Liveness reachability
• Efficiency can be very high • Gives programmers “control”
Garbage Collection
Automatic memory management
• reduces programmer burden • eliminates sources of errors • integral to modern object-oriented languages, i.e., Java, C#, .net • now part of mainstream computing • Challenge:
Garbage Collection
Liveness: the GC and VM/Compiler Contract
• GC Maps - identify what is live
– Root set
• Live registers, walk the stack to enumerate stack variables, globals at any potential GC point

内存控制器(Memory

内存控制器(Memory

内存控制器(Memory Controller)——介绍内存控制器(Memory Controller)——介绍2007年05⽉02⽇星期三 02:43内存控制器(控制器(Memory Controller)是计算机系统内部控制内存并且通过内存控制器使内存与CPU之间交换数据的重要组成内存部分。

内存控制器决定了计算机系统所能使⽤的最⼤内存容量、内存BANK数、内存类型和速度、内存颗粒数据深度和数据宽度等等重要参数,也就是说决定了计算机系统的内存性能,从⽽也对计算机系统的整体性能产⽣较⼤影响。

传统的计算机系统其内存控制器位于主板芯⽚组的北桥芯⽚内部,CPU要和内存进⾏数据交换,需要经过“CPU--北桥--内存--北桥--CPU”五个步骤,在此模式下数据经由多级传输,数据延迟显然⽐较⼤从⽽影响计算机系统的整体性能;⽽AMD的K8系列CPU(包括Socket 754/939/940等接⼝的各种处理器)内部则整合了内存控制器,CPU与内存之间的数据交换过程就简化为“CPU--内存--CPU”三个步骤,省略了两个步骤,与传统的内存控制器⽅案相⽐显然具有更低的数据延迟,这有助于提⾼计算机系统的整体性能。

CPU内部整合内存控制器的优点,就是可以有效控制内存控制器⼯作在与CPU核⼼同样的频率上,⽽且由于内存与CPU之间的数据交换⽆需经过北桥,可以有效降低传输延迟。

打个⽐⽅,这就如同将货物仓库直接搬到了加⼯车间旁边,⼤⼤减少了原材料和制成品在货物仓库和加⼯车间之间往返运输所需要的时间,极⼤地提⾼了⽣产效率。

这样⼀来系统的整体性能也得到了提升。

CPU内部整合内存控制器的最⼤缺点,就是对内存的适应性⽐较差,灵活性⽐较差,只能使⽤特定类型的内存,⽽且对内存的容量和速度也有限制,要⽀持新类型的内存就必须更新CPU内部整合的内存控制器,也就是说必须更换新的CPU;例如AMD的K8系列CPU⽬前就只能⽀持DDR,⽽不能⽀持更⾼速的DDR2。

开机提示内存错误常见错误代码及解决方法!

开机提示内存错误常见错误代码及解决方法!

开机提示内存错误常见错误代码及解决方法!下面是小编收集整理的一些常见的内存可能发生的错误代码和简单解决办法,一起来看看吧!为什么开机提示内存错误1.开机提示“CMOSMemorySizeMismatch”【故障表现】开机启动时,提示“CMOSMemorySizeMismatch 信息。

【故障分析】从提示信息来看,是BIOS发现主板上的内存与CMOS中存放的数值不同,所发产生此错误信息。

【故障处理】启动计算机,进入BIOS设置程序,正确设置后保存退出即可。

2.开机提示“Memorytestfail”【故障表现】开机自检时,屏幕出现“Memorytestfail”,并给出“pressF1continue,orDELtosetup”提示信息。

【故障分析】该错误提示信息,表示检测内存时出现故障。

引起该故障多半是由于使用了不同品牌或不同工作频率的内存条,从而导致兼容性问题,亦有可能是内存插槽出现问题。

【故障处理】根据可能引起故障的不同原因,可通过下面的方法解决。

⑴按照提示信息,按“F1”键继续启动计算机,如果可以正常进入系统,但发现显示的内容容量比实际安装的内存条容量要少,则说明该故障完全是由于多个内存条之间的不兼容引起的,可以通过使用同品牌同频率的内存来解决此问题。

⑵若按“F1”键不能正常进入系统,则说明内存发生严重的故障。

先应对单根内存条进行测试排除单个内存条故障。

然后再组合多根内存条进一步测试,找出不兼容的内存条。

⑶若确定所使用的内存条并不存在兼容问题,则需要进一步检查内存及主板上的内存插槽是否有损坏,因为这些问题也可能引起该故障。

3.开机提示“pressESCtoskipmemorytest”【故障表现】开机后会对内存进行很多次检测,并给出“pressESCtoskipmemorytest”提示信息,按“ESC”键可跳过内存检测,继续启动计算机。

【故障分析】多次检测内存并出现提示信息,是因为CMOS中没有启用快速加电自检功能。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#define MAX_NN 10000 int iarr[MAX_NN];
– below max_nn (note C is case-sensitive and hence max_nn and MAX_NN are different “symbols”) is a variable and hence the size of iarr is also variable
• heap memory: – deallocation:
void free(void *ptr);
• note free takes a void * as an argument thus any pointer could be passed to it:
double *dval_arr = NULL; dval_arr = (double *) malloc(SMALL_LEN * sizeof(double)); free(dval_arr);
• note however though, free only “frees” the space pointed to by dval_arr, it doesn’t “free” dval_arr itself, what does that mean? • the above “thing” is called the problem of “dangling pointers” &
June 29, 2005
&
c 2005 - Gopi Goswami (goswami@)
Page 8
%
ccr2005@
'
C-C++-R
/˜ccr2005
Memory Management IX
June 29, 2005
&
c 2005 - Gopi Goswami (goswami@)
Page 4
%
ccr2005@
'
C-C++-R
/˜ccr2005
$
Memory Management V
#define BIG_LEN 200000 /* Memory leak example: don’t do something like the following */ for (ii = 0; ; ) { printf("count = %d\n", ++ii); dval_arr = (double *) malloc(BIG_LEN * sizeof(double)); }
June 29, 2005
&
Page 3
%
ccr2005@
'
C-C++-R
/˜ccr2005
$
Memory Management IV
• example of stack memory:
#define SMALL_LEN 50 double * mem_stack1 (void) { double dval; return &dval; } double * mem_stack2 (void) { double dval[SMALL_LEN]; return dval; }
• when to use which? • suppose your job is to store and do some “stuff” with the first N-natural numbers, where N is used specified – suppose beforehand you fix an upper limit and ask of the user to only specify N < 10000 then you may declare:
June 29, 2005
c 2005 - Gopi Goswami (goswami@)
Page 6
%
Hale Waihona Puke ccr2005@'
C-C++-R
/˜ccr2005
$
Memory Management VII
$
• example stack memory mis-usage and heap memory usage:
malloc(SMALL_LEN * sizeof(double))
– “cast” the void * pointer to a pointer to double:
(double *) malloc(SMALL_LEN * sizeof(double))
– note, (double *) is the “cast” operator, in general (typename) foo casts variable foo to type typename &
June 29, 2005
&
c 2005 - Gopi Goswami (goswami@)
Page 7
%
ccr2005@
'
C-C++-R
/˜ccr2005
$
Memory Management VIII
• the stack memory may/may not, we don’t know, cause runtime error giving Segmentation Fault • the heap memory if managed properly would work just fine • remember to always deallocate heap memory which you wouldn’t be using anymore • failure to properly deallocate causes what is known as “memory-leak” which makes the program slow and it ultimately gets killed by the operating system
int max_nn = 100000, iarr[max_nn];
– the above is not allowed by gcc -ansi -pedantic i.e. in ANSI C but C99 allows it so plain gcc without those options would compile it just fine – do not use this feature: is the right place to use dynamic memory management with malloc( ), free( ) and the sort c 2005 - Gopi Goswami (goswami@)
June 29, 2005
c 2005 - Gopi Goswami (goswami@)
Page 1
%
ccr2005@
'
C-C++-R
/˜ccr2005
$
Memory Management II
• heap memory: – allocation:
void * malloc(size_t size);
• note malloc returns a void * pointer but, say, we want to allocate space for SMALL_LEN many doubles, here’s how you do it: – get the right amount of space:
June 29, 2005
c 2005 - Gopi Goswami (goswami@)
Page 5
%
ccr2005@
'
C-C++-R
/˜ccr2005
$
Memory Management VI
Page 2
%
ccr2005@
'
C-C++-R
/˜ccr2005
Memory Management III
$
• the following two snippets do not do the same thing: – below MAX_NN is literally substitued by 10000 before compilation i.e. pretend that manually you had typed 10000 in your code wherever you see MAX_NN (why do this? it avoids “magic numbers”, coming later!)
#define MAX_NN 10000 int iarr[MAX_NN];
– note fixing an upper limit is kind of restrictive so you may decide to take a postive number, say, nn, as an input from the user and work with an array of length nn then you may declare (hold on, details coming later):
• example of heap memory:
#define SMALL_LEN 50 double * mem_heap1 (void) { double *dval_arr; dval_arr = (double *) malloc(SMALL_LEN * sizeof(double)); return dval_arr; }
相关文档
最新文档