Questions tagged [range-checking]

29 questions
81
votes
7 answers

Is it more efficient to perform a range check by casting to uint instead of checking for negative values?

I stumbled upon this piece of code in .NET's List source code: // Following trick can reduce the range check by one if ((uint) index >= (uint)_size) { ThrowHelper.ThrowArgumentOutOfRangeException(); } Apparently this is more efficient (?) than if…
enzi
  • 4,057
  • 3
  • 35
  • 53
77
votes
2 answers

Why does Python allow out-of-range slice indexes for sequences?

So I just came across what seems to me like a strange Python feature and wanted some clarification about it. The following array manipulation somewhat makes sense: p = [1,2,3] p[3:] = [4] p = [1,2,3,4] I imagine it is actually just appending…
Akaisteph7
  • 5,034
  • 2
  • 20
  • 43
21
votes
7 answers

Function checking if an integer type can fit a value of possibly different (integer) type

Is it possible to create a templated function that checks if a primitive data type can fit a value of potentially different primitive data type? Let's limit the scope to integer types for the moment. More precisely: Is it possible to create a "one…
Antonio
  • 19,451
  • 13
  • 99
  • 197
19
votes
3 answers

Switch off Delphi range checking for a small portion of code only

How can one switch off range checking for a part of a file. Switching off is easy, but how do I revert to the project setting later on? The pseudo-code below should explain it: Unit1; //here's range checking on or off as per the project…
Giel
  • 2,066
  • 20
  • 22
16
votes
1 answer

Why doesn't "i := i + 1" give a range-check error for Integers and larger types?

Consider: {$R+} i:= 1; While i > 0 do i:= i + 1; ShowMessage(IntToStr(i)); If I declare i as Byte, Word, Shortint or TinyInt I get a range-check error, as expected. If I declare i as LongWord, Cardinal, Integer, LongInt or Int64 it just goes…
Pieter B
  • 1,874
  • 10
  • 22
6
votes
2 answers

Why no runtime error when clearly writing over array bounds?

I have a program that assigns an array beyond it's bounds, and I was expecting a run-time error to be thrown. Yet no error is raised at all and the program proceeds to write into undeclared memory. Is there some compiler option to guard against…
John Alexiou
  • 28,472
  • 11
  • 77
  • 133
6
votes
1 answer

Is it OK to use exceptions to check for array boundaries?

I want to check whether the given coordinates are withing an array or not. public boolean checkBounds(int x, int y) { try { Object val = array[x][y]; return true; } catch (ArrayIndexOutOfBoundsException e) { return…
Dariusz
  • 21,561
  • 9
  • 74
  • 114
2
votes
5 answers

Checking at compile time if specified value is in a range of a type

Is it possible to check this: template struct X{}; What I mean by this is, is it possible to check that value supplied by user will "fit" into IntType (which can be any of std integer types) type? For example, I would…
There is nothing we can do
  • 23,727
  • 30
  • 106
  • 194
2
votes
3 answers

Highlight rows where the same values in columns A:B but different values in column C

I have 4000+ rows of data need to be working on. Where column A represents the SKU, column B represents the unit and column C represents the Unit Price. The same SKU, Unit and Unit Price may duplicate at their own columns as shown as the picture…
1
vote
1 answer

Range checking for operator[] with std::vector and std::array in debug mode

I'm writing some numerical C++ where both std::vector and std::array are being used in performance critical parts of the code. Preferably I would like the operator[] to do range checking in debug mode to weed out any potential out of bounds…
ander
  • 11
  • 3
1
vote
1 answer

Enumeration Range Checking

Delphi doesn't directly support any range checking or out-of-range excption raising for typecasts of integers to an enumeration. See: How do I convert an integer to an enumerated type? I am trying to get around this by automatic range checking of an…
PJH
  • 87
  • 5
1
vote
5 answers

Check if an Edit Text that only accepts number is not empty and the number is equal or less than 100

I'm building an application for receiving grades and I want to make sure that the Edit Texts are not empty and the values are less or equal to 100 I wrote this line but it crashes the application if(Integer.parseInt(editText.gettext().toString()) >…
Nima
  • 41
  • 1
  • 2
  • 7
1
vote
4 answers

Range checking using regular expressions?

How to perform range checking using regular expressions? Take a 4-bit number (i.e. "dddd") as an example, how can I check whether it is within given range, say [1256-4350] or not?
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
1
vote
1 answer

Extending iterators in C++

I'm trying to implement an iterator which encapsulates another iterator and performs range checking. Therefore I'm extending from that Iterator like so: template class r_iterator : public ITERATOR_T { //... …
ben
  • 5,671
  • 4
  • 27
  • 55
1
vote
1 answer

Instantiator Function for Bound template doesn't compile

I'm trying to implement an instantiator function for my Bound template wrapper but I can't get it to work. I need this in order to convince people at work that we should switch from Ada to D. I want this template /** Bounded Value of Type T.…
Nordlöw
  • 11,838
  • 10
  • 52
  • 99
1
2