DEBUG模式下, 内存中的变量地址分析
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DEBUG模式下, 内存中的变量地址分析相关的知识,希望对你有一定的参考价值。
测试函数的模板实现
- /// @file my_template.h
- /// @brief 测试数据类型用的模板实现
- #ifndef MY_TEMPLATE_H_2016_0123_1226
- #define MY_TEMPLATE_H_2016_0123_1226
- template<int iArySize>
- void fnTestDataType() {
- char szBuf[iArySize] = {‘\0‘};
- unsigned short wTmp = 0;
- int iTmp = 0;
- long lTmp = 0;
- float f = .0f;
- double dbl = .0;
- char szBuf1[iArySize] = {‘\0‘};
- unsigned short wTmp1 = 0;
- int iTmp1 = 0;
- long lTmp1 = 0;
- float f1 = .0f;
- double dbl1 = .0;
- printf("============================================================\n");
- printf("char array size = %d\n", iArySize);
- printf("------------------------------------------------------------\n");
- printf("&szBuf[0] = \t0x%p\n", &szBuf[0]);
- printf("&szBuf[%d] = \t0x%p\n", iArySize - 1, &szBuf[iArySize - 1]);
- printf("&wTmp = \t0x%p\n", &wTmp);
- printf("&iTmp = \t0x%p\n", &iTmp);
- printf("&lTmp = \t0x%p\n", &lTmp);
- printf("&f = \t\t0x%p\n", &f);
- printf("&dbl = \t\t0x%p\n", &dbl);
- printf("&szBuf1[0] = \t0x%p\n", &szBuf1[0]);
- printf("&szBuf1[%d] = \t0x%p\n", iArySize - 1, &szBuf1[iArySize - 1]);
- printf("&wTmp1 = \t0x%p\n", &wTmp1);
- printf("&iTmp1 = \t0x%p\n", &iTmp1);
- printf("&lTmp1 = \t0x%p\n", &lTmp1);
- printf("&f1 = \t\t0x%p\n", &f1);
- printf("&dbl1 = \t0x%p\n\n", &dbl1);
- };
- #endif // #ifndef MY_TEMPLATE_H_2016_0123_1226
测试代码和实验结论
- /// @file main.cpp
- /// @brief DEBUG模式下, 内存中的变量地址分析
- #include <iostream>
- using namespace std;
- #include "my_template.h"
- /** 试验结论 - <<DEBUG模式下, 内存中的变量地址分析>>
- 如果定义多个变量
- DEBUG模式下, 内存中的变量地址和定义的顺序相同
- 变量开始地址都是模式地址
- 先定义的变量在内存高地址
- 后定义的变量在内存低地址
- Release模式下, 经过优化, 变量的内存地址和变量定义的顺序不相同!
- 如果变量有越界访问的情况, Release模式下的越界访问情况未知.
- 好像有一些规律, 依赖于编译器的优化选项
- 依赖变量越界访问达成变量的存取, 在Release模式下会和Debug模式下的运行效果不相同.
- 运行结果肯定不对了. 是否报错,要看运气了. 如果不报错, 发布后会死的很惨
- */
- int main(int argc, char* argv[]) {
- /// 将函数模板特化的数组大小, 从1填到9来测试
- /// 不能直接在一个作用域内写多个函数模板的实例, 会全被替换成最后一个函数模板的实例
- fnTestDataType<9>();
- printf(""); //< bp
- /** IDE debug 变量内存地址分析
- ============================================================
- char array size = 9
- ------------------------------------------------------------
- &szBuf[0] = 0x0018FEE8
- &szBuf[8] = 0x0018FEF0
- &wTmp = 0x0018FEE4
- &iTmp = 0x0018FEE0
- &lTmp = 0x0018FEDC
- &f = 0x0018FED8
- &dbl = 0x0018FED0
- &szBuf1[0] = 0x0018FEC4
- &szBuf1[8] = 0x0018FECC
- &wTmp1 = 0x0018FEC0
- &iTmp1 = 0x0018FEBC
- &lTmp1 = 0x0018FEB8
- &f1 = 0x0018FEB4
- &dbl1 = 0x0018FEAC
- ============================================================
- char array size = 8
- ------------------------------------------------------------
- &szBuf[0] = 0x0018FEEC
- &szBuf[7] = 0x0018FEF3
- &wTmp = 0x0018FEE8
- &iTmp = 0x0018FEE4
- &lTmp = 0x0018FEE0
- &f = 0x0018FEDC
- &dbl = 0x0018FED4
- &szBuf1[0] = 0x0018FECC
- &szBuf1[7] = 0x0018FED3
- &wTmp1 = 0x0018FEC8
- &iTmp1 = 0x0018FEC4
- &lTmp1 = 0x0018FEC0
- &f1 = 0x0018FEBC
- &dbl1 = 0x0018FEB4
- ============================================================
- char array size = 7
- ------------------------------------------------------------
- &szBuf[0] = 0x0018FEEC
- &szBuf[6] = 0x0018FEF2
- &wTmp = 0x0018FEE8
- &iTmp = 0x0018FEE4
- &lTmp = 0x0018FEE0
- &f = 0x0018FEDC
- &dbl = 0x0018FED4
- &szBuf1[0] = 0x0018FECC
- &szBuf1[6] = 0x0018FED2
- &wTmp1 = 0x0018FEC8
- &iTmp1 = 0x0018FEC4
- &lTmp1 = 0x0018FEC0
- &f1 = 0x0018FEBC
- &dbl1 = 0x0018FEB4
- ============================================================
- char array size = 6
- ------------------------------------------------------------
- &szBuf[0] = 0x0018FEEC
- &szBuf[5] = 0x0018FEF1
- &wTmp = 0x0018FEE8
- &iTmp = 0x0018FEE4
- &lTmp = 0x0018FEE0
- &f = 0x0018FEDC
- &dbl = 0x0018FED4
- &szBuf1[0] = 0x0018FECC
- &szBuf1[5] = 0x0018FED1
- &wTmp1 = 0x0018FEC8
- &iTmp1 = 0x0018FEC4
- &lTmp1 = 0x0018FEC0
- &f1 = 0x0018FEBC
- &dbl1 = 0x0018FEB4
- ============================================================
- char array size = 5
- ------------------------------------------------------------
- &szBuf[0] = 0x0018FEEC
- &szBuf[4] = 0x0018FEF0
- &wTmp = 0x0018FEE8
- &iTmp = 0x0018FEE4
- &lTmp = 0x0018FEE0
- &f = 0x0018FEDC
- &dbl = 0x0018FED4
- &szBuf1[0] = 0x0018FECC
- &szBuf1[4] = 0x0018FED0
- &wTmp1 = 0x0018FEC8
- &iTmp1 = 0x0018FEC4
- &lTmp1 = 0x0018FEC0
- &f1 = 0x0018FEBC
- &dbl1 = 0x0018FEB4
- ============================================================
- char array size = 4
- ------------------------------------------------------------
- &szBuf[0] = 0x0018FEF0
- &szBuf[3] = 0x0018FEF3
- &wTmp = 0x0018FEEC
- &iTmp = 0x0018FEE8
- &lTmp = 0x0018FEE4
- &f = 0x0018FEE0
- &dbl = 0x0018FED8
- &szBuf1[0] = 0x0018FED4
- &szBuf1[3] = 0x0018FED7
- &wTmp1 = 0x0018FED0
- &iTmp1 = 0x0018FECC
- &lTmp1 = 0x0018FEC8
- &f1 = 0x0018FEC4
- &dbl1 = 0x0018FEBC
- ============================================================
- char array size = 3
- ------------------------------------------------------------
- &szBuf[0] = 0x0018FEF0
- &szBuf[2] = 0x0018FEF2
- &wTmp = 0x0018FEEC
- &iTmp = 0x0018FEE8
- &lTmp = 0x0018FEE4
- &f = 0x0018FEE0
- &dbl = 0x0018FED8
- &szBuf1[0] = 0x0018FED4
- &szBuf1[2] = 0x0018FED6
- &wTmp1 = 0x0018FED0
- &iTmp1 = 0x0018FECC
- &lTmp1 = 0x0018FEC8
- &f1 = 0x0018FEC4
- &dbl1 = 0x0018FEBC
- ============================================================
- char array size = 2
- ------------------------------------------------------------
- &szBuf[0] = 0x0018FEF0
- &szBuf[1] = 0x0018FEF1
- &wTmp = 0x0018FEEC
- &iTmp = 0x0018FEE8
- &lTmp = 0x0018FEE4
- &f = 0x0018FEE0
- &dbl = 0x0018FED8
- &szBuf1[0] = 0x0018FED4
- &szBuf1[1] = 0x0018FED5
- &wTmp1 = 0x0018FED0
- &iTmp1 = 0x0018FECC
- &lTmp1 = 0x0018FEC8
- &f1 = 0x0018FEC4
- &dbl1 = 0x0018FEBC
- ============================================================
- char array size = 1
- ------------------------------------------------------------
- &szBuf[0] = 0x0018FEF0
- &szBuf[0] = 0x0018FEF0
- &wTmp = 0x0018FEEC
- &iTmp = 0x0018FEE8
- &lTmp = 0x0018FEE4
- &f = 0x0018FEE0
- &dbl = 0x0018FED8
- &szBuf1[0] = 0x0018FED4
- &szBuf1[0] = 0x0018FED4
- &wTmp1 = 0x0018FED0
- &iTmp1 = 0x0018FECC
- &lTmp1 = 0x0018FEC8
- &f1 = 0x0018FEC4
- &dbl1 = 0x0018FEBC
- */
- printf("END, press any key to quit\n");
- return 0;
- }
http://blog.csdn.net/lostspeed/article/details/50569619
以上是关于DEBUG模式下, 内存中的变量地址分析的主要内容,如果未能解决你的问题,请参考以下文章
visual studio c/c++变量内存地址里的cc是什么意思?
Android 逆向整体加固脱壳 ( DEX 优化流程分析 | dvmDexFileOpenPartial | dexFileParse | 脱壳点 | 获取 dex 文件在内存中的首地址 )(代码片