Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9204829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:48:17+00:00 2026-06-17T23:48:17+00:00

Having read this interesting article outlining a technique for debugging heap corruption, I started

  • 0

Having read this interesting article outlining a technique for debugging heap corruption, I started wondering how I could tweak it for my own needs. The basic idea is to provide a custom malloc() for allocating whole pages of memory, then enabling some memory protection bits for those pages, so that the program crashes when they get written to, and the offending write instruction can be caught in the act. The sample code is C under Linux (mprotect() is used to enable the protection), and I’m curious as to how to apply this to native C++ and Windows. VirtualAlloc() and/or VirtualProtect() look promising, but I’m not sure how a use scenario would look like.

Fred *p = new Fred[100];
ProtectBuffer(p);
p[10] = Fred(); // like this to crash please

I am aware of the existence of specialized tools for debugging memory corruption in Windows, but I’m still curious if it would be possible to do it “manually” using this approach.

EDIT: Also, is this even a good idea under Windows, or just an entertaining intellectual excercise?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:48:18+00:00Added an answer on June 17, 2026 at 11:48 pm

    Yes, you can use VirtualAlloc and VirtualProtect to set up sections of memory that are protected from read/write operations.

    You would have to re-implement operator new and operator delete (and their [] relatives), such that your memory allocations are controlled by your code.

    And bear in mind that it would only be on a per-page basis, and you would be using (at least) three pages worth of virtual memory per allocation – not a huge problem on a 64-bit system, but may cause problems if you have many allocations in a 32-bit system.

    Roughly what you need to do (you should actually find the page-size for the build of Windows – I’m too lazy, so I’ll use 4096 and 4095 to represent pagesize and pagesize-1 – you also will need to do more error checking than this code does!!!):

    void *operator new(size_t size)
    {
        Round size up to size in pages + 2 pages extra.
        size_t bigsize = (size + 2*4096 + 4095) & ~4095; 
    
        // Make a reservation of "size" bytes. 
        void *addr = VirtualAlloc(NULL, bigsize, PAGE_NOACCESS, MEM_RESERVE);
    
        addr = reinterpret_cast<void *>(reinterpret_cast<char *>(addr) + 4096);
    
        void *new_addr = VirtualAlloc(addr, size, PAGE_READWRITE, MEM_COMMIT); 
    
        return new_addr;
    }
    
    void operator delete(void *ptr)
    {
        char *tmp = reinterpret_cast<char *>(ptr) - 4096;
    
        VirtualFree(reinterpret_cast<void*>(tmp)); 
    }
    

    Something along those lines, as I said – I haven’t tried compiling this code, as I only have a Windows VM, and I can’t be bothered to download a compiler and see if it actually compiles. [I know the principle works, as we did something similar where I worked a few years back].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having read this article F# Versus Mathematics: Part One - Getting Started with BLAS
After having read this interesting article about Ada and C++ and knowing of D's
I remember having read about it somewhere… Could anyone shed some light on this?
I was wondering, after having read this question ... he has this code: public
Having read this link on RBAR and this , my understanding of RBAR amounts
Having read this question Immutable or not immutable? and reading answers to my previous
After googling and landing on SO and having read this other question Is it
Having read around it appears this should be possible. I have the following content
I am fairly certain this is a ten penny closure question. But having read
I have a .txt file having some text. I want to read this file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.