views:

70

answers:

2

I have the following code and when I run it, passing 1000 bytes to the parameter in the function, the structure MEMORY_BASIC_INFORMATION has none of its variables used, they all stay the value 0. I wondered if that is supposed to be?

public unsafe static bool CheckForSufficientStack(long bytes)
{
  MEMORY_BASIC_INFORMATION stackInfo = new MEMORY_BASIC_INFORMATION();

  IntPtr currentAddr = new IntPtr((uint)&stackInfo - 4096);

  VirtualQuery(currentAddr, ref stackInfo, sizeof(MEMORY_BASIC_INFORMATION));

  return ((uint)currentAddr.ToInt64() - stackInfo.AllocationBase) > (bytes + STACK_RESERVED_SPACE);
}

private const long STACK_RESERVED_SPACE = 4096 * 16;

[DllImport("kernel32.dll")]
private static extern int VirtualQuery(
  IntPtr lpAddress,
  ref MEMORY_BASIC_INFORMATION lpBuffer,
  int dwLength);

   private struct MEMORY_BASIC_INFORMATION
   {
     internal uint BaseAddress;
     internal uint AllocationBase;
     internal uint AllocationProtect;
     internal uint RegionSize;
     internal uint State;
     internal uint Protect;
     internal uint Type;
    }

I'm running a Vista Enterprise X64 on a Core Duo 2.0Ghz.

+1  A: 

Well, using uint to talk about an address on X64 could be a problem. And why the -4096?

I'd have thought just:

IntPtr currentAddr = new IntPtr(&stackInfo);
Marc Gravell
Wouldn't a `uint` be 64 bits as well on an X64?
casablanca
casablanca: No; uint is always a wrapper for the `System.UInt32` class.
Warren
@Warren - specifically it is always an *alias* for `System.UInt32` (not a wrapper)
Marc Gravell
Hi (sorry for offtopic here; didn't know how to reach you), you closed a question here http://stackoverflow.com/questions/3170940/toc-and-figure-side-by-side-in-a-slide-closed despite community consensus that LaTeX questions are welcome on Stack Overflow. Can you reopen it? [And if really hate TeX so much, maybe you can get TeX users to "commit" here: http://area51.stackexchange.com/proposals/2148/tex-latex-and-friends?referrer=LtGW8MKLMIWU41zfK7oIpw2 so that there's a separate website.]
ShreevatsaR
BTW, if you don't believe me, see the meta thread on LaTeX questions: http://meta.stackoverflow.com/questions/12918/can-we-have-a-ruling-on-latex-on-stackoverflowThe consensus seems pretty unanimous to me. Aren't moderators supposed to use their powers wisely and only when necessary or the situation is obvious? :-)
ShreevatsaR
@ShreevatsaR - my e-mail is on my public profile. It has been re-opened, but I don't appreciate the tone you've used here or on the LaTeX question; "ignorant moderators", "if really hate TeX so much", etc. It was closed after being flagged multiple times as not being programming related. Which (and I hate to upset you here...) **is correct** ; the question discussed is not in the least bit programming related. At all. Not even a little. Nix.
Marc Gravell
I apologize if I offended you. I assumed you closed because you were not aware that LaTeX questions are appropriate on StackOverflow. Perhaps you were. (If not, please reread http://meta.stackoverflow.com/questions/12918 .) It is hard to determine *a priori* whether it needs programming or not. In this case, I have no doubt that it *is* more programming-related than *most* HTML and CSS questions we get here — finding the right commands to achieve desired output. Anyway, if multiple people flagged it and it showed only your name, apologies again... this seems a failure of the interface.
ShreevatsaR
@ShreevatsaR - it was flagged (multiple times) via the "requires moderator attention" function, not as close votes.
Marc Gravell
Oh I see... Still, I don't think closing harmless questions, especially ones on topics explicitly considered appropriate, requires moderator intervention. Maybe you can go through some CSS questions and examine how they are more programming-related: http://stackoverflow.com/questions/tagged/css . Or better yet, please answer at the thread on meta.stackoverflow, so that more people can understand your perspective, if any. Regards,
ShreevatsaR
Thanks for reopening it, BTW. I also just realised now that I could have just flagged it for moderator attention myself, and any reasonable moderator (including possibly yourself) would have reopened it, so I didn't have to bother you specifically. But please do go comment at http://meta.stackoverflow.com/questions/12918/can-we-have-a-ruling-on-latex-on-stackoverflow so that we can actually understand or come to a consensus on how it's not programming related. Regards,
ShreevatsaR
Not to hound you (this will be my final comment here), but it would really help if you answered at http://meta.stackoverflow.com/questions/12918/can-we-have-a-ruling-on-latex-on-stackoverflow — many people have expressed mystification at why LaTeX questions are closed, and I'm sure an explanation from a moderator like you will be quite valued, especially since you assure (in reaction to my unfair comments! sorry again) that your closing was well-considered and not from being ignorant of, or "hating", (La)TeX.
ShreevatsaR
+1  A: 

Yes, this code cannot work on a 64-bit operating system. The casts are wrong, so is the declaration of the MEMORY_BASIC_INFORMATION. This ought to be closer, untested since I'm not close to an x64 machine right now:

    public unsafe static bool CheckForSufficientStack(long bytes) {
        var stackInfo = new MEMORY_BASIC_INFORMATION();
        IntPtr currentAddr = new IntPtr((long)&stackInfo - 4096);
        VirtualQuery(currentAddr, ref stackInfo, sizeof(MEMORY_BASIC_INFORMATION));
        return (currentAddr.ToInt64() - (long)stackInfo.AllocationBase) > (bytes + STACK_RESERVED_SPACE);
    }

    private struct MEMORY_BASIC_INFORMATION {
        internal IntPtr BaseAddress;
        internal IntPtr AllocationBase;
        internal uint AllocationProtect;
        internal IntPtr RegionSize;
        internal uint State;
        internal uint Protect;
        internal uint Type;
    }
Hans Passant
It hasn't changed, still are struct vars are 0...
Tony
One more problem: the dwLength argument declaration is wrong too, it is IntPtr.
Hans Passant