C++11中使用sizeof获取类成员大小

C/C++ 专栏收录该内容
¥29.90 ¥99.00
118篇文章

C++11中使用sizeof获取类成员大小

在C++11标准中,我们可以使用sizeof运算符来获取类对象的大小。当我们用它来获取类成员的大小时,需要特别注意一些细节问题。接下来,本文将会给大家详细阐述C++11中如何使用sizeof运算符来获取类成员大小。

首先,需要明确的是,对于非静态成员变量,sizeof运算符只会计算其类型的大小,而不会计算实际占用内存的大小。具体来说,sizeof运算符计算非静态成员变量的大小时,只会考虑成员变量的类型和对齐方式,而不会计算该成员变量实际所占用的内存大小。

接下来,我们来看一个例子:

class MyClass {
public:
    int m_i;
    double m_d;
};

int main() {
    cout << "sizeof(MyClass) = " << sizeof(MyClass) << endl;
    cout << "sizeof(m_i) = " << sizeof(MyClass::m_i) << endl;
    cout << "sizeof(m_d) = " << sizeof(MyClass::m_d) << endl;
    return 0;
}

输出结果为:

sizeof(MyClass) = 16
sizeof(m_i) = 4
sizeof(m_d) = 8

可以看到,sizeof(MyClass)的结果为16字节,这是因为int类型和double类型都需要按照8字

C++11新增关键字~alignas(struct对齐、sizeof、alignof) 本文记录alignas关键字,同时介绍alignof和sizeof. 阅读详情

相关推荐

C++ sizeof 运算符:获取对象大小的工具

通过使用 sizeof 运算符,我们可以在编程过程中准确地获取对象、数据类型或表达式的大小。在C++编程中,sizeof 运算符是一个非常有用的工具,它用于获取对象或数据类型的大小。除了对象和数据类型,我们还可以使用 sizeof 运算符获取表达式的大小。需要注意的是,sizeof 运算符在编译时求值,返回的是一个常量表达式的结果。如果我们只想获取某个数据类型的大小,而不是具体对象的大小,可以直接将数据类型作为 sizeof 运算符的参数。要获取对象的大小,可以直接将对象作为 sizeof 运算符的参数。

PixelCoder的博客 206

C++11 - sizeof用于类成员

sizeof运算符 sizeof运算符返回表达式或者类型所占内存空间的字节数。它返回一个size_t类型的常量表达式。 假设有下面的结构体: struct Point3d{ int x; int y; int z; }; 可以这样进行初始化: Point3d pt; memset(&pt, 0, sizeof(pt)); 也可以这样初始化: Point3d pt1; memset(&amp...

希望我的博客,能帮上你解决学习中工作中所遇到的问题 554

指向对象的C++指针、对象的大小

课程有以下六大特色: 一、业内独家专业手游开发网络培训课程 二、注重手机游戏开发基础,全程项目贯穿 三、Android4.3游戏开发基础、Cocos2D-X,Unity2D,Unity3D一个都不能少 四、课程首次涉及跨平台游戏引擎技术,国内独家(AndEngine引擎、libGDX引擎) 五、跨平台技术(HTML5技术) 六、经典游戏重现(捕鱼达人、斗地主、天天酷跑、围住神经猫)

共享单车需求预测问题:建模篇

机器学习训练营——机器学习爱好者的自由交流空间(qq群号:696721295) 在上一篇里,我们使用可视化的方法分析了“共享单车”的数据集,理解了特征(变量)之间的关系。我们将在本篇建立几个模型预测单车的需求量,并比较它们的预测效果。 随机森林填充法 首先,导入必需的库、训练集和检验集。 import pylab import calendar import numpy as np impor...

wong2016的博客 1万+

sdio wifi 移植

介绍 简单来说,sdio起源自sd标准,顾名思义就是sd的IO接口(interface)的意思,更具体的说明,sd本来是记忆卡的标准,但是现在也可以把SD卡拿来插上一些外围设备接口使用,这个就是就叫做sdio。 而且sdio本身是一种相当单纯的技术,通过sd的IO引脚来连接外部外围,并且通过sd的IO数据接位与外围传输数据,而且sd协会也推出很完整的sdio stack 驱动程序,使得sdio外围得开发与应用变得非常热门 这里我们要移植的是RTL8822cs的sdio WiFi 模块,将其移植到rk3

Enosji的博客 9631

BEV感知技术全景解析:从纯视觉到多传感器融合的实战指南

本文全面解析了自动驾驶中的BEV感知技术,从纯视觉到多传感器融合的实战应用。文章深入探讨了BEV如何提供“上帝视角”,对比了IPM、Lift-Splat-Shoot及Transformer等核心实现范式,并详细阐述了多传感器融合(尤其是中融合)的工程实践与优势,为开发者提供了从理论到原型搭建的完整指南。

win55的博客 1146

SAE J1939协议详解

SAE J1939协议是由美国汽车工程师协会(SAE)制定的主要针对商用车的CAN总线通讯协议,其基础是CAN2.0B协议,面向对象为客车和载重货车;船舶、农业机械等非路面设备。1.SAE J1939协议文档结构如下图:2.CAN的发展概况20世纪80年代,Bosch的工程人员开始研究用于汽车的串行总线系统,1986年, Bosch在SAE(汽车工程师协会)大会上提出了 CAN,1987年,INTEL就推出了第一片CAN控制芯片—82526;随后Philips半导体推出了82C200。

weixin_46184756的博客 8465

电动转向系统 TAS 传感器

靠扭杆变形测力气,用霍尔效应算角度,借 ECU 给电机发指令。它就像 EPS 系统的 "神经末梢",把驾驶员的操作意图转化成电信号,再通过电机变成实实在在的助力,让我们开车既省力又安全。下次转动方向盘时,不妨想想这个藏在转向柱里的 "小传感器"—— 正是它的精准感知,才有了每一次顺畅的转向体验。

weixin_59825476的博客 4346

MavLink协议入门

MAVLink是一个非常轻量化的开源的消息协议,用于无人机之间的通信以及无人机机载设备之间的通信。

ffffffeiyu的博客 2487

标准测试测试数据STDF学习笔记

本文介绍了STDF(Standard Test Data Format)文件的结构与解析方法。首先详细说明了STDF记录的组成,包括记录头类型(REC_TYP和REC_SUB)、数据类型定义、时间日期格式以及无效数据标识。重点分析了25种主要记录类型的功能、数据内容和出现位置,如FAR、MIR、MRR等关键记录。然后介绍了基于pystdf开源工具的解析方法,包括自带脚本的使用和自编程解析流程,通过事件驱动机制处理数据流。特别指出测试数据是按照PIR→PTR/MPR/FTR→PRR的循环模式组织。文章为ATE

歌颂苦难的行为是无耻的; 1524

游戏开发:逻辑与渲染分离技术解析

游戏开发中逻辑与渲染分离的架构设计 本文系统阐述了游戏开发中逻辑与渲染分离的设计原则与实践方法。文章首先定义了核心概念:逻辑层负责游戏规则与状态,渲染层处理视觉表现,二者通过数据模型层进行通信。通过历史回顾,揭示了从早期混合架构到现代分层设计的演变过程。 在技术实现层面,文章详细分析了分离架构的优势:性能优化、流畅度控制、测试便利性、扩展灵活性以及团队协作效率。提供了Unity和Unreal两大引擎的具体实现方案,包括代码示例、分层模型和最佳实践。特别探讨了网络同步、客户端预测、插值处理等关键技术难点。 文

本博客聚焦游戏开发技巧、创业实战心得及大学热门课程干货。这里既有技术深度,也有思维广度,无论你是开发者、创业者还是高校学子,都能在这里找到适合自己的成长之路! 1066

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

深度学习工具包 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")

上一篇: 使用cuBLAS库进行LU分解:10000个4x4矩阵的实例
下一篇: Boost库中的fusion模块提供了许多有用的工具来处理元组和容器类型,其中包括类似于STL容器的vector,而as_vector则是将任何序列式容器转换为
普通网友
博客等级 码龄3年 454粉丝 863原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址