备份3个判断指针是否有效的函数,以备不时之需

BOOLEAN MmIsAddressValid(
_In_  PVOID VirtualAddress
);

Parameters



VirtualAddress [in]

A pointer to the nonpaged virtual address to check. The caller must ensure
that this 


address cannot be paged out or deleted for the duration of this call. Even
after the return from the call, you must not page out or delete this address.
If you do page out or delete this address, the return value might be
unreliable. Paging out or deleting this address might cause the computer to
stop responding (that is, crash).

Return value


If no page fault would occur from reading or writing at the given virtual
address, MmIsAddressValid returns TRUE.

Remarks

Even
if MmIsAddressValid returns TRUE,
accessing the address can cause page faults unless the memory has been locked
down or the address is a valid nonpaged pool address.

—————————————————————————————–


R3:


—————————————————————————————–

BOOL WINAPI IsBadReadPtr(
_In_  const VOID *lp,
_In_  UINT_PTR ucb
);

Parameters



lp [in]

A pointer to the first byte of the memory block.

ucb [in]

The size of the memory block, in bytes. If this parameter is zero, the
return value is zero.

Return value

If the calling process has read access to all bytes in the specified memory
range, the return value is zero.

If the calling process does not have read access to all bytes in the
specified memory range, the return value is nonzero.

If the application is compiled as a debugging version, and the process does
not have read access to all bytes in the specified memory range, the function
causes an assertion and breaks into the debugger. Leaving the debugger, the
function continues as usual, and returns a nonzero value. This behavior is by
design, as a debugging aid.

Remarks

This function is typically used when working with pointers returned from
third-party libraries, where you cannot determine the memory management behavior
in the third-party DLL.

Threads in a process are expected to cooperate in such a way that one will
not free memory that the other needs. Use of this function does not negate the
need to do this. If this is not done, the application may fail in an
unpredictable manner.

Dereferencing potentially invalid pointers can disable stack expansion in
other threads. A thread exhausting its stack, when stack expansion has been
disabled, results in the immediate termination of the parent process, with no
pop-up error window or diagnostic information.

If the calling process has read access to some, but not all, of the bytes in
the specified memory range, the return value is nonzero.

In a preemptive multitasking environment, it is possible for some other
thread to change the process’s access to the memory being tested. Even when the
function indicates that the process has read access to the specified memory, you
should use structured exception handling when attempting to access the memory.
Use of structured exception handling enables the system to notify the process if
an access violation exception occurs, giving the process an opportunity to
handle the exception.

—————————————————————————————–

IsBadWritePtr function

1 out of 3 rated this helpful - Rate
this topic

Verifies that the calling process has write access to the specified range of
memory.

Important  This function is obsolete and should
not be used. Despite its name, it does not guarantee that the pointer is valid
or that the memory pointed to is safe to use. For more information, see Remarks
on this page.

Syntax

C++

BOOL WINAPI IsBadWritePtr(
_In_  LPVOID lp,
_In_  UINT_PTR ucb
);

Parameters



lp [in]

A pointer to the first byte of the memory block.

ucb [in]

The size of the memory block, in bytes. If this parameter is zero, the
return value is zero.

Return value

If the calling process has write access to all bytes in the specified memory
range, the return value is zero.

If the calling process does not have write access to all bytes in the
specified memory range, the return value is nonzero.

If the application is run under a debugger and the process does not have
write access to all bytes in the specified memory range, the function causes a
first chance STATUS_ACCESS_VIOLATION exception. The debugger can be configured
to break for this condition. After resuming process execution in the debugger,
the function continues as usual and returns a nonzero value This behavior is by
design and serves as a debugging aid.

Remarks

This function is typically used when working with pointers returned from
third-party libraries, where you cannot determine the memory management behavior
in the third-party DLL.

Threads in a process are expected to cooperate in such a way that one will
not free memory that the other needs. Use of this function does not negate the
need to do this. If this is not done, the application may fail in an
unpredictable manner.

Dereferencing potentially invalid pointers can disable stack expansion in
other threads. A thread exhausting its stack, when stack expansion has been
disabled, results in the immediate termination of the parent process, with no
pop-up error window or diagnostic information.

If the calling process has write access to some, but not all, of the bytes in
the specified memory range, the return value is nonzero.

In a preemptive multitasking environment, it is possible for some other
thread to change the process’s access to the memory being tested. Even when the
function indicates that the process has write access to the specified memory,
you should use structured exception handling when attempting to access the
memory. Use of structured exception handling enables the system to notify the
process if an access violation exception occurs, giving the process an
opportunity to handle the exception.

IsBadWritePtr is not multithread safe. To use it
properly on a pointer shared by multiple threads, call it inside a critical
region of code that allows only one thread to access the memory being checked.
Use operating system–level objects such as critical sections or mutexes or the
interlocked functions to create the critical region of code.

时间: 2024-10-12 21:45:53

备份3个判断指针是否有效的函数,以备不时之需的相关文章

判断指针是否为空

这两天一直迷惑一个问题,就是如何判断指针是否为空,通常有如下两种方式(假设p为指针): if (NULL == p) { // ... } if (!p) { } 到底哪种方式好呢?这个问题应该从两个方面去考虑,本文就是围绕这两个问题展开的,仅限于C语言. 它们在语义.编译等环节上是否等价? 它们在可读性.可维护性等上面是否等价? 一.是否存在隐式类型转换? <C 程序设计语言中>规定(中文第二版177页),对于运算符==和!=,可以作如下比较:指针可以和值为0的常量表达式或指向void的指针

给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该结点

#include <iostream> #include <string.h> #include <stdlib.h> #include <stack> using namespace std; struct Node { int data; struct Node* next; }; struct Node* create_list(int len) { if (len <= 0) return NULL; struct Node* head; st

C和指针 学习笔记-4.函数

参数传递: 参数传递采用按值传递 ADT&黑盒 ADT:abstract data type,抽象数据类型 c可以用于设计与实现抽象数据类型,因为它可以限制函数和数据定义的作用域,这种技巧也称为黑盒设计 user.h #define MAXLEN 3 struct UserClz { char *name; char *phone; char *address; }; typedef struct UserClz User; /* *接函数 *通地名称查找地址 */ char const * l

判断在类中某个函数(也可以是变量或类型)是否存在

一,判断在类中某个函数(也可以是变量或类型)是否存在 template<typename T> struct xxxx_detector { template<typename P,void (P::*)(void)> struct detector{}; template<typename P> static char func(detector<P,&P::init>*); template<typename P> static lo

c语言:利用指针变量,用函数实现将3个整数按从大到小的顺序输出

利用指针变量,用函数实现将3个整数按从大到小的顺序输出. 解:程序: #include<stdio.h> void swap(int *ptr1, int *ptr2) { int temp = *ptr1; *ptr1 = *ptr2; *ptr2 = temp; } void exchange(int *q1, int *q2,int *q3) { void swap(int *ptr1, int *ptr2); if (*q1 < *q2) { swap(q1, q2); } if

一个判断手机浏览器的PHP函数

function is_mobile(){     $regex_match="/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|";     $regex_match.="htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|&quo

201507010852_《Javascript权威指南(第六版)——判断值类型的type函数 、鸭式辩型》(P210-217)

一. 类的扩充 1. 只要扩充原型类,就能动态更新到实例.例如:xxx.prototype.do = function() {//...}; 2. 不建议采用污染JavaScript的方法,如:Object.prototype.methods = function() {//...};   采用<权威指南>9.8.1中的方法添加属性,如:Object.defineProperty(//...); 3. 二. 类和类型 1. isPrototypeOf(); 2. 三. constructor属

函数指针与类成员函数指针

1,函数指针函数指针,顾名思义就是函数的指针,而指针其实就是地址,那么函数指针就是存储函数的地址,可是实际大部分时间里,我们写程序时是根本不会去考虑函数地址在哪里.我们只需要知道函数原型和函数声明就可以.但是想象一下,我们总是以为函数就应该接收参数,那么函数本身是否可以作为函数的参数呢?我做了下面的一个实验 #include<iostream>#include<stdio.h>#include<cstring>using namespace std;typedef in

js如何判断一个对象是数组(函数)

js如何判断一个对象是数组(函数) 1.typeof操作符  示例: // 数值 typeof 37 === 'number'; // 字符串 typeof '' === 'string'; // 布尔值 typeof true === 'boolean'; // Symbols typeof Symbol() === 'symbol'; // Undefined typeof undefined === 'undefined'; // 对象 typeof {a: 1} === 'object'