Something strange...

Why does the following code compile?

1
2
3
int fuck[10];
fuck[-2000] = 3;
cout << "fuck[-2000] = " << fuck[-2000] << "\n" << endl;


I've just noticed something similar to it in my dlist class, which uses an bool array to keep track of how many digits are possible in a sudoku.

a function from my dlist:
1
2
3
4
5
void dlist::addN(const int & x)
{
    digits[x-1] = true;
    return;
}


But when I tried implementing my crowdout inference rule using an array of dlists to keep track of which rows a number is possible in any 1 given column, I forgot that rows and columns start from 0, but numbers from 1. So the above member function works when I want to keep a list of numbers starting from 1, but not from 0. Another good reason to always document/comment your code! Had the top code snippet failed to compile (as I presume should have been the case) I could have been saved hours trying to debug.... alas...

Any ideas on how to implement my dlist for rows, columns, and other datatypes that logically start from 0 without having to remember to increment each function call? i.e. crowd[i].addN(rows+1);
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
using namespace std;

template <class T, int N>
class ShiftedArray //hahaha
{
private:
    T arr[N];
    static T foo;

public:
    T & operator[](int id)
    {
        if (id<1 || id>N)
        {
            cerr << "out of bounds!\n";
            return foo;
        }

        return arr[id-1];
    }

    const T & operator[](int id) const
    {
        if (id<1 || id>N)
        {
            cerr << "out of bounds!\n";
            return foo;
        }

        return arr[id-1];
    }
};

template <class T, int N>
T ShiftedArray<T,N>::foo;

int main()
{
    const int size=5;

    ShiftedArray<int,size> my_arr;

    int i;
    for (i=1; i<=size; i++)
    {
        cout << "enter my_arr[" << i << "]: ";
        cin >> my_arr[i];
    }

    cin.get();

    cout << "\nyour array is:\n";
    for (i=1; i<=size; i++)
    {
        cout << "my_arr[" << i << "]: ";
        cout << my_arr[i] << endl;
    }

    cout << "\nlet's try some bad things...\n";
    my_arr[0]=3;
    my_arr[-2]=5;
    my_arr[size+1]=7;

    cout << "\nhit enter to quit";
    cin.get();
    return 0;
}
Your code compiles because the compiler does no bounds checking. It probably will result in a few unwanted runtime errors, though, like Segmentation Fault.

-Albatross
and usually does. but Ive just spent a few minutes testing it and it seems to cout the correct results if its only slightly out of bounds.
so:
1
2
3
int fuck [10];
fuck[20] = 3;
cout << "fuck[20] = " << fuck[20] << endl;


would display:

fuck[20] = 3

Probably it had not as much to do with the fact that it's less out of bounds than fbomb[-2000] than the fact that your section of memory wasn't allocated by any other program, and therefore not write protected, while fbomb[-2000] might have been.

-Albatross
Topic archived. No new replies allowed.