查找指针指向的数组的大小 [英] Find size of array pointed to by pointer

查看:110
本文介绍了查找指针指向的数组的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

int* d = (int*) malloc(100 * sizeof(int));
cout<<"size of d which is pointer is: " << sizeof(d)<<endl;

我知道sizeof输出4作为d是一个ptr。但是,我怎么找到整个数组的sizeof

I know that sizeof outputs 4 as d is a ptr. But, how can I find the sizeof the entire array using sizeof

推荐答案

你不能 - code>是一个编译时操作,因此不是动态的。

You cannot - sizeof is a compile time operation and hence not dynamic.

正在使用c ++使用 std :: vector 。否则创建一个结构来存储指针和数组的大小。传递,而不是。

As you are using c++ use std::vector instead. Otherwise create a structure to store both the pointer and the size of the array. Pass that around instead.

这篇关于查找指针指向的数组的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆