www.digitalmars.com         C & C++   DMDScript  

D - Dynamic arrays

reply Karl Bochert <kbochert copper.net> writes:
Is there any word on when dynamic arrays might be functional?

I wish to start a D project, but with neither alloc-free nor dynamic
arrays I seem to be unable to do anything at all.

Are dynamic arrays due in days? weeks? months? unknown?

Thanx
Karl Bochert
Dec 04 2002
parent reply Evan McClanahan <evan dontSPAMaltarinteractive.com> writes:
Karl Bochert wrote:
 Is there any word on when dynamic arrays might be functional?
 
 I wish to start a D project, but with neither alloc-free nor dynamic
 arrays I seem to be unable to do anything at all.
 
 Are dynamic arrays due in days? weeks? months? unknown?
 
 Thanx
 Karl Bochert

afaik, both dynamic arrays and and alloc/free are in D at the moment. To get to malloc and free (which aren't the way that D is meant to be used, but to each his own) you do : import c.stdlib; and either: var * varp = malloc(var.size); or: var * varp = stdlib.malloc(var.size); which should give you your system standard malloc (also calloc, free, alloca, and realloc). if it is the second one, and the stdlib. annoys you, you can always do this: alias stdlib.malloc malloc; etc. I'm not sure I understand which problems you're having with dynamic arrays. Care to describe them in more detail? Evan
Dec 05 2002
parent reply Karl Bochert <kbochert copper.net> writes:
 
 afaik, both dynamic arrays and and alloc/free are in D at the moment. 
 To get to malloc and free (which aren't the way that D is meant to be 
 used, but to each his own) you do :
 
 import c.stdlib;
 
 and either:
 
 var * varp = malloc(var.size);
 
 or:
 
 var * varp = stdlib.malloc(var.size);
 
 which should give you your system standard malloc (also calloc, free, 
 alloca, and realloc).
 
 if it is the second one, and the stdlib. annoys you, you can always do this:
 
 alias stdlib.malloc malloc;
 
 etc.

Thanks -- I keep thinking that if its not in the manual it doesn't exist, but of course D isn't at that point yet. I would much prefer dynamic arrays if they worked.
 
 I'm not sure I understand which problems you're having with dynamic 
 arrays.  Care to describe them in more detail?

See my 'Stuid Question' thread. Basically my system instantly crashes when I try to do anything that involves enlarging a dynamic array. For instance: char[] s; char[4] t = "test" s = t.dup; // bomb s.length = 6; // bomb Is this problem unique to me? Is it only a problem in the latest release?
 Karl
 

Dec 05 2002
parent reply Evan McClanahan <evan dontSPAMaltarinteractive.com> writes:
Karl Bochert wrote:

 Thanks -- I keep thinking that if its not in the manual it doesn't exist,
 but of course D isn't at that point yet.
 I would much prefer dynamic arrays if they worked.

yeah, sometimes you have to dig or ask to find anything. But it's a small project as of yet, and I'm sure that the documentation will improve over time.
 See my 'Stuid Question'  thread.
 Basically my system instantly crashes when I try to do anything
  that involves enlarging a dynamic array.
  
 For instance:
     char[] s;
     char[4] t = "test"
     s =  t.dup;        // bomb
     s.length = 6;    // bomb
 
 Is this problem unique to me?
  Is it only a problem in the latest release?

ok, I've tried some things. Here are some suggestions. char[] s; char[] t = "test"; s = t[0..4]; s.length = 7; s[4] = '!'; printf("%.*s\n", s); //prints 'test!' this works. char[] s; char[] t = "test"; s ~= t; s.length = 7; s[4] = '!'; printf("%.*s\n", s); this also works. char[] s; char[4] t = "test"; s ~= t; s.length = 7; s[4] = '!'; printf("%.*s\n", s); that too... char[] s; char[] t = "test"; s = t.dup; s.length = 7; s[4] = '!'; printf("%.*s\n", s); and here as well. so I think that the problem is that char[4] isn't the same type as char[], and the compiler isn't catching it and warning you (or giving you an error) at compile time. Which I think that it should, rather than just puking at runtime, but that's something to take up with Walter. I hope that this has been helpful, at least a little. Actually, trying something else, check the formatting on your original printf() because I just tried your original example again, and it worked. Note the printing discussion on the Arrays page in the Docs, down at the bottom. We really need a native printf() implementation. Evan
Dec 05 2002
parent reply Karl Bochert <kbochert copper.net> writes:
On Thu, 05 Dec 2002 17:25:58 +0100, Evan McClanahan
<evan dontSPAMaltarinteractive.com> wrote:
 Karl Bochert wrote:
 

 
 ok, I've tried some things.  Here are some suggestions.
 
      char[] s;
      char[] t = "test";
 
      s = t[0..4];
 
      s.length = 7;
 
      s[4] = '!';
 
      printf("%.*s\n", s);  //prints 'test!'
 
 this works.

Nope void foo () char[] s; char[] t = "test"; s = t[0..4]; // ok -- s is just a reference s.length = 7; // fails -- involves re-allocation s[4] = '!'; // fails -- s[3] = '!'; is ok printf("%.*s\n", s); //No output (Gui app??) } Called from WinMain() bombs. Is it just Windows applications? Walter said that he would look into it, so I guess I'll just have to be patient. I had hoped that it was just a typo in the latest release (dynamic arrays must have worked by now!). Is it just my system or am I really the first to try WinMain() ?? Karl
Dec 05 2002
parent reply "Mike Wynn" <mike.wynn l8night.co.uk> writes:
been using WinMain for a while (with a .def file so you don't get a console)
though this might be the reason my D directX app only runs for 5 mins then
exits without error, BUT

the code works fine for console apps
int main( char[][]args ) { ..... }

on ver 0.50 the following works ( compiled in one go with 'dmd test2.d
test2.def ' )

import windows;
import c.stdio;

extern (C) void gc_init();
extern (C) void gc_term();
extern (C) void _minit();
extern (C) void _moduleCtor();
extern (C) void _moduleUnitTests();

char[256] buf;

void begin()
{
 char[] s;
 char[] t = "test";

 s = t[0..4];
 s.length = 7;
 s[4] = "!";

 sprintf((char *)buf, "%.*s\n", s);
 MessageBoxA(null, (char *)buf, "what ?",
      MB_OK | MB_ICONEXCLAMATION);
}

extern (Windows)
int WinMain(HINSTANCE hInstance,
 HINSTANCE hPrevInstance,
 LPSTR lpCmdLine,
 int nCmdShow)
{
    int result;

    gc_init();   // initialize garbage collector
    _minit();   // initialize module constructor table

    try
    {
 _moduleCtor();  // call module constructors
 _moduleUnitTests(); // run unit tests (optional)

  begin();
    }

    catch (Object o)  // catch any uncaught exceptions
    {
 MessageBoxA(null, (char *)o.toString(), "Error",
      MB_OK | MB_ICONEXCLAMATION);
 result = 0;  // failed
    }

    gc_term();   // run finalizers; terminate garbage collector
    return 1;
}


"Karl Bochert" <kbochert copper.net> wrote in message
news:1103_1039111393 bose...
 On Thu, 05 Dec 2002 17:25:58 +0100, Evan McClanahan

 Karl Bochert wrote:

 ok, I've tried some things.  Here are some suggestions.

      char[] s;
      char[] t = "test";

      s = t[0..4];

      s.length = 7;

      s[4] = '!';

      printf("%.*s\n", s);  //prints 'test!'

 this works.

Nope void foo () char[] s; char[] t = "test"; s = t[0..4]; // ok -- s is just a reference s.length = 7; // fails -- involves re-allocation s[4] = '!'; // fails -- s[3] = '!'; is ok printf("%.*s\n", s); //No output (Gui app??) } Called from WinMain() bombs. Is it just Windows applications? Walter said that he would look into it, so I guess I'll just have to be patient. I had hoped that it was just a typo in the latest release (dynamic arrays must have worked by now!). Is it just my system or am I really the first to try WinMain() ?? Karl

Dec 05 2002
parent reply Karl Bochert <kbochert copper.net> writes:
On Thu, 5 Dec 2002 18:23:36 -0000, "Mike Wynn" <mike.wynn l8night.co.uk> wrote:

 
 on ver 0.50 the following works ( compiled in one go with 'dmd test2.d
 test2.def ' )
 
 import windows;
 import c.stdio;
 
 ....
 

Its a miracle! Not only does it work fine, but the information has magically appeared in the manual!! Thanx for your patience. Karl Bochert
Dec 05 2002
parent "Walter" <walter digitalmars.com> writes:
"Karl Bochert" <kbochert copper.net> wrote in message
news:1104_1039120628 bose...
 Its a miracle! Not only does it work fine, but the information has

 appeared in the manual!!

I love it when that happens. The gnomes have been at work <g>.
Jan 17 2003