求BYTE *byte数组个数

hu376641756 2013-01-23 11:38:39
BYTE byte[6];
byte[0]=22;
byte[1]=22;
byte[2]=22;
byte[3]=22;
byte[4]=22;
byte[5]=22;
int bLength = sizeof(byte);

为什么上面代码sizeof(byte)返回的是4,这怎么理解?如果我是想求byte数组个数,该怎么求?
...全文
445 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
mujiok2003 2013-01-24
  • 打赏
  • 举报
回复

#include <string>
#include <iostream>

template <typename T, size_t SZ>
size_t Size(T (&)[SZ])
{
  return SZ;
}

struct Dummy
{
};

int main()
{ 
         Dummy d[7];
	 std::cout << Size(d) << std::endl; //7
	 return 0;
}
ForestDB 2013-01-24
  • 打赏
  • 举报
回复
数组的长度还是自己维护比较好。
赵4老师 2013-01-24
  • 打赏
  • 举报
回复
其实楼主完全可以做到一辈子都不用sizeof,照样当一个好程序员。
apexMing 2013-01-24
  • 打赏
  • 举报
回复
这样试试

对于你的sizeof(byte)是为何为4,是否你的byte也是一个数据类型呢
上一个回复搞笑了
apexMing 2013-01-24
  • 打赏
  • 举报
回复
这样试试
对于你的sizeof(byte)是为何为4,是否你的byte也是一个数据类型呢
apexMing 2013-01-24
  • 打赏
  • 举报
回复
引用
基础不够扎实啊, 朋友. 来自MSDN中的原文: The operand to sizeof can be one of the following: A type name. To use sizeof with a type name, the name must be enclosed in parentheses. An expression. When used with an expression, sizeof can be specified with or without the parentheses. The expression is not evaluated. When the sizeof operator is applied to an object of type char, it yields 1. When the sizeof operator is applied to an array, it yields the total number of bytes in that array, not the size of the pointer represented by the array identifier. To obtain the size of the pointer represented by the array identifier, pass it as a parameter to a function that uses sizeof. For example:
哈哈,确是如此啊。长期用糊涂了,见笑
lin5161678 2013-01-24
  • 打赏
  • 举报
回复
编译器bug 要不就是 你通过一个函数调用输出sizeof(byte)的值????
xiaohuh421 2013-01-24
  • 打赏
  • 举报
回复
引用 2 楼 luqichao 的回复:
C/C++ code?1sizeof(byte) 这里byte是数组名,也就是指针,所以sizeof(byte)是4很正常。 你说想求得数组的元素个数,对不起,这个依靠数组指针是无法求解的。 不然所谓的数组越界问题也就不会存在了。
基础不够扎实啊, 朋友. 来自MSDN中的原文: The operand to sizeof can be one of the following: A type name. To use sizeof with a type name, the name must be enclosed in parentheses. An expression. When used with an expression, sizeof can be specified with or without the parentheses. The expression is not evaluated. When the sizeof operator is applied to an object of type char, it yields 1. When the sizeof operator is applied to an array, it yields the total number of bytes in that array, not the size of the pointer represented by the array identifier. To obtain the size of the pointer represented by the array identifier, pass it as a parameter to a function that uses sizeof. For example:
apexMing 2013-01-24
  • 打赏
  • 举报
回复
sizeof(byte)
这里byte是数组名,也就是指针,所以sizeof(byte)是4很正常。 你说想求得数组的元素个数,对不起,这个依靠数组指针是无法求解的。 不然所谓的数组越界问题也就不会存在了。
Crazy_Number 2013-01-24
  • 打赏
  • 举报
回复
数组传入函数后会被转换为指针。 所以你在函数内sizeof(functionByte);为4,也就是指针的大小。
hu376641756 2013-01-24
  • 打赏
  • 举报
回复
这个是通过了一个函数传递后 void GetLength(BYTE *functionByte) { int bLength = sizeof(functionByte); } main() { BYTE mainbyte[6]; byte[0]="22"; .........类推 } 我是这用求的长度,照理数组变量也就是个指针,传递过去函数接收的也是同类型的指针,所以传递后functionByte的长度也应该是mainByte的长度哇,他们指向的值一样。难道这样不是按引用传递吗?
ppsharp 2013-01-23
  • 打赏
  • 举报
回复
引用 楼主 hu376641756 的回复:
BYTE byte[6];
byte[0]=22;
byte[1]=22;
byte[2]=22;
byte[3]=22;
byte[4]=22;
byte[5]=22;
int bLength = sizeof(byte);

为什么上面代码sizeof(byte)返回的是4,这怎么理解?如果我是想求byte数组个数,该怎么求?

……


我的输出是6。
内容概要:本文系统研究了基于CNN-SVM的卷积神经网络与支持向量机融合的数据分类预测方法,聚焦其在工业故障识别中的应用,提供了完整的Matlab代码实现。通过CNN提取输入数据的深层空间特征,再由SVM进行高精度分类,充分发挥两者优势,有效提升了故障识别的准确性、鲁棒性与泛化能力。该方法特别适用于处理电力系统、机械设备等领域的高维、非线性、强噪声监测数据,在变压器故障诊断、轴承缺陷识别等场景中具有重要应用价值。文档还整合了机器学习、深度学习、图像处理、路径规划、电力系统优化等多个前沿科研方向的技术资源,配套大量Matlab/Simulink仿真案例与Python代码,全面支持科研复现与工程实践。; 适合人群:具备一定编程基础,熟练掌握Matlab或Python语言,从事电气工程、自动化、人工智能、机械故障诊断等相关领域研究的研发人员及高校研究生; 使用场景及目标:① 实现工业设备的状态监测与多类别故障分类;② 深入理解CNN与SVM融合模型的设计原理与工程实现细节;③ 借助所提供的丰富算法案例开展科研复现、模型优化与系统仿真验证; 阅读建议:建议按照文档目录结构系统化学习,结合百度网盘提供的完整代码资源进行动手实践,重点关注CNN特征提取层与SVM分类器之间的数据接口设计与参数调优策略,同时可延伸学习文中涉及的其他智能算法及其在电力系统、信号处理等领域的交叉应用,全面提升科研创新能力。

65,212

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧