| Thread overview | |||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 24, 2015 Dynamic memory | ||||
|---|---|---|---|---|
| ||||
How do we get dynamic memory in D ? I want to use memory based on user input. In this case declare a bi-dimensional array (int[2][var]), var being the user input. | ||||
July 24, 2015 Re: Dynamic memory | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Binarydepth | On Friday, 24 July 2015 at 15:22:15 UTC, Binarydepth wrote:
> I want to use memory based on user input. In this case declare a bi-dimensional array (int[2][var]), var being the user input.
Declare:
int[2][] your_array;
your_array.length = var;
The runtime will handle the dynamic memory allocation for you when you set the length on a slice.
| |||
July 24, 2015 Re: Dynamic memory | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Friday, 24 July 2015 at 15:26:27 UTC, Adam D. Ruppe wrote:
> On Friday, 24 July 2015 at 15:22:15 UTC, Binarydepth wrote:
>> I want to use memory based on user input. In this case declare a bi-dimensional array (int[2][var]), var being the user input.
>
> Declare:
>
> int[2][] your_array;
>
> your_array.length = var;
>
>
> The runtime will handle the dynamic memory allocation for you when you set the length on a slice.
this is what I did :
int liCases [2][];
readf(" %d\n", &num);//Number of cases input
liCases.length = num;
And I get this error :
prime1.d:26: error: constant liCases.length is not an lvalue
| |||
July 24, 2015 Re: Dynamic memory | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Friday, 24 July 2015 at 15:26:27 UTC, Adam D. Ruppe wrote:
> On Friday, 24 July 2015 at 15:22:15 UTC, Binarydepth wrote:
>> I want to use memory based on user input. In this case declare a bi-dimensional array (int[2][var]), var being the user input.
>
> Declare:
>
> int[2][] your_array;
>
> your_array.length = var;
>
>
> The runtime will handle the dynamic memory allocation for you when you set the length on a slice.
Ok, Sorry I was declaring the array in C style also. It compiled after chaging it. :D
TY
| |||
July 24, 2015 Re: Dynamic memory | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Binarydepth | On Friday, 24 July 2015 at 15:33:45 UTC, Binarydepth wrote:
> int liCases [2][];
Those brackets are in the wrong place, you should write that as
int[2][] liCases;
The syntax you used there is a deprecated C compatibility feature. in C, arrays are defined differently and the dimensions go in the opposite direction than in D. (So int a[2][] in C means int[][2] in D - that's why the length doesn't change in that format.)
But write it the D style for most consistency and it will work out. Just be aware that what I wrote is a dynamic array of two elements, not a two element group of dynamic arrays. So indexing is
liCases[i][0] and liCases[i][1] rather than swapping those.
| |||
July 28, 2015 Re: Dynamic memory | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | Here is what I'm trying to do :
import std.stdio : readf, writef;
void main() {
int[2][] nam;
int num;
readf(" %d", &num);
nam.length = num;
foreach(nim; 0..num) {
readf(" %d %d", &nam[0][num], &nam[1][num]);
}
foreach(nim; 0..num) {
writef(" %d %d\n", &nam[0][num], &nam[1][num]);
}
}
And here is the output I get when running the program :
core.exception.RangeError@code.d(8): Range violation
----------------
0x406cfb _Dmain
???:0
0x415fde void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runAll().void __lambda1()
../../../../src/libphobos/libdruntime/rt/dmain2.d:408
0x41624e void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate())
../../../../src/libphobos/libdruntime/rt/dmain2.d:383
0x4164a8 void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runAll()
../../../../src/libphobos/libdruntime/rt/dmain2.d:408
0x41624e void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate())
../../../../src/libphobos/libdruntime/rt/dmain2.d:383
0x4163d5 _d_run_main
../../../../src/libphobos/libdruntime/rt/dmain2.d:416
0x7f51eb84ea3f __libc_start_main
???:0
0x406088 _start
???:0
0xffffffffffffffff ???
???:0
----------------------------------------------------------------------------------
I just want to read and print data depending on user input.
| |||
July 28, 2015 Re: Dynamic memory | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Binarydepth | On Tuesday, 28 July 2015 at 16:09:46 UTC, Binarydepth wrote:
> readf(" %d %d", &nam[0][num], &nam[1][num]);
> }
> foreach(nim; 0..num) {
> writef(" %d %d\n", &nam[0][num], &nam[1][num]);
Those indexes are backwards. And you really shouldn't need & on write. so try:
readf(" %d %d", &nam[num][0], &nam[num][1]);
and
writef(" %d %d\n", nam[num][0], nam[num][1]);
| |||
July 28, 2015 Re: Dynamic memory | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Tuesday, 28 July 2015 at 16:12:28 UTC, Adam D. Ruppe wrote:
> On Tuesday, 28 July 2015 at 16:09:46 UTC, Binarydepth wrote:
>> readf(" %d %d", &nam[0][num], &nam[1][num]);
>> }
>> foreach(nim; 0..num) {
>> writef(" %d %d\n", &nam[0][num], &nam[1][num]);
>
> Those indexes are backwards. And you really shouldn't need & on write. so try:
>
> readf(" %d %d", &nam[num][0], &nam[num][1]);
>
> and
>
> writef(" %d %d\n", nam[num][0], nam[num][1]);
I declared : int[2][] nam;
For real it is backwards ? nam[0][num]
| |||
July 28, 2015 Re: Dynamic memory | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Binarydepth | On Tuesday, 28 July 2015 at 16:09:46 UTC, Binarydepth wrote:
> Here is what I'm trying to do :
>
> import std.stdio : readf, writef;
> void main() {
> int[2][] nam;
> int num;
> readf(" %d", &num);
> nam.length = num;
> foreach(nim; 0..num) {
> readf(" %d %d", &nam[0][num], &nam[1][num]);
> }
> foreach(nim; 0..num) {
> writef(" %d %d\n", &nam[0][num], &nam[1][num]);
> }
> }
In addition to Adam:
there are typos (num instead of nim) - since num is the array length and the indices are 0-based, num is out of bounds...
foreach(nim; 0..num) {
readf(" %d %d", &nam[nim][0], &nam[nim][1]);
}
foreach(nim; 0..num) {
writef(" %d %d\n", nam[nim][0], nam[nim][1]);
}
works fine.
| |||
July 28, 2015 Re: Dynamic memory | ||||
|---|---|---|---|---|
| ||||
Posted in reply to anonymous | On Tuesday, 28 July 2015 at 16:24:39 UTC, anonymous wrote:
> On Tuesday, 28 July 2015 at 16:09:46 UTC, Binarydepth wrote:
>> Here is what I'm trying to do :
>>
>> import std.stdio : readf, writef;
>> void main() {
>> int[2][] nam;
>> int num;
>> readf(" %d", &num);
>> nam.length = num;
>> foreach(nim; 0..num) {
>> readf(" %d %d", &nam[0][num], &nam[1][num]);
>> }
>> foreach(nim; 0..num) {
>> writef(" %d %d\n", &nam[0][num], &nam[1][num]);
>> }
>> }
>
>
> In addition to Adam:
> there are typos (num instead of nim) - since num is the array length and the indices are 0-based, num is out of bounds...
>
> foreach(nim; 0..num) {
> readf(" %d %d", &nam[nim][0], &nam[nim][1]);
> }
> foreach(nim; 0..num) {
> writef(" %d %d\n", nam[nim][0], nam[nim][1]);
> }
> works fine.
Damn! my first typo! Thanks :D
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply