|
Archives
D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger
C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows
digitalmars.empire
digitalmars.DMDScript
|
D - Stupid question
↑ ↓ ← → 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
↑ ↓ ← → "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
↑ ↓ ← → 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
↑ ↓ ← → 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.
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
↑ ↓ ← → "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>
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
↑ ↓ ← → 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$",
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;
}
↑ ↓ ← → 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.
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;
}
↑ ↓ ← → "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>
Please post a complete program that fails, that makes it a lot easier
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
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;
}
↑ ↓ ← → 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
}
↑ ↓ ← → "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>
Please post a complete program that fails, that makes it a lot easier
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
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;
}
↑ ↓ ← → "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
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>
Please post a complete program that fails, that makes it a lot easier
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
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;
}
↑ ↓ ← → "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
"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
WinMain(). Check out the example in www.digitalmars.com/index.html under
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>
Please post a complete program that fails, that makes it a lot
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
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;
}
↑ ↓ ← → "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
"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
WinMain(). Check out the example in www.digitalmars.com/index.html under
for Win32" and "Windows Executables."
↑ ↓ ← → "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.
|
|