0

i have an array b[i] which stores the certain indices of another array a[i];both are integer arrays Is it possible to access it as a[b[i]]

#include <iostream>
using namespace std;
int main()
{
    int k=0,v=0,i,b[10],c[10];
    int a[]={1,2,3,4,5,6,7,8,9,10};
    for(i=0;i<10;i++){
        if(a[i]%2==0) 
        {b[k]=i; k++;}
    }
    for(i=0;i<10;i++)
    {
        c[v]=a[b[i]];
        v++;
    }
    return 0;
}

results in segmentation fault

4
  • 2
    Why don't you try it, and see what happens. Commented May 14, 2016 at 2:32
  • yes. though you have to make sure you have the memory or types set up right Commented May 14, 2016 at 2:36
  • added code and results in segmentation fault Commented May 14, 2016 at 2:53
  • Based on our responses you edited your question so that the code does not throw ‘segmentation fault’ any longer. So the code is now in contrary to what is stated at the end of your question. Such discrepancy will confuse future readers of this thread. Reformulate your questions if any doubts remain, please (or revert it to its previous form and post a new one regarding your current problems, if any). Commented May 15, 2016 at 20:16

5 Answers 5

1

In your second loop, the b array is only half initialized (once for every even number from 1 to 10) so when you try to dereference b[5] you get undefined behavior and hence a segfault

Sign up to request clarification or add additional context in comments.

Comments

0

It works fine until you get to i = 5 for the second loop. When i = 5, a[b[5]] is undefined because b[5] is uninitialized. After the first loop, you have b[] = {1, 3, 5, 7, 9, ....} the rest is uninitialized. Try to initialize b[10] and it should work.

b[10] = {};

5 Comments

finally b contain anonymous values
You have missed the point. Initializing b[] with constants (zeroes, as your above code suggests) is not a solution - as the whole goal of angel's code seems to be to fill b[] wisely ! :-)
@ArturOpalinski a good programming practice is whenever you create a variable (could be type), you should initialize it with something (0 is pretty common to use) so that you will not run into problems like this. Of course you can always fill it however you want afterward.
@Hympert: you are absolutely right, but if the question is not about programing best practices, then such general advices belong to comments, not to answers. Such remarks as yours can always be valuable, of course.
@Hympert: oh. sorry, I've just noted that you are probably not eligible for making comments yet. So you perhaps had no other meas to express your thougts.
0

This method should work:

a[b[i]];

I'm not 100% sure it will work though, because the compiler has a role to play here too. For example, it works perfectly in TC++...

A better way to do it is this:

for(i=0;i<10;i++)
{
    int temp=b[i];
    c[v]=a[temp];
    v++;
}

Comments

0

Here is the reason for the error you see:

'Segmentation fault' in this case means that you used a[] array index well beyond a value within memory available to the process (note: it does not need to be beyond the memory range of your computer to get this error; also note that using indexes slightly bigger than array size usually do not cause segmentation faults; but such index value is still a semantic error, of course).

When you declare b[], you do not initialize it (and that is usual). Your first loop only initializes selected elements of b[]: only as much elements from the beginning of b[], as there are even numbers in a[] (you use the condition:a[i]%2==0).

Currently, you have 5 even numbers in a: 2,4,6,8,10. This is sufficient to initialize elements of b[0]...b[4]. The elements of b[] starting with b[5] are left uninitialized, i.e. contain numbers which can be considered random, or trash. You can print b[5]...b[9] to see what do they contain without initialization – it is often 0’s (making debugging even harder, because a[0] does not cause segmentation faults - but a[0] is probably not what you want to get in c[]).

But in different runs of your program elements of b[] starting with b[5] can be anything (and then any of them can make a segmentation fault, e.g. a[-2000000000]).
Chances are extremely low that these accidental values will represent valid indexes of a[].

Relief: change your SECOND loop so it runs only k times, not 10 times. This was probably your initial intention, anyway: for(i=0;i<k;i++)

Comments

0
#include <iostream>
using namespace std;
int main()
{
    int k=0,v=0,i,b[10],c[10];
    int a[]={1,2,3,4,5,6,7,8,9,10};
    for(i=0;i<10;i++){
        if(a[i]%2==0) 
        {b[k]=i; k++;}
    }
    for(i=0;i<5;i++)
    {
        c[v]=a[b[i]];
        v++;
    }
    return 0;
}

works well

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.