digitalmars.D.bugs - Returning Pointers
- Wagner Engel (13/13) Nov 20 2005 I'm trying to return a pointer, but all I get is "Error: Acess Violation...
- Derek Parnell (23/39) Nov 20 2005 Unlike C++, in D you must explictly 'new' a class to bring it into
- Chris (7/10) Nov 21 2005 the C# and Java compilers do their best detect when an uninstantiated
- Jarrett Billingsley (9/13) Nov 21 2005 Or, like I suggested, insert an implicit "assert(obj !is null)" for ever...
- Sean Kelly (10/22) Nov 21 2005 DBC is useful for this:
- Derek Parnell (8/10) Nov 20 2005 But on a more general issue, why use pointers at all? With D, most
- clayasaurus (2/12) Nov 21 2005 If you want to use older C libraries that use pointers.
I'm trying to return a pointer, but all I get is "Error: Acess Violation" when I run the code below. I'm using dmd v0.139: class C { char *str; this() { str = cast(char*) malloc(char.sizeof * 10); } char* get() { return str; } } int main() { C s; char *c; c = s.get(); return 0; }
Nov 20 2005
On Mon, 21 Nov 2005 03:15:50 +0000 (UTC), Wagner Engel wrote:I'm trying to return a pointer, but all I get is "Error: Acess Violation" when I run the code below. I'm using dmd v0.139: class C { char *str; this() { str = cast(char*) malloc(char.sizeof * 10); } char* get() { return str; } } int main() { C s; char *c; c = s.get(); return 0; }Unlike C++, in D you must explictly 'new' a class to bring it into existance. If you don't all you have is a null reference. This is the cause of your access violation. Try this ... ======================== class C { char *str; this() { str = cast(char*) malloc(char.sizeof * 10); } char* get() { return str; } } int main() { C s = new C; // <<<--- 'new' must be used. char *c; c = s.get(); return 0; } ======================== -- Derek (skype: derek.j.parnell) Melbourne, Australia 21/11/2005 2:21:11 PM
Nov 20 2005
On Mon, 21 Nov 2005 14:25:29 +1100, Derek Parnell <derek psych.ward> wrote:Unlike C++, in D you must explictly 'new' a class to bring it into existance. If you don't all you have is a null reference. This is the cause of your access violation.object is used. It would be nice to have this in the D compiler as well, at least as a warning (if they are turned on). Anything to reduce the very irritating access violations. Chris
Nov 21 2005
"Chris" <ctlajoie yahoo.com> wrote in message news:acu3o1d2cdanejf7u0ebtsda5cenj9lgru 4ax.com...object is used. It would be nice to have this in the D compiler as well, at least as a warning (if they are turned on). Anything to reduce the very irritating access violations.Or, like I suggested, insert an implicit "assert(obj !is null)" for every object access in the debug build (or with a switch for the debug build), akin to the implicit array bounds checking in debug mode. This wouldn't require any huge change in the compiler, wouldn't require any fancy initialization determination, and the utility would be undeniable. Of course, Walter seems to think that Memory Access Violations are more than helpful in this matter, so don't expect this to be implemented. :P
Nov 21 2005
Jarrett Billingsley wrote:"Chris" <ctlajoie yahoo.com> wrote in message news:acu3o1d2cdanejf7u0ebtsda5cenj9lgru 4ax.com...DBC is useful for this: void fn( MyClass c ) in { assert( c ); } body { } as the problem tends to be caught fairly quickly, and usually without an access violation. Seanobject is used. It would be nice to have this in the D compiler as well, at least as a warning (if they are turned on). Anything to reduce the very irritating access violations.Or, like I suggested, insert an implicit "assert(obj !is null)" for every object access in the debug build (or with a switch for the debug build), akin to the implicit array bounds checking in debug mode. This wouldn't require any huge change in the compiler, wouldn't require any fancy initialization determination, and the utility would be undeniable.
Nov 21 2005
On Mon, 21 Nov 2005 03:15:50 +0000 (UTC), Wagner Engel wrote:I'm trying to return a pointer, but all I get is "Error: Acess Violation" when I run the code below. I'm using dmd v0.139:But on a more general issue, why use pointers at all? With D, most circumstances no longer require the use of pointers. -- Derek (skype: derek.j.parnell) Melbourne, Australia 21/11/2005 2:26:41 PM
Nov 20 2005
Derek Parnell wrote:On Mon, 21 Nov 2005 03:15:50 +0000 (UTC), Wagner Engel wrote:If you want to use older C libraries that use pointers.I'm trying to return a pointer, but all I get is "Error: Acess Violation" when I run the code below. I'm using dmd v0.139:But on a more general issue, why use pointers at all? With D, most circumstances no longer require the use of pointers.
Nov 21 2005