【举例说明】测试C++中数组空间是否连续

/*
 * @Author: benjie
 * @Date: 2021-11-15 21:35:20
 * @LastEditTime: 2021-11-15 21:41:09
 * @LastEditors: benjie
 * @Description: 测试数组在内存空间是否连续
 * C++中二维数组在地址空间上也是连续的
 */
#include<iostream>
#include<vector>

using namespace std;

// 0x61fdd0
// 0x61fdd4
// 0x61fdd8
// 0x61fddc
// 0x61fde0
// 0x61fde4
void test_array() {
    int array[6] = {0,1,2,3,4,5};
    cout << &array[0] << endl;
    cout << &array[1] << endl;
    cout << &array[2] << endl;
    cout << &array[3] << endl;
    cout << &array[4] << endl;
    cout << &array[5] << endl;
}
// 0x61fdd0 0x61fdd4 0x61fdd8
// 0x61fddc 0x61fde0 0x61fde4
void test_two_array() {
    int array[2][3] = {
        {0,1,2},
        {3,4,5}
    };
    cout << &array[0][0] << " " << &array[0][1] << " " << &array[0][2] <<endl;
    cout << &array[1][0] << " " << &array[1][1] << " " << &array[1][2] <<endl;
}

int main() {
    test_array();
    test_two_array();
    return 0;
}
posted @ 2021-11-15 21:44  伊万夫斯基  阅读(232)  评论(0编辑  收藏  举报