了解指针、取消引用等
Understanding pointers, dereference etc
int _tmain(int argc, _TCHAR* argv[]){
int justInt = 10;
int* pointerToInt = &justInt;
int** pointerToPointer = &pointerToInt; //Why are 2 asteriks necessary? I thought at first that's because it would actually just point to 'justInt', but I checked and it points to 'pointerToInt' as expected.
void* voidPointerToInt = &justInt; //Can't be dereferenced unless we specify type because it's void.
cout << *pointerToInt << "n";
cout << *(int*)voidPointerToInt;// How do you put into English "*(int*)"? Not sure what it effectively does step-by-step except that it takes 4 bytes from the start of address and puts them into an int, however that works.
/* //I took a look into the disassembly, don't really know it very well though.
mov eax,dword ptr [voidPointerToInt] ;Eax stores the memlocation of voidPointerToInt as I watched the registers change. But I thought that the [] would dereference it? EDIT: I forgot that all variables in masm are pointers until you dereference them. So, if the [] weren't there then we would move a pointer to 'voidPointerToInt' into eax instead of its value.
mov ecx,dword ptr [eax] ;Ecx becomes 10, as it dereferences the 4 bytes stored at the begining of memlocation that eax stores.
;Not sure what the rest is for.
push ecx
mov ecx,dword ptr ds:[0EF10A4h]
call dword ptr ds:[0EF1090h]
cmp esi,esp
call __RTC_CheckEsp (0EE1343h)
*/
cin.ignore();
}
pointerToInt 存储 justInt 的内存位置。'int' 表示指针指向一个整数,因此我们可以取消引用它。
为什么"int pointerToInt = &justInt;"无效?如果我想将 mem 位置存储在普通整数中怎么办。它是 4 位上的 32 字节,那么有什么问题?取消引用普通 int 也是如此。
int* 和存储内存地址的普通 int 之间会有区别吗?
pointerToPointer
指向的东西有类型 int *
。 指向X
的类型是X*
,所以指向int *
的类型是int **
。
cout << *(int*)voidPointerToInt;
与cout << *pointerToInt
具有相同的效果。 void *
是通用指针类型;你可以认为它有一个非标记的变体,如果这会有所帮助. 它可用于通过通用接口传输不同类型的指针。
在代码中,将pointerToInt
转换为void *
,然后int *
返回到其原始类型,恢复原始值。
使用代码:可以说,如果X
的类型为 int *
,则(int *)((void *)X) == X
。
相关文章:
- C++取消引用指针.为什么会发生变化
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- 取消引用运算符不能重载
- 在他自己的方法中,有可能将一个对象取消引用到另一个对象吗
- C++ 关于指针取消引用的技术问题
- 没有取消引用/解包对象的标准方法?
- 列表 iter 不取消引用 使用列表进行插入排序
- 在这个函数中是有缺陷的,因为取消引用 null 是无效的,所以我想更改代码
- 取消引用结束指针到数组类型的一个
- 取消引用向量时出现问题
- 取消引用指向整数的指针时获得不同的结果
- C++:取消引用十六进制值,有点语法问题
- C/C++ 取消引用错误:在空检查之前取消引用
- 取消引用后C++空测试
- 立即取消引用unique_ptr
- 如何在不使用临时变量的情况下取消引用返回指针的函数的返回值?
- 取消引用 void 指针以将值分配给结构
- boost_multi_index迭代器取消引用给出常量
- 为什么"std::uninitialized_copy"通常取消对未初始化内存的迭代器的引用不是未定
- 矢量迭代器不能与 std::shared_ptr<> 取消引用