www.digitalmars.com         C & C++   DMDScript  

D - pointer to struct

reply "Carlos Santander B." <carlos8294 msn.com> writes:
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
Jun 23 2003
next sibling parent reply Burton Radons <loth users.sourceforge.net> writes:
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?
Jun 23 2003
parent reply "Carlos Santander B." <carlos8294 msn.com> writes:
"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
Jun 23 2003
parent reply Ilya Minkov <midiclub 8ung.at> writes:
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.
Jun 23 2003
next sibling parent reply "Carlos Santander B." <carlos8294 msn.com> writes:
"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
Jun 23 2003
parent reply Ilya Minkov <midiclub 8ung.at> writes:
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.
Jun 23 2003
parent "Walter" <walter digitalmars.com> writes:
"Ilya Minkov" <midiclub 8ung.at> wrote in message
news:bd7qoq$22th$1 digitaldaemon.com...
 Walter or some other guru, could you please explain why the obvious "new
 A" is prohibited?
That looks like a bug I need to fix.
 BTW, is there a legal way for returning a stack-allocated struct from a
 function?
Yes. If you just return it, a copy will be made of it. If you return a pointer to it, well, your code will have a pointer bug.
Jun 24 2003
prev sibling parent reply Burton Radons <loth users.sourceforge.net> writes:
Ilya Minkov wrote:

 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;
Err, no. The type after new is a literal description outside of static arrays, so what you're allocating with that new is a stack object. "new A[1];" is the correct expression.
 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'm not as against C-style cast syntax as I am against D-style cast syntax, which is horrid. I don't like syntax that requires semantic analysis like the C-style cast does in D, though.
Jun 27 2003
parent Ilya Minkov <midiclub 8ung.at> writes:
Burton Radons wrote:
 Err, no.  The type after new is a literal description outside of static 
 arrays, so what you're allocating with that new is a stack object.  "new 
 A[1];" is the correct expression.
Oh... OK. I considereed new as allocating sizeof(expr) space on the GC heap, so the "stack semantics" somewhat irritates me. :/ And doesn't make A[1] any more obvious in this case. -i.
Jun 27 2003
prev sibling next sibling parent "Vathix" <Vathix dprogramming.com> writes:
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
Jun 23 2003
prev sibling next sibling parent reply Helmut Leitner <helmut.leitner chello.at> writes:
"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
Jun 23 2003
parent reply "Carlos Santander B." <carlos8294 msn.com> writes:
"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
Jun 23 2003
parent "Fabian Giesen" <rygNO SPAMgmx.net> writes:
 It doesn't work for what I'm trying to do.
Why not use a class then, which is by definition a reference? -fg
Jun 23 2003
prev sibling parent Derek Parnell <derek.parnell no.spam> writes:
On Mon, 23 Jun 2003 14:17:21 -0500 (06/24/03 05:17:21)
, Carlos Santander B. <carlos8294 msn.com> 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:

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

 And it actually works.
I would have thought that ... A* a; only declares a variable 'a' such that it is possible for it to hold an address (pointer to an 'A'), but at this moment in time, 'a' does NOT have an address in it. It holds an undefined value. Thus if you use it without doing anything else, your app will be referencing some spot in RAM that is probably not under your app's control, and so BANG!, an access violation occurs. The malloc() call (or something similar) is needed to cause 'a' to hold a valid address value. a = new A; would seem to be a simple way of doing this, but I don't think D is this simple. ;-) If you use malloc() I think you are obliged to manage the memory yourself. The GC does not take responsibility for it in this case. -- Derek
Jun 23 2003