Archived

This topic is now archived and is closed to further replies.

Simple Array Question

This topic is 5176 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Say I wanted to declare an array with 99 elements. int myArray[99]; I used to know how to, but I had a big brain fart. How would I go through and initialize each element of this using a for() loop? [edited by - Krak on October 11, 2003 7:52:32 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by skullfire
ok..lets see
int myArray[99];
for(int k=0;k<99;k++)
myArray[k] = 0;


That works but it won't set the last item in the array it should be

for(int k=0;k <= 99;k++)
myArray[k] = 0;


you can also do the same like this (and its a little bit faster)

memset(&MyArray,0,sizeof(MyArray));



[edited by - FtMonkey on October 11, 2003 8:11:53 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by FtMonkey
quote:
Original post by skullfire
ok..lets see
int myArray[99];
for(int j=0;j<99;j++)
myArray[j] = 0;


That works but it won't set the last item in the array it should be

for(int j=0;j <= 99;j++)
myArray[j] = 0;



Actually, skullfire is correct, and his code will set the last element. Your code will access elements outside the array. The array has 99 elements, so the index ranges from 0 to 98, and you access index 99, which is outside the array, with your for loop.

edit: Changed loop index to j to prevent forum to change text to italic.

[edited by - Brother Bob on October 11, 2003 7:51:44 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by skullfire
ok..lets see
int myArray[99];
for(int i=0;i<99;i++)
myArray = 0;


...And that''ll sift through every element and assign them all to zero?

Share this post


Link to post
Share on other sites
quote:
Original post by Brother Bob
quote:
Original post by FtMonkey
quote:
Original post by skullfire
ok..lets see
int myArray[99];
for(int j=0;j<99;j++)
myArray[j] = 0;


That works but it won't set the last item in the array it should be

for(int j=0;j <= 99;j++)
myArray[j] = 0;



Actually, skullfire is correct, and his code will set the last element. Your code will access elements outside the array. The array has 99 elements, so the index ranges from 0 to 98, and you access index 99, which is outside the array, with your for loop.

edit: Changed loop index to j to prevent forum to change text to italic.

[edited by - Brother Bob on October 11, 2003 7:51:44 PM]

Umm never mind then I guess I was the one doing it wrong hehe I forgot the last item in the array was out of range shouldn't that crash the program? I just tried it and it works as any other variable in the array...
..maybe it was just luck my program didn't crash though....



[edited by - FtMonkey on October 11, 2003 8:23:07 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by FtMonkey
Umm never mind then I guess I was the one doing it wrong hehe I forgot the last item in the array was out of range shouldn''t that crash the program?


What happens when you access elements outside the array is undefined. Anything can happen, and that includes a crash.
quote:

I just tried it and it works as any other variable in the array...


The undefined behaviour you happen to get seems to work the way you want it to work. But be VERY careful, since very hard to find bugs may appear in a completely unrelated part of the program. Here''s a small example.

void bar(int *foo1, int *foo2)
{
*foo1 = 1;
foo2[1] = 2;
foo2[2] = 3;
}

int main(int, char **)
{
int foo1;
int foo2[2];

bar(&foo1, foo2);

cout << foo1 << endl;

return 0;
}

What do you think this code will output? 1? I get 3.

Share this post


Link to post
Share on other sites
Brother Bob: the problem is that you index your array of two items using indices 1 and 2. You should use indices 0 and 1.

The compiler will turn:


foo[bar]


internally into:


*(foo+bar)


This is the way the language is defined (!)

If you don't believe me, check this out:


#include <stdio.h>
int main()
{
printf( "%d\n", 1["abcd"] );
return 0;
}


The initial solution was correct, except it was missing the indirection of

myArray[ i] = 0;

(This was probably the BBS turning it into the "italic" tag)

Last: this construct will NOT initialize everything to 0, only the first element:


int myArray[ 99 ] = { 0 };


However, if your variable/array is global or static, it is guaranteed to be initialized to 0 before the program starts! This is different from locals, members and heap allocations, which contain random junk initially.


[edited by - hplus0603 on October 11, 2003 9:14:14 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by hplus0603
Brother Bob: the problem is that you index your array of two items using indices 1 and 2. You should use indices 0 and 1.


What I''m trying to show with the code is that writing outside the array can cause surprising effects. Writing to foo2[1] also was a bit redundant, maybe I should have written to foo2[2] only (apart from foo1) to show the effect.

Share this post


Link to post
Share on other sites
quote:
Original post by hplus0603
Last: this construct will NOT initialize everything to 0, only the first element:


int myArray[ 99 ] = { 0 };

The C++ standard says:
quote:
C++ standard, 8.5.1/7
If there are fewer initializers in the list than there are members in the aggregate, then each member not explicitly initialized shall be default-initialized (8.5). [Example:
    struct S { int a; char* b; int c; };
    S ss = { 1, "asdf" };

initializes ss.a with 1, ss.b with "asdf", and ss.c with the value of an expression of the form int(), that is, 0. ]
I assume that this is the same for C, but I can''t be certain.

Share this post


Link to post
Share on other sites