为什么sizeof不同? [英] why sizeof is different ??

查看:82
本文介绍了为什么sizeof不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我有这个代码:


void sizeArr(float a []){

cout<< sizeof(a)<<结束;

}


无效测试(无效){

float val [] = {1,2,0.1};


sizeArr(val); //返回4

cout<< sizeof(val)<< endl //返回12


我不明白???


想法是我想计算数组的大小。


提前致谢,

pujo

Hello I have this code:

void sizeArr(float a[]){
cout << sizeof(a) << endl;
}

void test(void){
float val[] = {1, 2, 0.1};

sizeArr(val); // return 4
cout << sizeof(val) << endl // return 12

I don''t understand ???

The idea is that I would like to calculate size of array.

Thanks in advance,
pujo

推荐答案

aj****@gmail.com 写道:
您好我有这个代码:

void sizeArr(float a []){
cout<< sizeof(a)<< endl;
}

void test(void){
float val [] = {1,2,0.1};

sizeArr(val) ; //返回4
cout<< sizeof(val)<< endl //返回12

我不明白???

想法是我想计算数组的大小。
提前致谢,
pujo
Hello I have this code:

void sizeArr(float a[]){
cout << sizeof(a) << endl;
}

void test(void){
float val[] = {1, 2, 0.1};

sizeArr(val); // return 4
cout << sizeof(val) << endl // return 12

I don''t understand ???

The idea is that I would like to calculate size of array.

Thanks in advance,
pujo




问题是sizeArr()中的数组没有定义的

限制 - 换句话说,它只是一个指针。尝试Boost.Typetraits''

范围:

http://boost.org/doc/html/boost_type...etraits.extent


干杯! --M



The problem is that your array in sizeArr() does not have a defined
limit -- in other words it''s just a pointer. Try Boost.Typetraits''
extent:

http://boost.org/doc/html/boost_type...etraits.extent

Cheers! --M


aj **** @ gmail.com < aj **** @ gmail.com>写道:
aj****@gmail.com <aj****@gmail.com> wrote:
你好我有这个代码:

void sizeArr(float a []){
cout<< sizeof(a)<< endl;
}

void test(void){
float val [] = {1,2,0.1};

sizeArr(val) ; //返回4
cout<< sizeof(val)<< endl //返回12

我不明白???

想法是我想计算数组的大小。
Hello I have this code:

void sizeArr(float a[]){
cout << sizeof(a) << endl;
}

void test(void){
float val[] = {1, 2, 0.1};

sizeArr(val); // return 4
cout << sizeof(val) << endl // return 12

I don''t understand ???

The idea is that I would like to calculate size of array.



您不能将数组直接传递给函数,因为数组会将
衰减为指向数组第一个元素的指针。因此,在你的
sizeArr()函数中,它实际上只是看到一个指针浮动,所以你的

函数知道数组的大小,你必须将它作为额外的

参数传递。


另外,如果你想要元素的数量而不仅仅是大小,你可以做b
$ b sizeof(val)/ sizeof(val [0] );


但请记住,这只适用于将val视为数组的范围

而不是指针。

当然,最好使用std :: vector<>因为它跟踪它的b $ b大小。


-

Marcus Kwok



You cannot pass an array directly to a function, as the array will decay
into a pointer to the array''s first element. Therefore, in your
sizeArr() function, it is really just seeing a pointer to float, so your
cout statement prints the size of a pointer to float. If you want the
function to know the size of the array, you must pass it in as an extra
parameter.

Also, if you want the number of elements instead of just the size, you
can do

sizeof(val) / sizeof(val[0]);

but remember that this only works in the scope that sees val as an array
instead of as a pointer.

Of course, it is preferred to use std::vector<> as it keeps track of its
size.

--
Marcus Kwok


aj****@gmail.com 写道:
aj****@gmail.com wrote:
你好我有这个代码:

void sizeArr(float a []){
cout<< sizeof(a)<< endl;
}

void test(void){
float val [] = {1,2,0.1};

sizeArr(val) ; //返回4
cout<< sizeof(val)<< endl //返回12

我不明白???


这是因为在函数参数中,'float a []''是指向float的指针的

语法。所以在函数中,sizeof(a)将

返回指向float的指针大小。

这是来自C的遗产,它被认为是方便的,但恕我直言,它只是让人困惑。


Btw:sizeof总是返回一个编译时常量。

这个想法是我想计算数组的大小。
Hello I have this code:

void sizeArr(float a[]){
cout << sizeof(a) << endl;
}

void test(void){
float val[] = {1, 2, 0.1};

sizeArr(val); // return 4
cout << sizeof(val) << endl // return 12

I don''t understand ???
This is due to the fact that in function parameters, ''float a[]'' is the
syntax for a pointer to float. So within the function, sizeof(a) will
return the size of a pointer to float.
This is a legacy from C, where it was considered convenient, but IMHO, it''s
only confusing.

Btw: sizeof always returns a compile-time constant.
The idea is that I would like to calculate size of array.




你不能。你的函数只获得一个指向数组第一个元素的指针,

,它没有给你任何方法来查找数组的大小。有几个解决方案



你可以这样做:


void sizeArr(float(& a)[3])


这会传递对函数的数组引用。您可以使用sizeof(a)来获得数组的大小,但是您只能将具有三个元素的数组

传递给该函数。


另一种解决方案是简单地将数组的大小作为第二个

参数传递。另一种方法是保留一个特殊元素值作为结束标记。

C样式字符串以这种方式处理,''\ 0''作为结束标记。

或者你可以 - 而不是原始数组 - 使用std :: vector,它知道它的大小为b $ b。



You can''t. Your function only gets a pointer to the array''s first element,
which does not give you any way to find the size of the array. There are
several solutions.
You could do:

void sizeArr(float (&a)[3])

This passes an array reference to the function. You can use sizeof(a) to get
the array''s size, but you can only pass arrays with exactly three elements
to the function.

An alternative solution is to simply pass the array''s size as second
argument. Another way is to reserve a special element value as end marker.
The C style strings are handled this way, with ''\0'' being the end marker.
Or you could - instead of a raw array - use std::vector, which knows its
size.


这篇关于为什么sizeof不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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