I have something like this:
struct A { ... }
...
A* a;
But if I attempt to do anything with 'a', I get an access violation. The only way I can think to solve this is:
a=(A*)malloc(A.size);
And it actually works. However I'd like to know if there's a better way to do so. Assuming there's no other way, should I free 'a' after it's not used or will the GC take care of that?
-------------------------
Carlos Santander
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.491 / Virus Database: 290 - Release Date: 2003-06-18

Carlos Santander B. wrote:
> I have something like this:
> > struct A { ... }
> ...
> A* a;
> > But if I attempt to do anything with 'a', I get an access violation. The
> only way I can think to solve this is:
We can't do anything without an example of what fails!
> a=(A*)malloc(A.size);
> > And it actually works. However I'd like to know if there's a better way to
> do so. Assuming there's no other way, should I free 'a' after it's not used
> or will the GC take care of that?

Carlos Santander B. wrote:
> struct A { int x; }
> A* a; /////////[1]
> a.x=7; //<------ Access violation
> > changing [1] to:
> > A* a=(A*)malloc(A.size);
Because on [1], only place for a pointer is reserved on stack, no heap allocation is done. you may want to replace it with
A * a = new A;
which is basically the same as with malloc, but GC-friendly.
malloc is a part of C library, thus whatever it gives you is not collected by the GC.
Besides: try not to use the C-style cast, but instead "cast(newtype) expr", since the other may not be supported in future compilers. Reasons are following:
- the requierement to recognize C-style casts rejects some otherwise legal expressions;
- Burton is against having them, and i believe Walter as well, because they cannot be searched for.
-i.

a=(A*)malloc(A.size); won't be scanned by the GC unless you tell it to via
the gc module.
The better way would be to use: a = new A[1];
"Carlos Santander B." <carlos8294@msn.com> wrote in message news:bd7jkb$1ffl$1@digitaldaemon.com...> I have something like this:
>> struct A { ... }
> ...
> A* a;
>> But if I attempt to do anything with 'a', I get an access violation. The only way I can think to solve this is:
>> a=(A*)malloc(A.size);
>> And it actually works. However I'd like to know if there's a better way to do so. Assuming there's no other way, should I free 'a' after it's not
used
> or will the GC take care of that?
>> -------------------------
> Carlos Santander
>>> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.491 / Virus Database: 290 - Release Date: 2003-06-18
>>

Carlos Santander B. wrote:
> I had already tried it."new is only for classes and arrays not A's". DMD
> gives something like that.
GRUNT. Then i believe the way by Vathix should work. Though it looks somewhat... like not very obvious.
Walter or some other guru, could you please explain why the obvious "new A" is prohibited?
BTW, is there a legal way for returning a stack-allocated struct from a function?
-i.