Jump to page: 1 2
Thread overview
pointer to struct
Jun 23, 2003
Burton Radons
Jun 23, 2003
Ilya Minkov
Jun 23, 2003
Ilya Minkov
Jun 24, 2003
Walter
Jun 27, 2003
Burton Radons
Jun 27, 2003
Ilya Minkov
Jun 23, 2003
Vathix
Jun 23, 2003
Helmut Leitner
Jun 23, 2003
Fabian Giesen
Jun 24, 2003
Derek Parnell
June 23, 2003
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


June 23, 2003
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?

June 23, 2003
"Burton Radons" <loth@users.sourceforge.net> escribió en el mensaje
news:bd7jne$1ffs$1@digitaldaemon.com...
| 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?
|

struct A { int x; }
A* a; /////////[1]
a.x=7;  //<------ Access violation

changing [1] to:

A* a=(A*)malloc(A.size);

works.

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


June 23, 2003
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.

June 23, 2003
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
>
>


June 23, 2003
"Ilya Minkov" <midiclub@8ung.at> escribió en el mensaje
news:bd7l4k$1i25$1@digitaldaemon.com...
| 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;
|

I had already tried it."new is only for classes and arrays not A's". DMD gives something like that.

-------------------------
Carlos Santander
"Ilya Minkov" <midiclub@8ung.at> escribió en el mensaje
news:bd7l4k$1i25$1@digitaldaemon.com...
| 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;
|

I had already tried it."new is only for classes and arrays not A's". DMD gives something like that.

-------------------------
Carlos Santander


June 23, 2003

"Carlos Santander B." wrote:
> 
> I have something like this:
> 
> struct A { ... }
> ...
> A* a;

Why not

  A a;

?

--
Helmut Leitner    leitner@hls.via.at Graz, Austria   www.hls-software.com
June 23, 2003
"Helmut Leitner" <helmut.leitner@chello.at> escribió en el mensaje
news:3EF761D3.DADC1472@chello.at...
|
| Why not
|
|   A a;
|

It doesn't work for what I'm trying to do.

-------------------------
Carlos Santander
"Helmut Leitner" <helmut.leitner@chello.at> escribió en el mensaje
news:3EF761D3.DADC1472@chello.at...
|
| Why not
|
|   A a;
|

It doesn't work for what I'm trying to do.

-------------------------
Carlos Santander


June 23, 2003
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.

June 23, 2003
> It doesn't work for what I'm trying to do.

Why not use a class then, which is by definition a reference?

-fg


« First   ‹ Prev
1 2