数组大小 [英] array size

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

问题描述



你好


为什么我从这段代码得到6个输出而不是5个?


谢谢


#include< iostream>

#include< string>


using namespace std;


int main(){

string a [] =

{

" blue" ,绿色,红色,黑色,白色

};

for(int i = 0; i<(sizeof( a)-1); ++ i)

cout<< item [" << i<< "] =" << a [i]<< endl;

}


item [0] = blue

item [1] = green

项目[2] =红色

项目[3] =黑色

项目[4] =白色

项目[5] =


程序收到信号SIGSEGV,分段错误。

0x40095847在std :: operator<< < char,std :: char_traits< char> ;,std :: allocator< char> > ()来自/usr/lib/libstdc++.so.5

(gdb)

解决方案



Baloff写道:


int main(){
string a [] =
{
" blue"," green" ,red,black,白色
};
for(int i = 0; i<(sizeof(a)-1); ++ i)


Sizeof(a)返回数组的* size *,而不是数组中元素的数量

。例如,如果std :: string的大小是4个字节,则

sizeof(a)以上将是4 * 5 = 20.


所以你从0到19循环,但是一旦你命中i = 5,你就已经超过数组了(这就是为什么它是段错误的。)


你想要的是元素的数量,而不是数组的大小。

你可以通过将数组的大小除以每个

元素的大小来得到这个。 />

将循环条件更改为:


for(int i = 0; i< sizeof a / sizeof * a; ++ i)

希望这会有所帮助,

-shez-


>您好


为什么我从此代码获得6个输出而不是5个?

感谢

#include< iostream>
#include< string>

使用命名空间std;

int main(){
string a [] =
{
blue,green,red,black,white,
;
for(int i = 0; i<(sizeof(a) )-1); ++ i)
cout<< item [" << i<< "] =" << a [i]<< endl;

}

item [0] =蓝色
item [1] =绿色
item [2] = red
item [3] = black
item [4] = white
item [5] =

程序收到信号SIGSEGV,分段错误。
在std :: operator<x中的0x40095847 ;< < char,std :: char_traits< char> ;,std :: allocator< char> > > ()来自/ usr / lib / libstdc ++。so.5




曾经尝试过打印sizeof(a)的值吗?它的20.难怪你得到了一个段错误。了解sizeof的含义并学会使用STL

迭代器。


Srini


>为什么我从这段代码得到6个输出而不是只有5个?


感谢

#include< iostream>
#include< string> ;

使用命名空间std;

int main(){
string a [] =
{
" blue"," ; green,red,black,white
};
for(int i = 0; i<(sizeof(a)-1); ++ i)
cout<< item [" << i<< "] =" << a [i]<< endl;
}

item [0] =蓝色
item [1] =绿色
item [2] = red
item [3] = black
item [4] = white
item [5] =

程序接收信号SIGSEGV,分段错误。
0x40095847在std :: operator<< < char,std :: char_traits< char> ;,std :: allocator< char> > ()来自/ usr / lib / libstdc ++。so.5




自程序崩溃后你只得到6个输出(如果它不会这样做

你得到垃圾的最小5 * sizeof(std :: string)值。

问题是当你做sizeof(a)时你会得到总大小

数组以字节为单位。简单添加

std :: cout<< sizeof(a)<< std :: endl;
循环之前
应该确认这一点。


for循环最好重写为

for(int i = 0; i<(sizeof( a)/ sizeof(* a)-1); ++ i)



Hello

why am I getting 6 outputs instead of just 5 from this code?

thanks

#include <iostream>
#include <string>

using namespace std;

int main(){
string a[]=
{
"blue", "green", "red", "black", "white"
};
for(int i=0; i< (sizeof(a)-1); ++i)
cout << "item [" << i << "]= " << a[i] << endl;
}

item [0]= blue
item [1]= green
item [2]= red
item [3]= black
item [4]= white
item [5]=

Program received signal SIGSEGV, Segmentation fault.
0x40095847 in std::operator<< <char, std::char_traits<char>, std::allocator<char> > () from /usr/lib/libstdc++.so.5
(gdb)

解决方案


Baloff wrote:


int main(){
string a[]=
{
"blue", "green", "red", "black", "white"
};
for(int i=0; i< (sizeof(a)-1); ++i)


Sizeof(a) returns the *size* of the array, not the number of elements
in the array. For example, if the size of std::string is 4 bytes, then
sizeof(a) above would be 4*5 = 20.

So you are looping from 0 to 19, but once you hit i = 5, you are
already past the array (this is why it segfaults).

What you want is the number of elements, not the size of the array.
You can get this by dividing the sizeof the array by the sizeof each
element.

Change your loop condition to:

for(int i=0; i< sizeof a/sizeof *a; ++i)
Hope this helps,
-shez-


> Hello


why am I getting 6 outputs instead of just 5 from this code?

thanks

#include <iostream>
#include <string>

using namespace std;

int main(){
string a[]=
{
"blue", "green", "red", "black", "white"
};
for(int i=0; i< (sizeof(a)-1); ++i)
cout << "item [" << i << "]= " << a[i] << endl;

}

item [0]= blue
item [1]= green
item [2]= red
item [3]= black
item [4]= white
item [5]=

Program received signal SIGSEGV, Segmentation fault.
0x40095847 in std::operator<< <char, std::char_traits<char>, std::allocator<char> > > () from /usr/lib/libstdc++.so.5



Ever tried printing the value of sizeof(a)?? Its 20. No wonder you''re
getting a segfault. Learn what sizeof means and learn to use STL
iterators.

Srini


> why am I getting 6 outputs instead of just 5 from this code?


thanks

#include <iostream>
#include <string>

using namespace std;

int main(){
string a[]=
{
"blue", "green", "red", "black", "white"
};
for(int i=0; i< (sizeof(a)-1); ++i)
cout << "item [" << i << "]= " << a[i] << endl;
}

item [0]= blue
item [1]= green
item [2]= red
item [3]= black
item [4]= white
item [5]=

Program received signal SIGSEGV, Segmentation fault.
0x40095847 in std::operator<< <char, std::char_traits<char>, std::allocator<char> > () from /usr/lib/libstdc++.so.5



You get only 6 outputs since the program crashes (if it wouldn''t do
that you''d get least 5 * sizeof(std::string) values of garbage.
The problem is that when you do sizeof(a) you get back the total size
of the array in bytes. a simple addition of
std::cout << sizeof(a)<< std::endl;
before the loop should confirm this.

The for loop is best rewritten to
for(int i=0; i< (sizeof(a)/sizeof(*a) -1); ++i)


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

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