Thread overview | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 30, 2002 Stupid question | ||||
---|---|---|---|---|
| ||||
Pardon my stupidity, but char[] foo() { char[] x; char[128] buf= "qwerty" // How do I return "querty" ?!? } I have tried scores of things over the last 8 hours without success. The best I have come up with is: char[] foo() { char[128] buf = "qwerty" char[] x ="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; // enuf a's for max size of buf! x.length = 6; for (i = 0; i < 6; ++i) { x[i] = buf [i]; } return x; } All attempts at using x[] = or buf.dup or new char[128] have failed. What am I missing. Karl Bochert |
November 30, 2002 Re: Stupid question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Karl Bochert | The following works:
char[] foo()
{
return "querty";
}
as does:
char[] foo()
{
char[128] buf= "qwerty";
return buf.dup;
}
"Karl Bochert" <kbochert@copper.net> wrote in message news:1103_1038645905@bose...
> Pardon my stupidity, but
>
> char[] foo()
> {
> char[] x;
> char[128] buf= "qwerty"
>
> // How do I return "querty" ?!?
>
> }
>
> I have tried scores of things over the last 8 hours without success.
>
> The best I have come up with is:
> char[] foo()
> {
> char[128] buf = "qwerty"
> char[] x ="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
> // enuf a's for max size of buf!
>
> x.length = 6;
> for (i = 0; i < 6; ++i) {
> x[i] = buf [i];
> }
> return x;
> }
>
> All attempts at using
> x[] =
> or
> buf.dup
> or
> new char[128]
> have failed.
>
> What am I missing.
>
> Karl Bochert
>
>
|
November 30, 2002 Re: Stupid question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | On Sat, 30 Nov 2002 10:51:07 -0800, "Walter" <walter@digitalmars.com> wrote:
> The following works:
>
> char[] foo()
> {
> return "querty";
> }
>
> as does:
>
> char[] foo()
> {
> char[128] buf= "qwerty";
> return buf.dup;
> }
>
Not for me.
I get an 'invalid page fault' (Win98) when the program returns.
With some strings and techniques, I get som garbage data back.
After many, many attempts, it appears that nothing will convince the dynamic array char[] in foo() to get larger.
??
Karl Bochert
|
November 30, 2002 Re: Stupid question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | On Sat, 30 Nov 2002 10:51:07 -0800, "Walter" <walter@digitalmars.com> wrote: > The following works: > > char[] foo() > { > return "querty"; > } > > as does: > > char[] foo() > { > char[128] buf= "qwerty"; > return buf.dup; > } > In case it's relevant here's a few details. WIN98 platform, DMC v8.31 and DMD downloaded 2 days ago. The 'application' is a windows I/O module that uses stdio for file output and Windows API for console I/O. I had a working version in C and translated it to D. (quite easy). The shell script: DMD=C:\d\dmd\bin\dmd.exe LIB=C:\d\dmd\lib;C:\d\dm\lib #set LINKCMD=%DMD\dm\bin\link DFLAGS= $(DMD) -c winio $(DFLAGS) $(DMD) winio.obj snn.lib -L/map win.def WIN.DEF contains: EXETYPE NT SUBSYSTEM WINDOWS The relevant code looks something like: char[] foo() // With an assortment of things I have tried { char[] x; char[128] buf = "qwerty"; // modify buf ok? strcpy (buf, "A test"); // should all of these return the same thing? // -- they all fail -- //1 x = buf[0..3]; return x; //2 return buf[0..3]; //3 x = buf; x.length = 4; return x; //4 x = "A te" return x; //5 x = buf; return x[1..4]; //6 return "A te"; //7 x.length = 4; buf[4] = 0; strcpy (x, buf); return x; } extern (Windows) int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow) { int argc; char **argv; char[] line; OS_init (); // check for redirection -- setup STDIN, etc. // show_console (); Fputs (STDERR, "Hello from stderr!\n"); line = foo(); // while(1){}; Fprintf (STDERR, "%.*s!\n", "test"); Fprintf (STDERR, "XX%.*s", line); Fprintf (STDERR, "STDIN= %d %d\n", STDIN, stdin); Fprintf (STDERR, "STDOUT= %d %d\n", STDOUT, stdout); Fprintf (STDERR, "STDERR= %d %d\n", STDERR, stderr); Fprintf (STDERR, "STDKEY= %d\n", STDKEY); Fprintf (STDERR, "Line = #%.*s#\n", test); ... Without the call to foo() it runs fine. With foo(), it causes a memory protection fault, even with the infinite loop. Karl Bochert |
November 30, 2002 Re: Stupid question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Karl Bochert | Please post a complete program that fails, that makes it a lot easier for us. "Karl Bochert" <kbochert@copper.net> wrote in message news:1104_1038693845@bose... > On Sat, 30 Nov 2002 10:51:07 -0800, "Walter" <walter@digitalmars.com> wrote: > > The following works: > > > > char[] foo() > > { > > return "querty"; > > } > > > > as does: > > > > char[] foo() > > { > > char[128] buf= "qwerty"; > > return buf.dup; > > } > > > > In case it's relevant here's a few details. > > WIN98 platform, DMC v8.31 and DMD downloaded 2 days ago. > > The 'application' is a windows I/O module that uses stdio for file output and Windows API for console I/O. I had a working version in C and translated it to D. (quite easy). > > The shell script: > DMD=C:\d\dmd\bin\dmd.exe > LIB=C:\d\dmd\lib;C:\d\dm\lib > #set LINKCMD=%DMD\dm\bin\link > DFLAGS= > > $(DMD) -c winio $(DFLAGS) > $(DMD) winio.obj snn.lib -L/map win.def > > WIN.DEF contains: > EXETYPE NT > SUBSYSTEM WINDOWS > > > The relevant code looks something like: > > char[] foo() // With an assortment of things I have tried > { > char[] x; > char[128] buf = "qwerty"; > > // modify buf ok? > strcpy (buf, "A test"); > > // should all of these return the same thing? > // -- they all fail -- > //1 > x = buf[0..3]; > return x; > > //2 > return buf[0..3]; > > //3 > x = buf; > x.length = 4; > return x; > > //4 > x = "A te" > return x; > > //5 > x = buf; > return x[1..4]; > > //6 > return "A te"; > > //7 > x.length = 4; > buf[4] = 0; > strcpy (x, buf); > return x; > } > > extern (Windows) > int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, > LPSTR szCmdLine, int iCmdShow) > { > int argc; > char **argv; > char[] line; > > OS_init (); // check for redirection -- setup STDIN, etc. > // show_console (); > Fputs (STDERR, "Hello from stderr!\n"); > line = foo(); > // while(1){}; > Fprintf (STDERR, "%.*s!\n", "test"); > Fprintf (STDERR, "XX%.*s", line); > > Fprintf (STDERR, "STDIN= %d %d\n", STDIN, stdin); > Fprintf (STDERR, "STDOUT= %d %d\n", STDOUT, stdout); > Fprintf (STDERR, "STDERR= %d %d\n", STDERR, stderr); > Fprintf (STDERR, "STDKEY= %d\n", STDKEY); > Fprintf (STDERR, "Line = #%.*s#\n", test); > ... > > Without the call to foo() it runs fine. > With foo(), it causes a memory protection fault, even with the > infinite loop. > > > Karl Bochert > > > |
December 01, 2002 Re: Stupid question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | On Sat, 30 Nov 2002 15:23:09 -0800, "Walter" <walter@digitalmars.com> wrote:
> Please post a complete program that fails, that makes it a lot easier for us.
>
I shrank the program to its minimum and, !remarkably!, the problem did NOT go away.
Here it is. The windows part was tested in C.
import windows;
import c.stdio;
import string;
extern (Windows) {
int AllocConsole ();
};
HANDLE STDERR;
static void show_console()
{
AllocConsole();
// Must AllocConsole beore getting standard handles!!
STDERR = CreateFileA("CONOUT$",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_WRITE,
(SECURITY_ATTRIBUTES*) 0,// Security not inherited
0,
OPEN_EXISTING,
(HANDLE)0);
}
char[] foo ()
{
char[] x ;
char[6] buf = "abcd";
// uint i;
// x.length = 6;
// for (i = 0; i < 6; ++i) {
// x[i] = buf[i];
// }
x = buf.dup;
return x;
}
void Fputs(HANDLE fd, char[] out_string)
{
uint len;
len = out_string.length;
WriteFile(fd, // handle to file
out_string, // data buffer
len, // number of bytes to write
&len, // number of bytes written
(OVERLAPPED*) 0 // no overlapped buffer
);
}
//HINSTANCE winInstance;
extern (Windows)
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR szCmdLine, int iCmdShow)
{
int argc;
char **argv;
show_console();
char[] line;
Fputs (STDERR, "Hello");
line = foo();
// while(1) {};
Fputs (STDERR, "Hello");
while(1){}; // Hang the console open for reading
return 0;
}
|
December 01, 2002 Re: Stupid question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | On Sat, 30 Nov 2002 15:23:09 -0800, "Walter" <walter@digitalmars.com> wrote:
> Please post a complete program that fails, that makes it a lot easier for us.
>
Just read my own post.
Here's a version without the tabs.
import windows;
import c.stdio;
import string;
extern (Windows) {
int AllocConsole ();
};
HANDLE STDERR;
static void show_console()
/* set up a console window if not done yet */
{
AllocConsole();
// Must AllocConsole beore getting standard handles!!
STDERR = CreateFileA("CONOUT$",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_WRITE,
(SECURITY_ATTRIBUTES*) 0,// Security not inherited
0,
OPEN_EXISTING,
(HANDLE)0);
}
char[] foo ()
{
char[] x ;
char[6] buf = "abcd";
// uint i;
// x.length = 6;
// for (i = 0; i < 6; ++i) {
// x[i] = buf[i];
// }
x = buf.dup;
return x;
}
void Fputs(HANDLE fd, char[] out_string)
{
uint len;
len = out_string.length;
WriteFile(fd, // handle to file
out_string, // data buffer
len, // number of bytes to write
&len, // number of bytes written
(OVERLAPPED*) 0 // overlapped buffer
);
}
//HINSTANCE winInstance;
extern (Windows)
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR szCmdLine, int iCmdShow)
{
int argc;
char **argv;
show_console();
char[] line;
Fputs (STDERR, "Hello");
line = foo();
// while(1) {};
Fputs (STDERR, "Hello");
while(1){}; // Hang the console open
return 0;
}
|
December 01, 2002 Re: Stupid question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Karl Bochert | Thanks, I'll check it out. -Walter "Karl Bochert" <kbochert@copper.net> wrote in message news:1103_1038703271@bose... > On Sat, 30 Nov 2002 15:23:09 -0800, "Walter" <walter@digitalmars.com> wrote: > > Please post a complete program that fails, that makes it a lot easier for > > us. > > > > Just read my own post. > Here's a version without the tabs. > > import windows; > import c.stdio; > import string; > > extern (Windows) { > int AllocConsole (); > }; > > HANDLE STDERR; > > static void show_console() > /* set up a console window if not done yet */ > { > > AllocConsole(); > // Must AllocConsole beore getting standard handles!! > STDERR = CreateFileA("CONOUT$", > GENERIC_READ | GENERIC_WRITE, > FILE_SHARE_WRITE, > (SECURITY_ATTRIBUTES*) 0,// Security not inherited > 0, > OPEN_EXISTING, > (HANDLE)0); > } > > > char[] foo () > { > char[] x ; > char[6] buf = "abcd"; > // uint i; > > > // x.length = 6; > // for (i = 0; i < 6; ++i) { > // x[i] = buf[i]; > // } > > x = buf.dup; > > return x; > } > > void Fputs(HANDLE fd, char[] out_string) > { > uint len; > > len = out_string.length; > WriteFile(fd, // handle to file > out_string, // data buffer > len, // number of bytes to write > &len, // number of bytes written > (OVERLAPPED*) 0 // overlapped buffer > ); > } > > > //HINSTANCE winInstance; > extern (Windows) > int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, > LPSTR szCmdLine, int iCmdShow) > { > int argc; > char **argv; > show_console(); > char[] line; > > Fputs (STDERR, "Hello"); > line = foo(); > // while(1) {}; > Fputs (STDERR, "Hello"); > > while(1){}; // Hang the console open > return 0; > } > > > |
December 01, 2002 Re: Stupid question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Karl Bochert | The first thing I notice is the gc isn't being started up and shutdown in WinMain(). Check out the example in www.digitalmars.com/index.html under "D for Win32" and "Windows Executables." "Karl Bochert" <kbochert@copper.net> wrote in message news:1103_1038703271@bose... > On Sat, 30 Nov 2002 15:23:09 -0800, "Walter" <walter@digitalmars.com> wrote: > > Please post a complete program that fails, that makes it a lot easier for > > us. > > > > Just read my own post. > Here's a version without the tabs. > > import windows; > import c.stdio; > import string; > > extern (Windows) { > int AllocConsole (); > }; > > HANDLE STDERR; > > static void show_console() > /* set up a console window if not done yet */ > { > > AllocConsole(); > // Must AllocConsole beore getting standard handles!! > STDERR = CreateFileA("CONOUT$", > GENERIC_READ | GENERIC_WRITE, > FILE_SHARE_WRITE, > (SECURITY_ATTRIBUTES*) 0,// Security not inherited > 0, > OPEN_EXISTING, > (HANDLE)0); > } > > > char[] foo () > { > char[] x ; > char[6] buf = "abcd"; > // uint i; > > > // x.length = 6; > // for (i = 0; i < 6; ++i) { > // x[i] = buf[i]; > // } > > x = buf.dup; > > return x; > } > > void Fputs(HANDLE fd, char[] out_string) > { > uint len; > > len = out_string.length; > WriteFile(fd, // handle to file > out_string, // data buffer > len, // number of bytes to write > &len, // number of bytes written > (OVERLAPPED*) 0 // overlapped buffer > ); > } > > > //HINSTANCE winInstance; > extern (Windows) > int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, > LPSTR szCmdLine, int iCmdShow) > { > int argc; > char **argv; > show_console(); > char[] line; > > Fputs (STDERR, "Hello"); > line = foo(); > // while(1) {}; > Fputs (STDERR, "Hello"); > > while(1){}; // Hang the console open > return 0; > } > > > |
December 01, 2002 Re: Stupid question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | How come the gc can't be automaticly stated up in WinMain by the D compiler? "Walter" <walter@digitalmars.com> wrote in message news:asc945$bsk$2@digitaldaemon.com... > The first thing I notice is the gc isn't being started up and shutdown in WinMain(). Check out the example in www.digitalmars.com/index.html under "D > for Win32" and "Windows Executables." > > "Karl Bochert" <kbochert@copper.net> wrote in message news:1103_1038703271@bose... > > On Sat, 30 Nov 2002 15:23:09 -0800, "Walter" <walter@digitalmars.com> > wrote: > > > Please post a complete program that fails, that makes it a lot easier > for > > > us. > > > > > > > Just read my own post. > > Here's a version without the tabs. > > > > import windows; > > import c.stdio; > > import string; > > > > extern (Windows) { > > int AllocConsole (); > > }; > > > > HANDLE STDERR; > > > > static void show_console() > > /* set up a console window if not done yet */ > > { > > > > AllocConsole(); > > // Must AllocConsole beore getting standard handles!! > > STDERR = CreateFileA("CONOUT$", > > GENERIC_READ | GENERIC_WRITE, > > FILE_SHARE_WRITE, > > (SECURITY_ATTRIBUTES*) 0,// Security not > inherited > > 0, > > OPEN_EXISTING, > > (HANDLE)0); > > } > > > > > > char[] foo () > > { > > char[] x ; > > char[6] buf = "abcd"; > > // uint i; > > > > > > // x.length = 6; > > // for (i = 0; i < 6; ++i) { > > // x[i] = buf[i]; > > // } > > > > x = buf.dup; > > > > return x; > > } > > > > void Fputs(HANDLE fd, char[] out_string) > > { > > uint len; > > > > len = out_string.length; > > WriteFile(fd, // handle to file > > out_string, // data buffer > > len, // number of bytes to write > > &len, // number of bytes written > > (OVERLAPPED*) 0 // overlapped buffer > > ); > > } > > > > > > //HINSTANCE winInstance; > > extern (Windows) > > int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, > > LPSTR szCmdLine, int iCmdShow) > > { > > int argc; > > char **argv; > > show_console(); > > char[] line; > > > > Fputs (STDERR, "Hello"); > > line = foo(); > > // while(1) {}; > > Fputs (STDERR, "Hello"); > > > > while(1){}; // Hang the console open > > return 0; > > } > > > > > > > > |
Copyright © 1999-2021 by the D Language Foundation