www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - trouble with struct values

reply Lars Ivar Igesund <larsivar igesund.net> writes:
I have some code that is greatly minimized to the one below (which won't 
compile or run, btw). The trouble is that the writefln call shows that 
wRepeatCount has a value greater than 0 at that point. When it comes to 
foo, it has been reset to 0. Do anyone have an idea what is happening? 
Is it a memory allocation issue? Scoping issue? Thread problem (In the 
real application, bar is part of the run function pointer)? I don't 
write to ip at no point in my application between getting the value and 
using it, still they change. I am aware that I'm not very experienced 
using structs, especially this type, so I find it likely that it is 
something I do and not a bug elsewhere. The app/library only depend on 
the Win32 API and Phobos. Suggestions is what I'm after here.

int bar()
{
   HANDLE hConIn;
   INPUT_RECORD ip;
   DWORD read;

   ReadConsoleInputW(hConIn, ip, 1, &read);
   writefln(ip.Event.KeyEvent.wRepeatCount);
   return foo(ip);
}

int foo(INPUT_RECORD ip)
{
   if (ip.EventType == KEY_EVENT) {
     return ip.Event.KeyEvent.wRepeatCount;
   }
   return 0;
}

Lars Ivar Igesund
Oct 10 2004
next sibling parent Sjoerd van Leent <svanleent wanadoo.nl> writes:
Lars Ivar Igesund wrote:
 
 int bar()
 {
   HANDLE hConIn;
   INPUT_RECORD ip;
   DWORD read;
 
   ReadConsoleInputW(hConIn, ip, 1, &read);
   writefln(ip.Event.KeyEvent.wRepeatCount);
   return foo(ip);
 }
 
 int foo(INPUT_RECORD ip)
 {
   if (ip.EventType == KEY_EVENT) {
     return ip.Event.KeyEvent.wRepeatCount;
   }
   return 0;
 }
 
 Lars Ivar Igesund
Got a clue. What kind of type is INPUT_RECORD? Is it a struct or a class, how is it build up? Also, should: writefln(ip.Event.KeyEvent.wRepeatCount); not be: writefln("%s", ip.Event.KeyEvent.wRepeatCount); Since it now just prints rubbish on the console. Regards, Sjoerd
Oct 10 2004
prev sibling parent reply Ben Hinkle <bhinkle4 juno.com> writes:
Lars Ivar Igesund wrote:

 I have some code that is greatly minimized to the one below (which won't
 compile or run, btw). The trouble is that the writefln call shows that
 wRepeatCount has a value greater than 0 at that point. When it comes to
 foo, it has been reset to 0. Do anyone have an idea what is happening?
 Is it a memory allocation issue? Scoping issue? Thread problem (In the
 real application, bar is part of the run function pointer)? I don't
 write to ip at no point in my application between getting the value and
 using it, still they change. I am aware that I'm not very experienced
 using structs, especially this type, so I find it likely that it is
 something I do and not a bug elsewhere. The app/library only depend on
 the Win32 API and Phobos. Suggestions is what I'm after here.
 
 int bar()
 {
    HANDLE hConIn;
    INPUT_RECORD ip;
    DWORD read;
 
    ReadConsoleInputW(hConIn, ip, 1, &read);
    writefln(ip.Event.KeyEvent.wRepeatCount);
    return foo(ip);
 }
 
 int foo(INPUT_RECORD ip)
 {
    if (ip.EventType == KEY_EVENT) {
      return ip.Event.KeyEvent.wRepeatCount;
    }
    return 0;
 }
 
 Lars Ivar Igesund
try declaring int foo(inout INPUT_RECORD ip) just to see if there is a problem passing structs to functions by value.
Oct 10 2004
parent Lars Ivar Igesund <larsivar igesund.net> writes:
Ben Hinkle wrote:

 Lars Ivar Igesund wrote:
 
 
I have some code that is greatly minimized to the one below (which won't
compile or run, btw). The trouble is that the writefln call shows that
wRepeatCount has a value greater than 0 at that point. When it comes to
foo, it has been reset to 0. Do anyone have an idea what is happening?
Is it a memory allocation issue? Scoping issue? Thread problem (In the
real application, bar is part of the run function pointer)? I don't
write to ip at no point in my application between getting the value and
using it, still they change. I am aware that I'm not very experienced
using structs, especially this type, so I find it likely that it is
something I do and not a bug elsewhere. The app/library only depend on
the Win32 API and Phobos. Suggestions is what I'm after here.

int bar()
{
   HANDLE hConIn;
   INPUT_RECORD ip;
   DWORD read;

   ReadConsoleInputW(hConIn, ip, 1, &read);
   writefln(ip.Event.KeyEvent.wRepeatCount);
   return foo(ip);
}

int foo(INPUT_RECORD ip)
{
   if (ip.EventType == KEY_EVENT) {
     return ip.Event.KeyEvent.wRepeatCount;
   }
   return 0;
}

Lars Ivar Igesund
try declaring int foo(inout INPUT_RECORD ip) just to see if there is a problem passing structs to functions by value.
That worked! I was thinking about trying that, but didn't as the original C code passed it by value. Lars Ivar Igesund
Oct 10 2004