C ++-检查指针是否指向有效内存(此处不能使用NULL检查) [英] C++ - Check if pointer is pointing to valid memory (Can't use NULL checks here)

查看:576
本文介绍了C ++-检查指针是否指向有效内存(此处不能使用NULL检查)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建脚本语言. 当我分配东西时,它分配东西并返回地址 然后我用它做任何事,然后将其删除.我无法控制其中的变量 例如在我的lang中创建结构(使用指针和布尔结构来检查指针是否指向有效数据)等,因为这会使我的lang在RAM中变慢和变大.

I am creating scripting language. When I allocate thing ,it's allocate the thing and returns the address and then I do whatever with it and then delete it. I can't control the variables in it like creating struct in my lang (Struct with pointer and bool to check if pointer is pointing to valid data) and etc because it'll make my lang slower and bigger in the RAM.

例如:(我的脚本语言很容易理解.我怀疑您不会理解这一点,但是无论如何我都会在其中添加一些注释)

For example: (My scripting language is easily to understood. I doubt you'll not understand this ,but I'll put some comments in it anyway)

MyStruct = { //Function. For create object with it use 'new' before it.
    TestAliveVar=0
}
Func = { //I'll explain what exactly this function does every place it runs.
    if (!exists(arg0)) //C++: ???
        exit;
    arg0.TestAliveVar=1
    println "Still alive!";
}
var MyVar=new MyStruct(); //Returns address of the new object in the heap
                          //and runs on it the `MyStruct` function.
Func(MyVar);              //Sets his 'TestAliveVar' to 1
                          //and prints 'Still Alive!' with new line
delete(MyVar);            //C++: free(MyVar);
Func(MyVar);              //Does nothing

问题是如何创建在此代码中看到的函数exists. 顺便说一句,我可以在此lang中运行C ++代码.

The question is how to create the function exists you saw in this code. BTW I can run C++ codes in this lang.

推荐答案

此有效检查仅在Windows(VS)中进行了检查,功能如下:

This valid check checked in windows only (VS),here is the function:

#pragma once
//ptrvalid.h
__inline bool isValid(void* ptr) {
    if (((uint)ptr)&7==7)
        return false; //Not valid address at all (Maybe random pointer?)
    char _prefix;
    __try {
        _prefix=*(((char*)ptr)-1); //Get the prefix of this data
    } __except (true) { //Catch all unique exceptions (Windows exceptions) 
        return false; //Can't reach this memory
    }
    switch (_prefix) {
    case 0:    //Running release mode with debugger
    case -128: //Running release mode without debugger
    case -2:   //Running debug mode with debugger
    case -35:  //Running debug mode without debugger
        return false; //Deleted :(
        break;
    }
    return true; //Still alive!
}

用法:

#include <stdio.h>
#include "ptrvalid.h"

void PrintValid(void* ptr) {
    if (isValid(ptr))
        printf("%d is valid.\n",ptr);
    else
        printf("%d is not valid.\n",ptr);
}

int main() {
    int* my_array=(int*)malloc(4);
    PrintValid(my_array);
    PrintValid((void*)99);
    free(my_array);
    PrintValid(my_array);
    my_array=new int[4];
    PrintValid(my_array);
    delete my_array;
    PrintValid(my_array);
    getchar();
}

输出:

764776 is valid.
99 is not valid.
764776 is not valid.
774648 is valid.
774648 is not valid.

函数的解释:(它的作用)

Function's explanation: (What it does)

该功能在实际检查之前进行检查,如果地址有效\则指向内存. 此后,他检查此过程是否可以到达该内存的前缀(如果捕获到异常,则无法到达),最后检查是检查该内存的前缀(如果以任何方式删除). (调试\无调试模式\发布模式) 如果该函数通过所有这些检查,则返回true.

The functions check before the real checking ,if the address is valid\start point to memory. After that he checks if this process can reach this memory's prefix (If caught exception if can't) and the last checking is checking what the prefix of this memory if deleted at any mode. (Debugging\Without Debug Mode\Release Mode) If the function passed all of those checks ,it returns true.

这篇关于C ++-检查指针是否指向有效内存(此处不能使用NULL检查)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆