简体   繁体   English

在C ++中从十六进制转换为无符号int *

[英]Cast from hexadecimal to unsigned int* in c++

I have an assignment that were supposed to evaluate some pointer manipulation expressions and memory leak situations in C/C++. 我有一个作业,应该对C / C ++中的一些指针操作表达式和内存泄漏情况进行评估。 There's one I'm stuck with: 我有一个坚持:

unsigned int* pInt = (unsigned int*) 0x403004;

Right off the bat this is suspicious to me, but in the assignment this line is theoretically working, however running the program I'm get segfault right at this line. 马上,这对我来说是可疑的,但是在分配中,这条线在理论上是可行的,但是在运行该程序时,我在这条线上遇到了段错误。

The question is: Is this right or even is possible or the professor is just fooling us telling this is right? 问题是:这是正确的,甚至是可能的,还是教授只是在愚弄我们说这是正确的? I've seen some examples and questions with string "hex" to int, but nothing regarding "pure hex" to int or int* 我已经看到了一些示例和带有int字符串“ hex”的问题,但没有任何关于将“ pure hex”字符串转换为int或int *的信息

unsigned int* pInt = (unsigned int*) 0x403004;

Two things are suspicious here: 这里有两点可疑:

  • Unless, you are writing some specialized Software like device drivers or OS, or you are in some embedded or special system where memory is fixed, seeing memory address hardcoded is certainly suspicious. 除非您正在编写某些专用软件(例如设备驱动程序或OS),或者您在某个固定内存的嵌入式系统或专用系统中,否则看到硬编码的内存地址肯定是可疑的。 Your program will (at best) fail if it tries to access memory it doesn't have the access rights to. 如果您的程序尝试访问没有访问权限的内存,则(最多)该程序将失败。

  • On the right hand side, the compiler first deduces the value 0x403004 as in int and will correctly convert it to a pointer. 在右侧,编译器首先将值0x403004int ,并将其正确转换为指针。 Thus, your Segfault is probably as a result of the first point. 因此,您的Segfault可能是第一点的结果。

unsigned int* pInt = (unsigned int*) 0x403004; 无符号整数* pInt =(无符号整数*)0x403004;

Possible?: yes (compiles, builds just fine) 可能吗?:是(编译,构建就可以了)

Is it right?: depends on what for. 是吗?:取决于目的。 Evidently it is useful for illustration in a classroom assignment. 显然,这对于课堂作业中的插图很有用。

Is it recommended? 推荐吗? no. 没有。 It will invoke undefined behavior . 它将调用未定义的行为 You are creating a variable that points to a location in memory that you may or may not have rights to. 您正在创建一个变量,该变量指向您在内存中可能有或没有权限的位置。 If you never use it, fine. 如果您从不使用它,那很好。 But if you do use it, the results are indeterminate. 但是,如果您确实使用它,结果将是不确定的。

it works fine only if that number represents an already allocated memory eg: 仅当该数字表示已分配的内存时,它才能正常工作,例如:

#include <iostream>

int main()
{
    int i = 7;
    std::cout << "i: " << i << std::endl; // 7
    std::cout << "&i: " << &i << std::endl; // in my case: 0018FF44
    unsigned int* ptr = (unsigned int*)0x0018FF44; // it's ok

    /*
    this is just for explaining because the address of i may differ at anytime the program
    launches thus 0018FF44 will no longer be assigned to i and consequently segfault.

    the right thing is to make pointer points always to whatever i may take as an address.
    to do so:
    */

    //int* ptr = (unsigned int*)&i; // not i but the address of i     

    (*ptr)++;

    std::cout << i << std::endl; // 8
    // above we changed i through pointer ptr

    int* pRandom = (int*)0xAB2FC0DE0; // this causes me segfault
    *pRandom = 5; // segfault

    std::cout << "*pRandom: " << *pRandom << std::endl; // segfault

    std::cout << std::endl;
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM