从内存地址获取值

Get value from memory address

本文关键字:获取 地址 内存      更新时间:2023-10-16

我有一个int*变量,用于存储内存地址的示例地址0x28c1150

我想知道,地址存储了什么值。

编辑:

struct list {
    int value;
    list *next;
    list *head = NULL;
    void push(int n);
    void select();
    void pop();
    void top();
};
void list::push(int value) {
    list *temp = new list;
    temp->value = value;
    temp->next = head;
    head = temp;
}
void list::top(){
    list * temp = new list;
    cout << head;
}

我想打印列表的顶部

如果您的变量为 list*

list* variable = new list;
variable->top();

...但是请注意,您当前的top()功能会泄漏内存,因为您每次被称为新列表,而您只是忘记了它。改用此方法:

int list::top(){  
    return head->value;
}
std::cout << variable->top() << "n";

您必须取消指针:

template<class T>
void print_value_at(T* pointer) {
    T& value = *pointer; //get value in pointer
    // print value 
    std::cout << value <<std::endl;
}

如果指针无效,则必须将其施放为最初的任何类型:

int x = 10;
// get void pointer to x
void* x_pointer_as_void = (void*)&x; 
// convert it back to a pointer to an int:
int* x_pointer = (int*)x_pointer_as_void;

这里(基于问题的当前版本)

cout << head->value;