Range violation error in the code

spir denis.spir at gmail.com
Tue Apr 12 06:31:45 PDT 2011


On 04/12/2011 02:20 PM, Ishan Thilina wrote:
> I can compile the following code. But when I run the program it gives me a
> "core.exception.RangeError at untitled(34): Range violation
> " error.
>
> The code is as follows.
>
> "
> import std.stdio;
>
> int main(char[][] args)
> {
> 	 struct Node{
> 		int _value;
> 		Node* _next,_prev,_up,_down;
> 	}
>
> 		Node*[]  pointers;
> 		int i=0;
>
> 		auto  n=new Node;
> 		pointers[i]=n;
>
> 	return 0;
> }
>
> "
>
> Here's the error.
>
> "
> core.exception.RangeError at untitled(34): Range violation
> [...]
> As it seems the problem is with the line "pointers[i]=n". What's wrong here? :s

There is no node in pointers as of now, thus pointers[i] can only be a range 
violation, whatever i (even 0, which should point to the *first* node).
     pointers[i]=n;
would *change* the current element number i. To put a *new* node into pointers, 
if that's what you intended, use the '~' appending operator (here in version '~=');
     pointers ~= n;

Denis
-- 
_________________
vita es estrany
spir.wikidot.com



More information about the Digitalmars-d-learn mailing list