D - pointer to struct
- Carlos Santander B. (16/16) Jun 23 2003 I have something like this:
- Burton Radons (2/15) Jun 23 2003
-
Carlos Santander B.
(34/34)
Jun 23 2003
"Burton Radons"
escribió en el mensaje - Ilya Minkov (15/22) Jun 23 2003 Because on [1], only place for a pointer is reserved on stack, no heap
-
Carlos Santander B.
(40/40)
Jun 23 2003
"Ilya Minkov"
escribió en el mensaje - Ilya Minkov (8/10) Jun 23 2003 GRUNT. Then i believe the way by Vathix should work. Though it looks
- Walter (5/9) Jun 24 2003 That looks like a bug I need to fix.
- Burton Radons (7/34) Jun 27 2003 Err, no. The type after new is a literal description outside of static
- Ilya Minkov (5/8) Jun 27 2003 Oh... OK. I considereed new as allocating sizeof(expr) space on the GC
- Vathix (6/22) Jun 23 2003 a=(A*)malloc(A.size); won't be scanned by the GC unless you tell it to ...
- Helmut Leitner (7/13) Jun 23 2003 Why not
-
Carlos Santander B.
(20/20)
Jun 23 2003
"Helmut Leitner"
escribió en el mensaje - Fabian Giesen (2/3) Jun 23 2003 Why not use a class then, which is by definition a reference?
- Derek Parnell (19/27) Jun 23 2003 I would have thought that ...
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
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
"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
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
"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
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
"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
Ilya Minkov wrote:Carlos Santander B. 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.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'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
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
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 notusedor 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
"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
"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
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
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