digitalmars.D.bugs - Returning Pointers
- Wagner Engel <Wagner_member pathlink.com> Nov 20 2005
- Derek Parnell <derek psych.ward> Nov 20 2005
- Chris <ctlajoie yahoo.com> Nov 21 2005
- "Jarrett Billingsley" <kb3ctd2 yahoo.com> Nov 21 2005
- Sean Kelly <sean f4.ca> Nov 21 2005
- Derek Parnell <derek psych.ward> Nov 20 2005
- clayasaurus <clayasaurus gmail.com> Nov 21 2005
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.
the C# and Java compilers do their best detect when an uninstantiated 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...the C# and Java compilers do their best detect when an uninstantiated 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...the C# and Java compilers do their best detect when an uninstantiated 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.
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. Sean
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: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.
If you want to use older C libraries that use pointers.
Nov 21 2005









Sean Kelly <sean f4.ca> 