0
#include<cstdio>
#include<string>
#include<iostream>

using namespace std; 

int main()
{
        int a[]={0,1,2,3};
        int *r[]={NULL};

        for(int i=0;i<4;i++)
        {
                r[i]=&a[i];
                cout << &a[i] << endl;
                cout << a[i]<<endl;
        }

        for(int i=0;i<4;i++)
        {
                cout << r[i] << endl;
                cout << *r[i] << endl;
        }
        return 0;
}

I have started working on the array of pointers very recently. Can someone please help me out in finding the mistake in the above program..

I attached the screenshots of the results when run on windows and linux platforms.

On Windows,the addresses of the *r[] and a[] are matching and still the values are not matching.

On linux,it says "BUS ERROR" sometimes and "Segmentation fault" sometimes.

It would be better if someone explain what the "BUS ERROR" mean? And why does it come for this program. linux GCC

4
  • 2 words - memory corruption. Commented Sep 9, 2013 at 7:56
  • 12
    This is a bus error. Commented Sep 9, 2013 at 8:45
  • 5
    People who write NULL instead of 0 (when they really want the integer zero, not the null pointer) are horrible people. Ask Stephan T. Lavavej, he'll tell you war stories. Commented Sep 9, 2013 at 8:48
  • I write 0 when I want a pointer 0, I just don't see the point of having a special value for pointers. Commented Sep 10, 2013 at 5:53

4 Answers 4

6

Your array r only has space for a single element in it, but you store 4. That overwrites memory, causing the crash.

Change this:

int *r[]={NULL};

to:

int *r[sizeof a / sizeof *a];

This makes r have the same number of elements as a, in other words 4.

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

3 Comments

Would it be a good idea to create a community-wiki question, something like "I'm writing to indexes outside the array bounds, why is my program crashing/hanging/giving wrong results?", so we can then mark these types of questions as duplicates and point them to it?
@sashoalm, I'm sure we have a few.
I searched, but I couldn't find any.
3

int *r[]={NULL}; should be int *r[]={0, 0, 0, 0};

That will allocates space for four pointers that your following code need.

BUS ERROR: What is a bus error?

Comments

3

Your are not allocating enough space for your r. Try int *r[4]; and you will not get a segmentation fault.

int *r[] = {0} it's equivalent with int *r[1];

Comments

0

r is an array of pointers, but in your code, it has only one element. You are lucky to run it on windows but it's a undefined behavior. It might seem to work today, on your compiler, but it is not legal C or C++, and there is no guarantee that it'll still work the next time you run the program. Or that it hasn't overwritten essential data even now, and you just haven't encountered the problems that is going to cause yet.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.