D - D: nice, although still puzzling
- Chr. Grade (43/43) Apr 21 2004 /*
/* ** My first D-day; got three newbie questions: ** (1) Is there a way to avoid using malloc() in order to ** create instances of a structure? Something D-like? ** [Other than "Free Lists"] see: CreateMyList() below ** (2) Does the garbage collector manage memory that has ** been allocated with the c-lib funcs? -> ** Say, I pluck a node out of my list, never use it ** again during runtime, but won't free it explicitly. ** (3) Are there any new os-independent facilities that ** make file accesses more convenient in D? */ import std.c.stdlib; struct MyList { uint nFoo; MyList* psNext; } MyList* CreateMyList(uint nNodes) { MyList* psFirstNode = (MyList*)malloc(MyList.size); MyList* psTempNode = psFirstNode; printf("\n New Node at: 0x%X", psTempNode); psTempNode.nFoo = --nNodes; // I initially feared this coding style wouldn't be allowed. while(nNodes--) psTempNode = psTempNode.psNext = (MyList*)malloc(MyList.size), printf("\n New Node at: 0x%X", psTempNode), psTempNode.nFoo = nNodes; return (psFirstNode); } uint main() { // Great! This nonsense still works :) "D somehow", "well", "feels like C" && "that's nice"; MyList* psFirstNode = CreateMyList(0xFF); do printf("\n Val: %u", psFirstNode.nFoo); while(psFirstNode.nFoo != 0, psFirstNode = psFirstNode.psNext); // Well, but where's that gone... // psFirstNode.psNext = ((void*)0); return (0x0); }
Apr 21 2004
Chr. Grade wrote:** (1) Is there a way to avoid using malloc() in order to ** create instances of a structure? Something D-like? ** [Other than "Free Lists"] see: CreateMyList() belowI've yet to ever even use malloc() from D. As to your provided program... consider the following (untested) re-write: ------------------------------ struct MyList { uint foo; MyList* next; static MyList* create(uint nodes) { MyList first; MyList* tmp = &first; void report() { printf("\n New Node at: 0x%X", tmp); } report(); tmp.foo = --nodes; while (nodes--) { tmp.next = new MyList; tmp = tmp.next; report(); tmp.foo = nodes; } return &first; } } int main() { MyList* node = MyList.create(0xFF); while (node) { printf("\n Val: %u", node.foo); node = node.next; } return 0; } ------------------------------ -C. Sauls -Invironz
Apr 21 2004
"Chr. Grade" <Chr._member pathlink.com> wrote in message news:c66du7$1nd8$1 digitaldaemon.com.../* ** My first D-day; got three newbie questions: ** (1) Is there a way to avoid using malloc() in order to ** create instances of a structure? Something D-like? ** [Other than "Free Lists"] see: CreateMyList() belowYes. new MyList;** (2) Does the garbage collector manage memory that has ** been allocated with the c-lib funcs? ->No. If a program goes outside of the gc, then the programmer is totally in charge of that <g>.** Say, I pluck a node out of my list, never use it ** again during runtime, but won't free it explicitly.If it was allocated with malloc(), it must be explicitly free()'d.** (3) Are there any new os-independent facilities that ** make file accesses more convenient in D?std.file offers a few.
Apr 21 2004