By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,125 Members | 1,249 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,125 IT Pros & Developers. It's quick & easy.

array size

P: n/a

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)
Aug 2 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a

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-

Aug 2 '05 #2

P: n/a
> 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

Aug 2 '05 #3

P: n/a
> 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)

Aug 2 '05 #4

P: n/a
ve*********@hotmail.com wrote:
The for loop is best rewritten to
for(int i=0; i< (sizeof(a)/sizeof(*a) -1); ++i)

You need to drop the "-1" (otherwise you will not include the last
element).

-shez-

Aug 2 '05 #5

P: n/a
Baloff wrote:
Hello

why am I getting 6 outputs instead of just 5 from this code? .... for(int i=0; i< (sizeof(a)-1); ++i)

....

sizeof(a)/sizeof(*a) is what you need instead of sizeof(a)-1.

This is commonly written into a macro like ...

#define Nelem( A ) (sizeof(A)/sizeof(*A))

.... which gives you the number of elements in the array, however it may
be interpretted incorrectly so it's much better to use a template that
will never be interpreted incorrectly.

const char * x = "The size of this string is unimportant";

Nelem( x ) will more than likely not be what you think it is and there
will be no error emitted by the compiler.
A better practice to get into it to is this:

template <typename T, unsigned N>
char ( & NelemArrayFunc( T ( & )[ N ] ) )[ N ];

#define Nelem( A ) (sizeof( NelemArrayFunc( A ) ))

In this case Nelem( x ) will be a compile-time error.

Aug 2 '05 #6

P: n/a
I don't even understand this mix with macroses and strange templates.
isn't better to simplify everything:

template<class T, size_t N>inline unsigned int size_of(const T (&)[N]){
return N;
}

and then for the OP's code:
...
for(int i=0; i< size_of(a); ++i)
cout << "item [" << i << "]= " << a[i] << endl;
...

or a pointer version:
template<class T, size_t N>inline const T* last_of(const T (&x)[N]){
return &x[N+1];
}

std::string *i = &a[0];
while(i!=last_of(a)){
cout << *i++ << endl;
}

Aug 2 '05 #7

P: n/a
__PPS__ wrote:
I don't even understand this mix with macroses and strange templates.
isn't better to simplify everything:

template<class T, size_t N>inline unsigned int size_of(const T (&)[N]){
return N;
}

and then for the OP's code:
...
for(int i=0; i< size_of(a); ++i)
cout << "item [" << i << "]= " << a[i] << endl;
...

or a pointer version:
template<class T, size_t N>inline const T* last_of(const T (&x)[N]){
return &x[N+1];
Should probably be

return &x[N];

or

return x + N;
}

std::string *i = &a[0];
while(i!=last_of(a)){
cout << *i++ << endl;
}


V
Aug 2 '05 #8

P: n/a
probably I chose a wrong name :)
should have been something like template<...> ... end(...)

Aug 2 '05 #9

P: n/a
__PPS__ wrote:
probably I chose a wrong name :)
should have been something like template<...> ... end(...)


It doesn't matter what the name is. The last element's index is
N-1. When you want "one-past-the-end" you should use N. N+1 is
simply _wrong_. Just think about it a bit.

V
Aug 2 '05 #10

P: n/a
yes, you are right!
my error

Aug 2 '05 #11

P: n/a
__PPS__ wrote:
I don't even understand this mix with macroses and strange templates.
isn't better to simplify everything:

template<class T, size_t N>inline unsigned int size_of(const T (&)[N]){
return N;
}


int x[5];

int y[ size_of( x ) ];

?
Aug 3 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.