Thread overview
Range violation error in the code
Apr 12, 2011
Ishan Thilina
Apr 12, 2011
Christian Manning
Apr 12, 2011
Ishan Thilina
Apr 12, 2011
spir
Apr 12, 2011
Ishan Thilina
April 12, 2011
I can compile the following code. But when I run the program it gives me a
"core.exception.RangeError@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@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
April 12, 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@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@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.
April 12, 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@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@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

April 12, 2011
On Tue, 12 Apr 2011 08:20:20 -0400, Ishan Thilina <ishanthilina@gmail.com> wrote:

> I can compile the following code. But when I run the program it gives me a
> "core.exception.RangeError@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@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

An array does not dynamically adjust its length when you assign an element, you have to assign the length explicitly before-hand.  Some dynamic languages do this (like Javascript), but not D.

You can achieve this with an associative array:

Node*[int] pointers;

However, iterating an AA does not guarantee order.  So it depends on your requirements.

-Steve
April 12, 2011
-Christian Manning wrote:

>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.

Thanks, it works...!

-Denis wrote:
>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;

Yes, that's exactly what I wanted to do. Thanks...!
April 12, 2011
>An array does not dynamically adjust its length when you assign an element, you have to assign the length explicitly before-hand.  Some dynamic languages do this (like Javascript), but not D.
>
>You can achieve this with an associative array:
>
>Node*[int] pointers;
>
>However, iterating an AA does not guarantee order.  So it depends on your requirements.
>
>-Steve

Thank you..!. That clarifies the things a lot. :)