## D - Stupid question

• Karl Bochert (29/29) Nov 30 2002 Pardon my stupidity, but
• Walter (13/42) Nov 30 2002 The following works:
• Karl Bochert (8/23) Nov 30 2002 Not for me.
• Karl Bochert (72/87) Nov 30 2002 In case it's relevant here's a few details.
• Walter (5/92) Nov 30 2002 Please post a complete program that fails, that makes it a lot easier fo...
• Karl Bochert (62/65) Nov 30 2002 I shrank the program to its minimum and, !remarkably!, the problem did N...
• Karl Bochert (63/66) Nov 30 2002 Just read my own post.
• Walter (6/71) Nov 30 2002 Thanks, I'll check it out. -Walter
• Karl Bochert (59/61) Dec 01 2002 One of my earlier posts (# 9635 -- 113 lines) -showed 8 different ways
• Walter (8/73) Nov 30 2002 The first thing I notice is the gc isn't being started up and shutdown i...
• anderson (4/98) Nov 30 2002 How come the gc can't be automaticly stated up in WinMain by the D compi...
• Walter (6/107) Dec 01 2002 Because I haven't written the code to do that .
• Sean L. Palmer (7/14) Dec 01 2002 Why are you using WinMain anyway? There's nothing there that can't be
• Walter (9/41) Dec 01 2002 Will fail because you're pointing into the stack of a returned function.
Karl Bochert <kbochert copper.net> writes:
```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
```
Nov 30 2002
"Walter" <walter digitalmars.com> writes:
```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

```
Nov 30 2002
Karl Bochert <kbochert copper.net> writes:
```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
```
Nov 30 2002
Karl Bochert <kbochert copper.net> writes:
```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.

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
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
```
Nov 30 2002
"Walter" <walter digitalmars.com> writes:
```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.

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
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

```
Nov 30 2002
Karl Bochert <kbochert copper.net> writes:
```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\$",
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;
}
```
Nov 30 2002
Karl Bochert <kbochert copper.net> writes:
```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.

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\$",
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;
}
```
Nov 30 2002
"Walter" <walter digitalmars.com> writes:
```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.

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\$",
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;
}

```
Nov 30 2002
Karl Bochert <kbochert copper.net> writes:
```On Sat, 30 Nov 2002 20:47:27 -0800, "Walter" <walter digitalmars.com> wrote:
Thanks, I'll check it out. -Walter

One of my earlier posts (# 9635  -- 113 lines) -showed  8 different ways
of returning the char[]. Can you tell me which are supposed to work?
A large portion of my problem is that I can't tell a bug from
my mis-understanding.

Here are a few further simplifications of the 'foo()' function
in my previous example

//calling this is ok
void foo ()
{
char[80] buf;
}
..
foo ()
//

//calling this fails
void foo ()
{
char[80] buf = "abcd";    // init a [N]
}

//OK
char[] x = "1234567890";
void foo ()
{
char[80] buf = "abcd";
uint i;
for (i = 0; i < 4; ++i) {
buf[i] = 'x';
}
x.length = 4;
for (i = 0; i < 4; ++i) {
x[i] = buf[i];
}
}

//fails
char[] x;
void foo ()
{
char[80] buf;
uint i;
for (i = 0; i < 4; ++i) {
buf[i] = 'x';
}
x.length = 4;      // enlarge a []
for (i = 0; i < 4; ++i) {
x[i] = buf[i];
}
}

//fails
char[] x = "1234567890";
void foo ()
{
char[80] buf = "abcd";
uint i;
for (i = 0; i < 4; ++i) {
buf[i] = 'x';
}
x = buf.dup;  // use dup
}
```
Dec 01 2002
"Walter" <walter digitalmars.com> writes:
```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.

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\$",
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;
}

```
Nov 30 2002
"anderson" <anderson firestar.com.au> writes:
```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.

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\$",
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;
}

```
Nov 30 2002
"Walter" <walter digitalmars.com> writes:
```Because I haven't written the code to do that <g>.

"anderson" <anderson firestar.com.au> wrote in message
news:asca6p\$dn0\$1 digitaldaemon.com...
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.

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\$",
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;
}

```
Dec 01 2002
"Sean L. Palmer" <seanpalmer directvinternet.com> writes:
```Why are you using WinMain anyway?  There's nothing there that can't be
gotten later by other means.  (GetModuleHandle etc.) Just use main().

Sean

"anderson" <anderson firestar.com.au> wrote in message
news:asca6p\$dn0\$1 digitaldaemon.com...
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."

```
Dec 01 2002
"Walter" <walter digitalmars.com> writes:
```"Karl Bochert" <kbochert copper.net> wrote in message
news:1104_1038693845 bose...
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;

Will fail because you're pointing into the stack of a returned function.

//2
return buf[0..3];

Will fail because you're pointing into the stack of a returned function.

//3
x = buf;
x.length = 4;
return x;

Will fail because you're pointing into the stack of a returned function.

//4
x = "A te"
return x;

Will work - pointing into static data.

//5
x = buf;
return x[1..4];

Will fail because you're pointing into the stack of a returned function.

//6
return "A te";

Will work, pointing into static data.

//7
x.length = 4;
buf[4] = 0;
strcpy (x, buf);
return x;
}

Will work, x points into heap.
```
Dec 01 2002