D - Memory Management issuse interfacing to C
- C <dont respond.com> Apr 13 2004
- C <dont respond.com> Apr 13 2004
- J Anderson <REMOVEanderson badmama.com.au> Apr 13 2004
- Russ Lewis <spamhole-2001-07-16 deming-os.org> Apr 13 2004
- C <dont respond.com> Apr 14 2004
------------U3GOiGqD9zaMqu9XgtdTCu
Content-Type: text/plain; format=flowed; charset=iso-8859-15
Content-Transfer-Encoding: Quoted-Printable
Hi all,
Im trying to create a linked-list struct in C, and retuning a pointer to=
=
it in D, but everytime I try to access the data it faults in D , though =
in =
C it works as expected.
Also trying to pass BackTrace* as a parameter to FillStruct and using D =
to =
allocate a BackTrace struct fails with same error.
Files attached, am I missing something obvious ?
dmc -c foo.c
dmd bar.d foo.obj
bar.exe
Thanks,
Charlie
-- =
D Newsgroup.
------------U3GOiGqD9zaMqu9XgtdTCu
Content-Disposition: attachment; filename=bar.d
Content-Type: application/octet-stream; name=bar.d
Content-Transfer-Encoding: Quoted-Printable
import std.c.stdio;
extern (C) {
struct tagBackTrace {
BackTraceItem* curr;
BackTraceItem* head; =
} ;
struct tagBackTraceItem{
char symName[4096];
char fileName[4096 +1];
int lineNumber;
BackTraceItem* next;
} ;
typedef tagBackTraceItem BackTraceItem;
typedef tagBackTrace BackTrace;
BackTrace* FillStruct() ;
}
void main () {
BackTrace* bt =3D FillStruct();
if ( bt =3D=3D null ) printf("NULLIFIED\n");
while ( bt.curr ) {
printf("MAIN : %s\n",bt.curr.symName );
}
}
------------U3GOiGqD9zaMqu9XgtdTCu
Content-Disposition: attachment; filename=foo.c
Content-Type: application/octet-stream; name=foo.c
Content-Transfer-Encoding: Quoted-Printable
#include <windows.h>
typedef struct tagBackTraceItem{
char symName[4096];
char fileName[MAX_PATH +1];
int lineNumber;
struct tagBackTraceItem* next;
} BackTraceItem;
typedef struct tagBackTrace {
BackTraceItem* curr;
BackTraceItem* head; =
} BackTrace ;
void AddItem(BackTrace* bt,char* name) { =
bt->curr =3D malloc(sizeof(BackTraceItem) );
if ( !bt->curr ) return; // Malloc failed, things are about to explode
strncpy(bt->curr->symName,name,4095);
bt->curr->next =3D bt->head;
bt->head =3D bt->curr;
}
BackTrace* FillStruct() {
int i;
BackTrace* bt =3D malloc(sizeof(BackTrace) );
for(i=3D1;i<=3D10;i++) {
AddItem(bt,"Mangled Symbol");
=
}
bt->curr =3D bt->head;
while(bt->curr) {
printf("FillStruct : %s\n", bt->curr->symName);
bt->curr =3D bt->curr->next ;
}
bt->curr =3D bt->head;
return bt;
}
------------U3GOiGqD9zaMqu9XgtdTCu--
Apr 13 2004
------------I5RmwB2wnVLjrRvcJwwVfm Content-Type: text/plain; format=flowed; charset=iso-8859-15 Content-Transfer-Encoding: Quoted-Printable Oops this should be the bar.d On Tue, 13 Apr 2004 17:03:20 -0700, C <dont respond.com> wrote:Hi all, Im trying to create a linked-list struct in C, and retuning a pointer =
it in D, but everytime I try to access the data it faults in D , thoug=
in C it works as expected. Also trying to pass BackTrace* as a parameter to FillStruct and using =
to allocate a BackTrace struct fails with same error. Files attached, am I missing something obvious ? dmc -c foo.c dmd bar.d foo.obj bar.exe Thanks, Charlie
-- = D Newsgroup. ------------I5RmwB2wnVLjrRvcJwwVfm Content-Disposition: attachment; filename=bar.d Content-Type: application/octet-stream; name=bar.d Content-Transfer-Encoding: Quoted-Printable import std.c.stdio; extern (C) { struct tagBackTrace { BackTraceItem* curr; BackTraceItem* head; = } ; struct tagBackTraceItem{ char symName[4096]; char fileName[4096 +1]; int lineNumber; BackTraceItem* next; } ; typedef tagBackTraceItem BackTraceItem; typedef tagBackTrace BackTrace; BackTrace* FillStruct() ; } void main () { BackTrace* bt =3D FillStruct(); if ( bt =3D=3D null ) printf("NULLIFIED\n"); while ( bt.curr ) { printf("MAIN : %s\n",bt.curr.symName ); bt.curr =3D bt.curr.next; } } ------------I5RmwB2wnVLjrRvcJwwVfm--
Apr 13 2004
C wrote:Oops this should be the bar.d
C and I figured it out. Simply replace %s with %.*s. D seems to be converting the c array to a d array when its passed into printf. -- -Anderson: http://badmama.com.au/~anderson/
Apr 13 2004
I also noticed that main() checks bt == null, but then assumes that it's not null. If you think that bt might be null, then you MUST NOT check bt.curr, since any attempt to read the value of this will result in null pointer dereferenece. Return from main() when you detect null, or put the while() inside an else... of the if statement, or just include a bt != null test in the while.
Apr 13 2004
Yes your , it was simplied for the example
I was also having trouble because in my D import file i used
struct tagBackTraceItem
{
char fileName[MAX_PATH+1];
char symName[4096];
int lineNumber;
BackTraceItem * next;
};
And in my actual header file I was using
typedef struct tagBackTraceItem{
char symName[4096];
char fileName[MAX_PATH +1];
int lineNumber;
struct tagBackTraceItem* next;
} BackTraceItem;
The order of the fields are not the same, I was getting the symName valu=
e =
stored in the D fileName variable.
Thanks,
Charlie
On Tue, 13 Apr 2004 23:03:21 -0700, Russ Lewis =
<spamhole-2001-07-16 deming-os.org> wrote:
I also noticed that main() checks bt =3D=3D null, but then assumes tha=
not null.
If you think that bt might be null, then you MUST NOT check bt.curr, =
since any attempt to read the value of this will result in null pointe=
dereferenece. Return from main() when you detect null, or put the =
while() inside an else... of the if statement, or just include a bt !=3D=
null test in the while.
-- =
D Newsgroup.
Apr 14 2004









J Anderson <REMOVEanderson badmama.com.au> 