2

So my professor told my class to run this program, it supposedly shows what's going on in computer memory. When I ran it, my output file had the same numbers as my professor's output file for the first like 4 lines and then totally different numbers from then on. But this certainly doesn't seem like it's pulling random numbers, because a lot of the output is 0s and a lot of it are large numbers of similar lengths. Can anyone explain this?

#include <iostream>
#include <fstream>  // for files
#include <cstdlib>   // for exit
#include <climits> // for INT_MAX etc.
int main( )
{
using namespace std;

ofstream outfile;
outfile.open("Whats_in_computer_memory.txt");

cout << "In this program, we declare a small array and then use array syntax " << endl
     << "to see what is in the computer's memory. " << endl
     << " " << endl
     << " " << endl
     << " " << endl
     << " " << endl
     << " " << endl;

int a[1];

int histogram[214749];

for (int i = 0; i < 214749; i++ )
{
    histogram[i] = 0;
}
cout << "&a[0] = " << &a[0] << endl
     << "&histogram[0] = " << &histogram[0]<< endl
     << " " << endl;

cout << INT_MAX << endl;

for (int i = 0; i < 1000; i++ )
{
    outfile << a[i] << endl;
    //cout << INT_MAX/1000000 +a[i]/1000000 << endl;
    //histogram[  a[i]/1000000 ]++;
}

for (int i = 0; i < 2 ; i++ )
{
    cout << histogram[ i ] << endl;
}


char dummy;

cin >> dummy;
return 0;

}

9
  • what is the output of the program? Apr 13, 2016 at 18:49
  • What did you expect to see? You're reading way past the bounds of an array. The program could be showing you anything. Is it one of those professors who try to show you "how powerful C++ is" by using things that are explicitly illegal but look cool? Apr 13, 2016 at 18:49
  • 6
    Your professor likes undefined behavior?
    – Chad
    Apr 13, 2016 at 18:49
  • 1
    This seems like a useless exercise. What point is your professor trying to make? Apr 13, 2016 at 18:51
  • 5
    If I were your parent, I'd file a complaint with the faculty on the ground of total incompetence of your educator.
    – SergeyA
    Apr 13, 2016 at 19:00

2 Answers 2

10

a has one element. That means

for (int i = 0; i < 1000; i++ )
{
    outfile << a[i] << endl;
    //cout << INT_MAX/1000000 +a[i]/1000000 << endl;
    //histogram[  a[i]/1000000 ]++;
}

is undefined behavior as soon as i >= 1. Once you have undefined behavior anything can happen so we can no longer reason out what is going on.

1
  • Much better now :)
    – SergeyA
    Apr 13, 2016 at 19:01
7

Your professor is trying to demonstrate that the area of memory around your program contains "stuff" - specifically that the memory around your program isn't zero'd or set to any other default value.

Presumably, this is part of a further point that accessing memory out-of-bounds doesn't guarantee that you'll have any particular value, and as such need to initialise your variables accordingly.

However; the problem is that we consider "accessing out-of-bounds of an array" to be undefined behaviour. Undefined behaviour in the C++ standard means "the compiler can handle this situation in whatever way it considers appropriate". Most compilers will serve you values from nearby memory locations (this being simple to do), but they're not required to do so.

Likewise, some compilers can aggressively optimise-out undefined behaviour (like concluding that i can never be greater than 1 when used to index into a, and adjusting the loop appropriately): compilers are free to assume (for optimisation purposes) that undefined behaviour is never invoked by the programmer. See this page from the LLVM project for more details on undefined behaviour (noting that under "Dereferences of Wild Pointers and Out of Bounds Array Accesses", both the Clang team and g++ have made the same decision with regards to handling out-of-bounds errors: they access nearby memory locations; because it's the simplest thing to do in that case).

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.