简体   繁体   English

当双指针分配给单指针时,memory 布局看起来如何?

[英]How does the memory layout look like when a double pointer is assigned to a single pointer?

I am currently working on a legacy code (still proprietary, so we'll have to make do with an MRE instead; and by legacy I mean code written in 1991).我目前正在处理遗留代码(仍然是专有的,所以我们必须使用 MRE 来代替;我所说的遗留代码是指 1991 年编写的代码)。 This is what I've come across:这就是我遇到的情况:

#include <iostream>

void foo(void*& ptr2) {
    
    // whatever
    
}

int main() {
    int** ptr = new int*[5];
    void* ptr2;
    int i = 1;
    for(int j = 0; j < 5; j++) {
        ptr[j] = new int;
        ptr[j][0] = i++;
    }
    ptr2 = ptr;
    foo(ptr2);

    // further operations here... and a comment which suggests the above was done to prevent accidental double dereferencing when ptr2 was passed by reference to another function
    return 0;
}

How does the logical layout of ptr2 look like in this case?在这种情况下, ptr2的逻辑布局如何? I am assuming that ptr[0][0] becomes ptr2[0] , ptr[1][0] becomes ptr2[1] and so on, but the print statements (not required in the MRE, just standard cout statements on various indices) prove my assumption wrong.我假设ptr[0][0]变成ptr2[0]ptr[1][0]变成ptr2[1]等等,但是打印语句(MRE 不需要,只是各种标准的 cout 语句指数)证明我的假设是错误的。 What am I missing here?我在这里错过了什么?

The question is pretty unclear but I will try to answer anyway:这个问题还不清楚,但我还是会尝试回答:

  • casting pointers does not alter any memory layout投射指针不会改变任何 memory 布局
  • the layout of a pointer is some contiguous bytes (usually 4 or 8) that hold a memory address.指针的布局是一些连续的字节(通常是 4 或 8),它们包含 memory 地址。 This should not be confused with the layout of memory that a pointer may be pointing to.这不应与指针可能指向的 memory 的布局相混淆。
  • the layout of the objects allocated by new int *[5] is 5 contiguous pointers each of type int * . new int *[5]分配的对象的布局是 5 个连续的指针,每个都是int *类型。 Mucking around with other pointers that point to this memory block does not change the layout of this memory block.与指向这个 memory 块的其他指针混在一起并不会改变这个 memory 块的布局。

Inside foo , the memory that was allocated by new int *[5] can be accessed by recovering its address with the correct type:foo内部,可以通过使用正确类型恢复其地址来访问由new int *[5]分配的 memory:

void foo(void*& ptr2)
{
     int **ptr = static_cast<int **>(ptr2);

     for(int j = 0; j < 5; j++)
         std::cout << ptr[j] << '\n';
}

Any other ways of attempting to access the memory block in question cause undefined behaviour.尝试访问有问题的 memory 块的任何其他方式都会导致未定义的行为。 (Well, except for aliasing it as unsigned char ). (好吧,除了别名为unsigned char )。

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

相关问题 花式指针是什么样的? - How does a fancy pointer look like? 当重载的运算符new通过一个额外的前缀使指针偏移时,有问题的情况类别看起来像 - When an overloaded operator new offsets a pointer by an additional prefix, how does the class of problematic cases look like 当指向动态分配的内存时,指针的指针将分配什么值? - What value does a pointer to pointer get assigned when points to a dynamically allocated memory? 当C ++被当作数组对待时,如何看待指向无符号字符的指针? - How does C++ look at a pointer to an unsigned char when it's treated like an array? 内存分配如何在指向 char 的双指针中工作 - How does memory assignment work in a double pointer to char 双指针内存分配 - double pointer memory allocation 如何查看指针映射中的指针是否不为0 - How to look if a pointer in a pointer map is not 0 为什么指针占用未分配的内存 - Why is pointer taking memory that is not assigned to it 如何在C ++中将双指针矩阵转换为单指针向量? - How to convert double pointer matrix to a single pointer vector in c++? boost::serialization 在通过指针反序列化时如何分配 memory? - How does boost::serialization allocate memory when deserializing through a pointer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM