如何获得动态分配的数组的大小? [英] How to get the size of a dynamically allocated array?

查看:90
本文介绍了如何获得动态分配的数组的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果函数签名是这样的:


int func(int arr []);


或者这个:


int func(int * arr);


然后在函数里面我无法知道

数组的大小,是吗?


我试过sizeof(arr)/ sizeof(int),我得到1.


所以这意味着我必须更改签名,以便

这个函数的调用者必须提供大小。


谢谢。

Hi,
If the function signature is like this:

int func( int arr[] );

Or this:

int func ( int* arr );

Then inside the function I have no way to know the size of the
array, is that right?

I tried sizeof(arr)/sizeof(int), I get 1.

So this means that I must change the signature so that the caller of
this function must provide the size.

Thanks.

推荐答案

li ***** @ hotmail.com 写道:



如果功能签名是这样的:


int func(int arr []);


或者这个:


int func(int * arr);


然后在函数内部我无法知道

数组的大小,是吗?
Hi,
If the function signature is like this:

int func( int arr[] );

Or this:

int func ( int* arr );

Then inside the function I have no way to know the size of the
array, is that right?



这是正确的。

That is correct.


我尝试过sizeof(arr)/ sizeof(int),我得到了1.
I tried sizeof(arr)/sizeof(int), I get 1.



sizeof(int *)/ sizeof(int)== 1在您的系统上。

数组函数参数是不可能的。 int func(int arr [])将func声明为接受一个类型为pointer-to-int的单个参数的函数,完全如同
的int func(int * arr) )。然后当你评估sizeof(arr)时,你得到指针的

大小。

sizeof(int *) / sizeof(int) == 1 on your system. It''s not possible to have
an array function parameter. int func(int arr[]) declares func as a
function accepting a single argument of type pointer-to-int, exactly as
does int func(int *arr). When you then evaluate sizeof(arr), you get the
size of the pointer.


所以这意味着我必须改变签名所以这个函数的调用者必须提供这个函数。
So this means that I must change the signature so that the caller of
this function must provide the size.



或者,或者你需要一个特殊的值来表现为数组的结尾

标记,类似于'' \'''终止字符串。

Either that, or you need a special value to behave as an "end of array"
marker, similar to the ''\0'' that terminates strings.


文章< 11 ********************* *@n15g2000prd.googlegroups .com>,

< li ***** @ hotmail.comwrote:
In article <11**********************@n15g2000prd.googlegroups .com>,
<li*****@hotmail.comwrote:

如果功能签名是这样的:
If the function signature is like this:


int func(int arr []);
int func( int arr[] );


或者:
Or this:


int func(int * arr) ;
int func ( int* arr );


然后在函数内部我无法知道
数组的大小,是吗?
Then inside the function I have no way to know the size of the
array, is that right?



非常正确。如果您必须知道例行程序中的大小,

您必须传递大小,或者必须从数据

本身确定大小。

-

有些想法如此错误,只有一个非常聪明的人才能相信他们。 - George Orwell

Quite correct. If you must know the size inside the routine,
you must pass the size in, or there must be some way from the data
itself to identify the size.
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell


li ***** @ hotmail.com 写道:
li*****@hotmail.com writes:



如果功能签名是这样的:


int func(int arr []);


或者这个:


int func(int * arr);


然后在函数内部我无法知道

数组的大小,是吗?
Hi,
If the function signature is like this:

int func( int arr[] );

Or this:

int func ( int* arr );

Then inside the function I have no way to know the size of the
array, is that right?



是的。

Yes.


所以这意味着我必须更改签名以便

此函数必须提供大小。
So this means that I must change the signature so that the caller of
this function must provide the size.



这是最灵活的解决方案。你*可能*可以逃脱:


(a)一个全球知名的大小 - 如果你编程,这可能会起作用

正在建模n维向量和矩阵。你或许可以

make" n"一个编译时常量(#define是这样做的方法)。


(b)你总是在
$ b结束时放置某种哨兵价值$ b数组使函数知道何时避免它们的眼睛。就我个人而言,这些东西是
_啊_但是他们有自己的位置(没有双关语)。


-

Ben。

That is the most flexible solution. You *might* be able to get away with:

(a) A globally known size -- this might just work if, say, you program
is modeling n-dimensional vectors and matrices. You might be able to
make "n" a compile-time constant (#define is the way to do this).

(b) Some kind of sentinel value you always place at the end of the
array so that functions know when to avert their eyes. Personally I
_hate_ such things, but they have their place (no pun intended).

--
Ben.


这篇关于如何获得动态分配的数组的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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