C ++ sizeof [英] C++ sizeof

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

问题描述

您好,如果我有以下代码,其数组为int *:


#include< iostream>

#include< string>

#include< cstring>

#include< cstddef>


使用命名空间std;


int main(){

int x = 1;

int y = 2;

int z = 3;


int * a [] = {& x,& y,& z};

int * c [] = {& x,& y};


cout<< sizeof(a)<< endl;

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


返回0;

}


输出为12和8。sizeof应该根据其参数类型返回其参数占据的

字节数,即

而不实际评估其参数。由于两者都是a。和c有

类型" int **",为什么" sizeof"返回不同的值?


谢谢。

Hello, if I have the following code that has an array of int*:

#include<iostream>
#include<string>
#include<cstring>
#include<cstddef>

using namespace std;

int main(){
int x = 1;
int y = 2;
int z = 3;

int* a[] = {&x,&y,&z};
int* c[] = {&x,&y};

cout << sizeof(a) << endl;
cout << sizeof(c) << endl;

return 0;
}

The output is 12 and 8. "sizeof" is supposed to return the number of
bytes its argument occupies according to its argument type, i.e.
without actually evaluating its argument. Since both "a" and "c" have
type "int**", why does "sizeof" return different values?

Thanks.

推荐答案

Jess写道:
Jess wrote:

您好,如果我有以下代码,其数组为int *:


#include< iostream>

#include< string>

#include< cstring>

#include< cstddef>


using namespace std;


int main(){

int x = 1;

int y = 2;

int z = 3;


int * a [] = {& x,& y,& z};

int * c [ ] = {& x,& y};


cout<< sizeof(a)<< endl;

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


返回0;

}


输出为12和8。sizeof应该根据其参数类型返回其参数占据的

字节数,即

而不实际评估其参数。由于两者都是a。和c有

类型" int **",为什么" sizeof"返回不同的值?
Hello, if I have the following code that has an array of int*:

#include<iostream>
#include<string>
#include<cstring>
#include<cstddef>

using namespace std;

int main(){
int x = 1;
int y = 2;
int z = 3;

int* a[] = {&x,&y,&z};
int* c[] = {&x,&y};

cout << sizeof(a) << endl;
cout << sizeof(c) << endl;

return 0;
}

The output is 12 and 8. "sizeof" is supposed to return the number of
bytes its argument occupies according to its argument type, i.e.
without actually evaluating its argument. Since both "a" and "c" have
type "int**", why does "sizeof" return different values?



不,''a''的类型为''int * [3]'',''c''的类型为''int * [ 2] ''。他们

是_arrays_,而不是指针。如果你的编译器支持类似的typeid

信息,你可以自己输出类型


cout<< typeid(a).name()<<结束;


V

-

请在通过电子邮件回复时删除资金''A' />
我没有回复最热门的回复,请不要问

No, ''a'' has the type ''int*[3]'', and ''c'' has the type ''int*[2]''. They
are _arrays_, not pointers. If your compiler supports decent typeid
information, you could output the type yourself

cout << typeid(a).name() << endl;

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Jess写道:
Jess wrote:

因为两者都是a和c有

类型" int **",为什么" sizeof"返回不同的值?
Since both "a" and "c" have
type "int**", why does "sizeof" return different values?



错误的假设。 a和c都不是int **类型。

-

Sumit Rajan< su ********* @ gmail.com>

Wrong assumption. Both a and c are not of type int**.
--
Sumit Rajan <su*********@gmail.com>


4月26日上午8:28,Victor Bazarov < v.Abaza ... @ comAcast.netwrote:
On Apr 26, 8:28 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

Jess写道:
Jess wrote:

您好,如果我有以下代码,其数组为int *:
Hello, if I have the following code that has an array of int*:


#include< iostream>

#include< string>

#include< cstring>

#include< cstddef>
#include<iostream>
#include<string>
#include<cstring>
#include<cstddef>


using namespace std;
using namespace std;


int main(){

int x = 1;

int y = 2 ;

int z = 3;
int main(){
int x = 1;
int y = 2;
int z = 3;


int * a [] = {& x,& y,& z};

int * c [] = {& x,& y};
int* a[] = {&x,&y,&z};
int* c[] = {&x,&y};


cout<< sizeof(a)<< endl;

cout<< sizeof(c)<< ENDL;
cout << sizeof(a) << endl;
cout << sizeof(c) << endl;


返回0;

}
return 0;
}


输出为12和8。sizeof应该根据其参数类型返回其参数占据的

字节数,即

而不实际评估其参数。由于两者都是a。和c有

类型" int **",为什么" sizeof"返回不同的值?
The output is 12 and 8. "sizeof" is supposed to return the number of
bytes its argument occupies according to its argument type, i.e.
without actually evaluating its argument. Since both "a" and "c" have
type "int**", why does "sizeof" return different values?



不,''a''的类型为''int * [3]'',''c''的类型为''int * [ 2] ''。他们

是_arrays_,而不是指针。如果您的编译器支持类似的typeid

信息,您可以自己输出类型


No, ''a'' has the type ''int*[3]'', and ''c'' has the type ''int*[2]''. They
are _arrays_, not pointers. If your compiler supports decent typeid
information, you could output the type yourself



我认为当应用于数组时sizeof给出的数量为元素
数组中的
。难道输出不应该是3和2吗?

I thought sizeof when applied to an array gives the number of elements
in the array. Shouldn''t the output be 3 and 2 ?


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

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