Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 27, 2004 C does not like New in D | ||||
---|---|---|---|---|
| ||||
Hello. I want to use D from C. But new does not work? :::::::::::::: slave.d :::::::::::::: extern (C) { int fhello() { ////////// deleting this it works class foo { int i = 37; } foo f = new foo; // segfault here delete f; ////////// end of deletion return 1957; } } int main(){return 0;} :::::::::::::: master.c :::::::::::::: #include <stdio.h> extern int fhello(void); int main() { int a = fhello(); printf("Fhello palautti %d.\n", a); return 0; } I do like this: [tmp]$ gcc -c master.c [tmp]$ dmd -c slave.d [tmp]$ gcc -o master master.o slave.o -lphobos -lpthread -lm [tmp]$ ./master Segmentation fault |
January 27, 2004 Re: C does not like New in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alkaja | If you use D from C, the main() program will have to be in D. Otherwise, the D runtime library will not get initialized. "Alkaja" <Alkaja_member@pathlink.com> wrote in message news:bv56mv$u7r$1@digitaldaemon.com... > Hello. > > I want to use D from C. But new does not work? > > :::::::::::::: > slave.d > :::::::::::::: > > extern (C) > { > > int fhello() > { > ////////// deleting this it works > class foo > { > int i = 37; > } > foo f = new foo; // segfault here > delete f; > ////////// end of deletion > > return 1957; > } > > } > > int main(){return 0;} > > :::::::::::::: > master.c > :::::::::::::: > #include <stdio.h> > > extern int fhello(void); > > int main() > { > int a = fhello(); > printf("Fhello palautti %d.\n", a); > return 0; > } > > I do like this: > > [tmp]$ gcc -c master.c > [tmp]$ dmd -c slave.d > [tmp]$ gcc -o master master.o slave.o -lphobos -lpthread -lm > > [tmp]$ ./master > Segmentation fault > > |
January 27, 2004 Re: C does not like New in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | In article <bv5f2c$1ba9$2@digitaldaemon.com>, Walter says... > >If you use D from C, the main() program will have to be in D. Otherwise, the D runtime library will not get initialized. THANK YOU! It works. I change in master.c main to mymain, and in slave.d main {return 0} to {mymain} and insert extern (C) mymain before main. (So I use master as slave and slave as master :-) But my real program is small Glade app, 1 button, 1 text field. I do same in there and it segfaults in malloc in C code. Maybe C library not get initialised now? (I tell Glade to make C, not C++) Glade makes main.c callbacks.c and my small program runs fine. In callbacks.c I want to use D. Is ok, but same problem with new. Glade is user interface builder for gtk+ and Gnome. >"Alkaja" <Alkaja_member@pathlink.com> wrote in message news:bv56mv$u7r$1@digitaldaemon.com... >> Hello. >> >> I want to use D from C. But new does not work? >> >> :::::::::::::: >> slave.d >> :::::::::::::: >> >> extern (C) >> { >> >> int fhello() >> { >> ////////// deleting this it works >> class foo >> { >> int i = 37; >> } >> foo f = new foo; // segfault here >> delete f; >> ////////// end of deletion >> >> return 1957; >> } >> >> } >> >> int main(){return 0;} >> >> :::::::::::::: >> master.c >> :::::::::::::: >> #include <stdio.h> >> >> extern int fhello(void); >> >> int main() >> { >> int a = fhello(); >> printf("Fhello palautti %d.\n", a); >> return 0; >> } >> >> I do like this: >> >> [tmp]$ gcc -c master.c >> [tmp]$ dmd -c slave.d >> [tmp]$ gcc -o master master.o slave.o -lphobos -lpthread -lm >> >> [tmp]$ ./master >> Segmentation fault >> >> > > |
January 27, 2004 Re: C does not like New in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alkaja | D's runtime will initialize the C runtime library, so it must be something else going wrong. I'm curious why you're using a main() in a windows app? Shouldn't it be winmain()? "Alkaja" <Alkaja_member@pathlink.com> wrote in message news:bv5ukn$24rq$1@digitaldaemon.com... > In article <bv5f2c$1ba9$2@digitaldaemon.com>, Walter says... > > > >If you use D from C, the main() program will have to be in D. Otherwise, the > >D runtime library will not get initialized. > > THANK YOU! > > It works. I change in master.c main to mymain, and > in slave.d main {return 0} to {mymain} and insert > extern (C) mymain before main. (So I use master as > slave and slave as master :-) > > But my real program is small Glade app, 1 button, 1 text field. I do same in there and it segfaults in malloc in C code. Maybe C library not get initialised now? (I tell Glade to make C, not C++) > > Glade makes main.c callbacks.c and my small program runs fine. In callbacks.c I want to use D. Is ok, but same problem with new. > > Glade is user interface builder for gtk+ and Gnome. > > >"Alkaja" <Alkaja_member@pathlink.com> wrote in message news:bv56mv$u7r$1@digitaldaemon.com... > >> Hello. > >> > >> I want to use D from C. But new does not work? > >> > >> :::::::::::::: > >> slave.d > >> :::::::::::::: > >> > >> extern (C) > >> { > >> > >> int fhello() > >> { > >> ////////// deleting this it works > >> class foo > >> { > >> int i = 37; > >> } > >> foo f = new foo; // segfault here > >> delete f; > >> ////////// end of deletion > >> > >> return 1957; > >> } > >> > >> } > >> > >> int main(){return 0;} > >> > >> :::::::::::::: > >> master.c > >> :::::::::::::: > >> #include <stdio.h> > >> > >> extern int fhello(void); > >> > >> int main() > >> { > >> int a = fhello(); > >> printf("Fhello palautti %d.\n", a); > >> return 0; > >> } > >> > >> I do like this: > >> > >> [tmp]$ gcc -c master.c > >> [tmp]$ dmd -c slave.d > >> [tmp]$ gcc -o master master.o slave.o -lphobos -lpthread -lm > >> > >> [tmp]$ ./master > >> Segmentation fault > >> > >> > > > > > > |
January 28, 2004 Re: C does not like New in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alkaja | Alkaja wrote:
> Hello.
>
> I want to use D from C. But new does not work?
>
> ::::::::::::::
> slave.d
> ::::::::::::::
>
> extern (C)
> {
>
> int fhello()
> {
> ////////// deleting this it works
> class foo
> {
> int i = 37;
> }
> foo f = new foo; // segfault here
> delete f; ////////// end of deletion
>
> return 1957;
> }
>
> }
>
> int main(){return 0;}
>
> ::::::::::::::
> master.c
> ::::::::::::::
> #include <stdio.h>
>
> extern int fhello(void);
>
> int main()
> {
> int a = fhello();
> printf("Fhello palautti %d.\n", a);
> return 0;
> }
>
> I do like this:
>
> [tmp]$ gcc -c master.c
> [tmp]$ dmd -c slave.d
> [tmp]$ gcc -o master master.o slave.o -lphobos -lpthread -lm
>
> [tmp]$ ./master
> Segmentation fault
>
>
In the documentation under "D for Win32"->"Windows Executables" (I know, bear with me :-D) there is an example of how to initialize the D garbage collector and whatnot. I would guess that your best bet would be to take that code (minus the windows specific stuff of course) and port it to C, then stick it in your master.c->main function.
|
January 28, 2004 Re: C does not like New in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | In article <bv6fri$qg$1@digitaldaemon.com>, Walter says... > >D's runtime will initialize the C runtime library, so it must be something >else going wrong. I'm curious why you're using a main() in a windows app? >Shouldn't it be winmain()? Sorry for not telling, I use Linux (Red Hat 9, with Glade. Glade is on same CD.) THANK YOU, I read D initialize C runtime, and I look more carefully. I think problem is GTK wants int main (int argc, char *argv[]) { ... gtk_init (&argc, &argv); ..} But I dont know what to write when D is main. Now I rename main to mymain in main.c, and main.d: int main(char[][] args) { char * a1 = "DIsBoss"; char ** a2 = &a1; mymain(1,a2); // i dont know how to pass argc and argv to C } and in main.c I rename int main to int mymain. This works PERFECTLY!! Now it is very easy to use Glade for interface and D for real program! But if I want to give command line arguments to GTK, I should give D args.length as first parameter (no problem) but what about char[][]args --> char*argv[] ?? >"Alkaja" <Alkaja_member@pathlink.com> wrote in message news:bv5ukn$24rq$1@digitaldaemon.com... >> In article <bv5f2c$1ba9$2@digitaldaemon.com>, Walter says... >> > >> >If you use D from C, the main() program will have to be in D. Otherwise, >the >> >D runtime library will not get initialized. >> >> THANK YOU! >> >> It works. I change in master.c main to mymain, and >> in slave.d main {return 0} to {mymain} and insert >> extern (C) mymain before main. (So I use master as >> slave and slave as master :-) >> >> But my real program is small Glade app, 1 button, 1 text field. I do same in there and it segfaults in malloc in C code. Maybe C library not get initialised now? (I tell Glade to make C, not C++) >> >> Glade makes main.c callbacks.c and my small program runs fine. In callbacks.c I want to use D. Is ok, but same problem with new. >> >> Glade is user interface builder for gtk+ and Gnome. >> >> >"Alkaja" <Alkaja_member@pathlink.com> wrote in message news:bv56mv$u7r$1@digitaldaemon.com... >> >> Hello. >> >> >> >> I want to use D from C. But new does not work? >> >> >> >> :::::::::::::: >> >> slave.d >> >> :::::::::::::: >> >> >> >> extern (C) >> >> { >> >> >> >> int fhello() >> >> { >> >> ////////// deleting this it works >> >> class foo >> >> { >> >> int i = 37; >> >> } >> >> foo f = new foo; // segfault here >> >> delete f; >> >> ////////// end of deletion >> >> >> >> return 1957; >> >> } >> >> >> >> } >> >> >> >> int main(){return 0;} >> >> >> >> :::::::::::::: >> >> master.c >> >> :::::::::::::: >> >> #include <stdio.h> >> >> >> >> extern int fhello(void); >> >> >> >> int main() >> >> { >> >> int a = fhello(); >> >> printf("Fhello palautti %d.\n", a); >> >> return 0; >> >> } >> >> >> >> I do like this: >> >> >> >> [tmp]$ gcc -c master.c >> >> [tmp]$ dmd -c slave.d >> >> [tmp]$ gcc -o master master.o slave.o -lphobos -lpthread -lm >> >> >> >> [tmp]$ ./master >> >> Segmentation fault >> >> >> >> >> > >> > >> >> > > |
January 28, 2004 Re: C does not like New in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jon Allen | In article <bv8c8b$5eq$1@digitaldaemon.com>, Jon Allen says... > >Alkaja wrote: >> Hello. >> >> I want to use D from C. But new does not work? > >In the documentation under "D for Win32"->"Windows Executables" (I know, bear with me :-D) there is an example of how to initialize the D garbage collector and whatnot. I would guess that your best bet would be to take that code (minus the windows specific stuff of course) and port it to C, then stick it in your master.c->main function. Thank you! I never looked. I use Linux. Now I see it is not hard in Windows to use GUI, very easy. Look my other post, it is also easy in Linux! |
January 29, 2004 Re: C does not like New in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alkaja | To use C programs as such by renaming C's main function and calling it from the D main function, is interesting. It opens a new way for reuse of old C code, effortless integration, and quick ways to resuscitate legacy code. I'm sorry, I have no elegant solution for passing the parameters to extern (C) [rett] foo(int argc, char* argv[]); In article <bv9fha$22te$1@digitaldaemon.com>, Alkaja says... > >In article <bv6fri$qg$1@digitaldaemon.com>, Walter says... >> >>D's runtime will initialize the C runtime library, so it must be something >>else going wrong. I'm curious why you're using a main() in a windows app? >>Shouldn't it be winmain()? .. >and in main.c I rename int main to int mymain. > >This works PERFECTLY!! Now it is very easy to use Glade >for interface and D for real program! > >But if I want to give command line arguments to GTK, >I should give D args.length as first parameter (no problem) >but what about char[][]args --> char*argv[] ?? |
February 01, 2004 Too Newbie to answer? (Was Re: C does not like New in D) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alkaja | Hmm. Either this was considered "too stupid a question", i.e. so obvious to all -- or too hard to answer?? This is my shot at an answer. In code like: <code> extern (C) int hardToChangeCFunction(int argc, char* argv[]); int main(char[][] args) { int foo = hardToChangeCFunction( /*How to pass the stuff?*/ ); return foo; } </code> Passing argc is simple. But the array args (or argv) is more difficult. In D, it is actually an array of string references (arry of char[]), whereas in C it is an array of pointers to char. You cannot just pass the address of args[0] to C because the string references occupy more space than mere pointers, since the string length is stored with the pointer. In C the array of char pointers is zero terminated, (there is a null pointer as the last item). This makes argc redundant, and it probably is mostly convenience sugar. Since we hope to use a (black box) C function, whose internals we do not have, we cannot rely on it using argc or the terminating null pointer alone, and we have to provide both. The strings pointed by the string references in args are D strings, i.e. they are not null terminated. Therefore: One has to create an array of [argc + 1] char pointers, and then create a null terminated string from each of the args strings. Then have the char pointers point at these, respectively, and assign null to the last pointer. The last thing to do is to pass the address of the first char pointer to the C function. This seems like a lot of work. Somehow I feel there is a much simpler solution. (For example, I wouldn't be surprised if C argc and argv both existed somewhere behind the scenes in D!) georg In article <bv9fha$22te$1@digitaldaemon.com>, Alkaja says... > >In article <bv6fri$qg$1@digitaldaemon.com>, Walter says... >> >>D's runtime will initialize the C runtime library, so it must be something >>else going wrong. I'm curious why you're using a main() in a windows app? >>Shouldn't it be winmain()? > >Sorry for not telling, I use Linux (Red Hat 9, with Glade. Glade is on same CD.) > >THANK YOU, I read D initialize C runtime, and I look more carefully. I think problem is GTK wants > >int main (int argc, char *argv[]) >{ ... >gtk_init (&argc, &argv); >..} > >But I dont know what to write when D is main. >Now I rename main to mymain in main.c, and > >main.d: >int main(char[][] args) >{ >char * a1 = "DIsBoss"; >char ** a2 = &a1; >mymain(1,a2); // i dont know how to pass argc and argv to C >} > >and in main.c I rename int main to int mymain. > >This works PERFECTLY!! Now it is very easy to use Glade >for interface and D for real program! > >But if I want to give command line arguments to GTK, >I should give D args.length as first parameter (no problem) >but what about char[][]args --> char*argv[] ?? > > >>"Alkaja" <Alkaja_member@pathlink.com> wrote in message news:bv5ukn$24rq$1@digitaldaemon.com... >>> In article <bv5f2c$1ba9$2@digitaldaemon.com>, Walter says... >>> > >>> >If you use D from C, the main() program will have to be in D. Otherwise, >>the >>> >D runtime library will not get initialized. >>> >>> THANK YOU! >>> >>> It works. I change in master.c main to mymain, and >>> in slave.d main {return 0} to {mymain} and insert >>> extern (C) mymain before main. (So I use master as >>> slave and slave as master :-) >>> >>> But my real program is small Glade app, 1 button, 1 text field. I do same in there and it segfaults in malloc in C code. Maybe C library not get initialised now? (I tell Glade to make C, not C++) >>> >>> Glade makes main.c callbacks.c and my small program runs fine. In callbacks.c I want to use D. Is ok, but same problem with new. >>> >>> Glade is user interface builder for gtk+ and Gnome. >>> >>> >"Alkaja" <Alkaja_member@pathlink.com> wrote in message news:bv56mv$u7r$1@digitaldaemon.com... >>> >> Hello. >>> >> >>> >> I want to use D from C. But new does not work? >>> >> >>> >> :::::::::::::: >>> >> slave.d >>> >> :::::::::::::: >>> >> >>> >> extern (C) >>> >> { >>> >> >>> >> int fhello() >>> >> { >>> >> ////////// deleting this it works >>> >> class foo >>> >> { >>> >> int i = 37; >>> >> } >>> >> foo f = new foo; // segfault here >>> >> delete f; >>> >> ////////// end of deletion >>> >> >>> >> return 1957; >>> >> } >>> >> >>> >> } >>> >> >>> >> int main(){return 0;} >>> >> >>> >> :::::::::::::: >>> >> master.c >>> >> :::::::::::::: >>> >> #include <stdio.h> >>> >> >>> >> extern int fhello(void); >>> >> >>> >> int main() >>> >> { >>> >> int a = fhello(); >>> >> printf("Fhello palautti %d.\n", a); >>> >> return 0; >>> >> } >>> >> >>> >> I do like this: >>> >> >>> >> [tmp]$ gcc -c master.c >>> >> [tmp]$ dmd -c slave.d >>> >> [tmp]$ gcc -o master master.o slave.o -lphobos -lpthread -lm >>> >> >>> >> [tmp]$ ./master >>> >> Segmentation fault >>> >> >>> >> >>> > >>> > >>> >>> >> >> > > |
Copyright © 1999-2021 by the D Language Foundation