VirtualAlloc函数使用总结

申请内存区别new malloc HeapAlloc VirtualAlloc 申请内存区别new malloc HeapAlloc VirtualAlloc,很多时候内存分配影响着程序性能,本例通过源代码显示这几个函数的本质。 立即下载
   如果我们的程序需要动态内存的话,则迟早会调用Win32函数VirtualAlloc。但是程序也可以不调用VirtualAlloc,而是直接调用Windows堆函数或者CRT堆函数。不过,知道VirtualAlloc如何工作,可以帮助我们更好地理解这些调用函数。

    首先,必须知道保留(Reserved)内存和占用(Committed)内存的含义。当内存放保留时,一段连续虚拟地址空间被留出。例如,假如我们的程序要使用5 -MB内存块(称为区域),但并不是要马上全部使用,则我们可以调用VirtualAlloc函数,使用MEM_RESERVE分配类型参数。Windows会以64 KB为边界计算该区域的起始地址,并防止进程在同一个范围内为其他内存保留。我们可以指定区域的起始地址,但更常见的是让Windows为区域分配地址。此时除了地址分配外,其他什么也没发生。没有RAM被分配,也没有交换文件空间被保留出来。

    当我们对内存的需求更迫切时,我们可以再次调用函数VirtualAlloc来占用被保留的内存,调用时使用MEM_COMMIT分配类型参数。现在,区域的起始和结束地址都被计算到4KB边界,对应的交换文件页和所要求的页表被留出来。内存块可以被指定为只读或者可读写。然而,仍然没有RAM被分配;只有当程序访问这部分内存时RAM内存才会被真正分配。如果在此之前内存没有被保留,那就不会有问题;如果在此之前内存被占用了的话,也不会有问题。所以原则是,在使用内存之前一定要先占用。

我们可以调用VirtualFree函数“收回”(decommit)占用的内存,使指定的页回到保留的状态。VirtualFree也能够释放保留的内存区域,但我们必须指定其基地址,这个基地址是在前面调用VirtualAlloc保留内存时获得的。

 

什么时候才要用到VirtualAlloc

在没有C运行库可用的时候,要用VirtualAlloc来申请内存。另外,VirtualAlloc还可对申请的内存有许多的控制。

1、可以保留地址空间但不是及分配内存,在需要的时候再分配;

2、可以声明分配的内存不使用分页;

3、可以指定内存在尽可能高的地址上分配;

4、指定内存的读、写、执行属性和内存保护;

5、禁止所分配内存被高速缓存。

我的理解是在 malloc,new的内部实际上还是调用了virtualalloc,virtualalloc是可以自己控制页面的保留,提交,释放的实际的。实际上我们申请的内存没有使用的时候,都是被放在内存页面中,在使用的时候才会被切换到真正的物理存储器里。对于c++程序员来说 一般情况使用malloc,new就好了,除非一开始就想给自己保留一大片空间。


杰理JL701N V142的SDK实现双备份自定义OTA教程和代码分享 摘要 本文档详细介绍了基于JL701N SDK的UART OTA升级功能开发方案。该方案采用双备份(Dual Bank)机制,通过串口实现安全可靠的固件远程更新。核心特性包括:1) 双备份升级机制确保升级失败可回滚;2) CRC-16/XMODEM校验保障数据完整性;3) 灵活的传输控制,支持自定义波特率和数据包大小。文档提供了完整的系统架构、API接口说明、通信协议规范以及移植指南,适用于生产烧录、现场升级等多种场景。配套Python升级工具简化了升级流程,使开发人员能够快速实现固件更新功能。 阅读详情

相关推荐

混合整数规划(MIP)在资源优化中的实战应用

本文深入探讨了混合整数规划(MIP)在资源优化中的实战应用,从菜市场采购到工厂调度,再到物流配送和钢铁厂切割问题。通过分支定界法和割平面法等技术,MIP能够有效解决包含整数和连续变量的复杂优化问题,提升企业运营效率。文章还介绍了现代求解器的优化策略和实际项目中的避坑指南,帮助读者更好地应用MIP技术。

weixin_30500105的博客 91

VirtualAlloc|VirtualFree|VirtualCopy |VirtualProtect |VirtualQuery - WINDOWS API 第七弹 为进程申请虚拟内存

我们知道计算机为了更加高效的使用物理内存,设计出了虚拟内存给进程使用,进程需要运行的话只需要将虚拟内存映射到物理内存即可,当然这也属于非常繁琐的计算机科学了,我们一步一步揭开这个api的神秘面纱吧。vitualalloc系列的函数实现了windows进程虚拟内存向物理内存的转换,是比较底层和重要的api。还有一些,这些大家自己去找吧。夜已深,好梦。

Bobowen的博客 3145

三轴加速度传感器计步算法优化:动态阈值与滤波技术的实践应用

本文深入探讨了三轴加速度传感器计步算法的优化实践,重点分析了动态阈值调整与高效滤波技术如何协同提升计步精度与鲁棒性。通过对比均值滤波的局限,引入向量模长计算、多级自适应阈值以及状态机判决逻辑,并结合代码示例,为嵌入式及移动应用开发者提供了应对复杂运动场景的实用解决方案。

cherry的博客 501

VirtualAlloc的使用

在应用程序中使用虚拟内存(VirtualAlloc VirtualFree) 本文转载以下博客,百度百科,cpp文档 https://blog.youkuaiyun.com/baidu_25539425/article/details/78764156 https://blog.youkuaiyun.com/u012372584/article/details/79621538 https://www.cnblo...

编程爱好者的博客 1万+

深入浅出SSD:固态存储核心技术、原理与实战(文末赠书)

作为经典畅销书的升级版,本书在第1版的基础上新增了大量新知识(如闪存、UFS、测试、文件系统等),并改写或删减了许多过时或不适用的内容,总修改量超过60%。本书由SSD技术社区SSDFans的核心团队成员联合业界、高校SSD方面的专家共同完成,相对于第1版,内容更全面、更新,也更有深度。本书从产品、技术和应用3个维度展开,全面、详细讲解了SSD的发展历史、产品形态、工作原理、整体架构,以及各个功能模块的原理和使用,从SSD的前端接口协议到FTL算法,再到后端的闪存和纠错,以及测试,本书都有涉及。

阿玥的小博客 3123

WhatsApp注册流程逆向分析:从协议解析到风控对抗实战

移动应用安全分析的核心在于理解客户端与服务端的交互协议与验证机制。通过逆向工程,可以深入剖析应用的数据加密、传输与身份验证原理,这对于构建安全模型、进行自动化测试和风控策略研究具有重要技术价值。在即时通讯、金融支付等高安全要求场景中,掌握应用注册流程的底层实现是评估其安全性的关键。本文聚焦于使用动态分析工具如Frida进行Hook和流量抓包,结合设备指纹识别与模拟器检测技术,详细解析了大型应用的注册风控体系,为安全研究人员和开发者提供了从环境搭建到实战对抗的完整方法。

weixin_33935636的博客 316

VirtualAllocAPi逆向笔记

Windows API逆向

qq_43147121的博客 1527

手把手教你解决COLMAP编译中的Ceres CUDA支持问题(附详细配置步骤)

本文详细解析了COLMAP编译中Ceres CUDA支持问题的完整解决方案,包括环境准备、Ceres Solver的CUDA编译配置、COLMAP的CUDA集成配置等关键步骤。通过优化CMake参数和解决常见错误,帮助技术人员充分利用GPU加速优势,提升三维重建效率。

vodka的博客 861

Windows系统上的VirtualAlloc, HeapAlloc和malloc,new的区别

Windows系统上的VirtualAlloc, HeapAlloc和malloc,new的区别。

kuangben2000的博客 1406

QT控件 参考Qt的PIMPL设计模式实现使用QWidget控件绘制3D饼状图表和3D柱状图表,使用QChartView绘制圆柱体图表

工作中之前遇到了绘制3D饼状图表的需求,通过Qt 实现3维饼状图 一文,成功迅速解决问题,现在闲下来了,之前看QtTreePropertyBrowser库的源代码,就想着学习学习Qt的PIMPL模式,于是就参照PIMPL模式实现通过QWidget控件的paintEvent函数重新绘制3D饼状图表和3D柱状图表统计数据的显示效果, 而使用QChartView绘制圆柱体图表完全是取巧了,为了紧急完成工作采用的方法,实际上使用3D柱状图表显示统计数据更加合理。

... 727

基于Simulink的双向DC-DC变换器在储能系统中的应用仿真

摘要:本文详细介绍了基于Simulink的双向DC-DC变换器在光伏储能系统中的仿真实现。主要内容包括:1)采用非隔离型双向Buck-Boost拓扑作为储能系统枢纽;2)针对750V直流微网中的20kWh锂电池接口场景进行建模;3)详细阐述双闭环控制器设计(电压外环+电流内环)、工作模式判断逻辑等关键步骤;4)通过仿真验证了系统在功率阶跃和模式切换下的性能。文章提供了完整的建模流程和控制方法,为储能系统接口设计提供了实用参考。

amy_mhd的博客 179

VirtualAlloc函数

VC声明LPVOID VirtualAlloc(  LPVOID lpAddress,        // region to reserve or commit  SIZE_T dwSize,           // size of region  DWORD flAllocationType,  // type of allocation  DWORD flProtect         

小虎的空间 1370

详细解释SigLIP的原理,为什么有些多模态模型喜欢用他?怎么应用于机器人控制呢

详细解释SigLIP的原理,为什么有些多模态模型喜欢用他?怎么应用于机器人控制呢。

具身机器人和通用AI 1230

VirtualAlloc函数用法总结 预订 调拨

VirtualAlloc函数原型为 LPVOID VirtualAlloc( LPVOID lpAddress, // region to reserve or commit SIZE_T dwSize, // size of region DWORD flAllocationType, // type of allocation DWORD flProtect // type of a

我的专栏 2317

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

[Windows C++] [转] VirtualAlloc函数使用总结

原文链接:https://www.cnblogs.com/davidshi/p/3386710.html 如果我们的程序需要动态内存的话,则迟早会调用Win32函数VirtualAlloc。但是程序也可以不调用VirtualAlloc,而是直接调用Windows堆函数或者CRT堆函数。不过,知道VirtualAlloc如何工作,可以帮助我们更好地理解这些调用函数。 首先,必须知道保留(Reserved)内存和占用(Committed)内存的含义。当内存放保留时,一段连续虚拟地址空间被留出。例如,...

ykun089的博客 1874

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

VirtualAlloc函数使用总结 .

如果我们的程序需要动态内存的话,则迟早会调用Win32函数VirtualAlloc。但是程序也可以不调用VirtualAlloc,而是直接调用Windows堆函数或者CRT堆函数。不过,知道VirtualAlloc如何工作,可以帮助我们更好地理解这些调用函数。     首先,必须知道保留(Reserved)内存和占用(Committed)内存的含义。当内存放保留时,一段连续虚拟地址空间被留出

sgkuo的专栏 861

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

VirtualAlloc 函数

上一次学习了全局内存的分配,在Windows里内存管理是分为两部份,全局内存是系统管理的内存,因而所有进程都可以访问的内存,而每一个进程又有自己的内存空间,这就是虚拟内存空间了,而虚拟内存的空间比较大,当物理内存不足时,系统会把虚拟内存的数据保存到硬盘里,这样只要硬盘的空间足够大,每个进程就可以使用3G的内存。虚拟内存分配可以作为程序里分配内存的主要方式,比如大量的数据缓冲区,动态分配内...

weixin_30376453的博客 201

Windows API一日一练 77 VirtualAlloc函数

Windows API一日一练 77 VirtualAlloc函数

qq_43682964的博客 178

Windows API一日一练(77)VirtualAlloc函数

上一次学习了全局内存的分配,在Windows里内存管理是分为两部份,全局内存是系统管理的内存,因而所有进程都可以访问的内存,而每一个进程又有自己的内存空间,这就是虚拟内存空间了,而虚拟内存的空间比较大,当物理内存不足时,系统会把虚拟内存的数据保存到硬盘里,这样只要硬盘的空间足够大,每个进程就可以使用3G的内存。虚拟内存分配可以作为程序里分配内存的主要方式,比如大量的数据缓冲区,动态分配内...

anjichan4261的博客 466
上一篇: WebViewClient 与 WebChromeClient
下一篇: Android AsyncTask异步线程
shareCode
博客等级 码龄15年 125粉丝 40原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值