Range violation error in the code

Christian Manning cmanning999 at gmail.com
Tue Apr 12 06:10:29 PDT 2011


On 12/04/2011 13:20, 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
> ----------------
> /home/ishan/untitled(onRangeError+0x28) [0x8098a08]
> /home/ishan/untitled(_d_array_bounds+0x16) [0x80968c6]
> /home/ishan/untitled(_D8untitled7__arrayZ+0x12) [0x8094332]
> /home/ishan/untitled(_Dmain+0x35) [0x8094309]
> /home/ishan/untitled(_D2rt6dmain24mainUiPPaZi7runMainMFZv+0x1a) [0x8096a26]
> /home/ishan/untitled(_D2rt6dmain24mainUiPPaZi7tryExecMFMDFZvZv+0x20) [0x80969b8]
> /home/ishan/untitled(_D2rt6dmain24mainUiPPaZi6runAllMFZv+0x32) [0x8096a6a]
> /home/ishan/untitled(_D2rt6dmain24mainUiPPaZi7tryExecMFMDFZvZv+0x20) [0x80969b8]
> /home/ishan/untitled(main+0x94) [0x8096964]
> /lib32/libc.so.6(__libc_start_main+0xe7) [0xf7613ce7]
> /home/ishan/untitled() [0x8094221]
> "
>
> As it seems the problem is with the line "pointers[i]=n". What's wrong here? :s

Seems like Node*[] pointers needs to have a defined length before 
allocating to an index as adding "++pointers.length;" before 
"pointers[i]=n;" makes it work fine.


More information about the Digitalmars-d-learn mailing list