DEBUG模式下, 内存中的变量地址分析

时间:2023-03-08 17:05:46
DEBUG模式下, 内存中的变量地址分析

测试函数的模板实现

  1. /// @file my_template.h
  2. /// @brief 测试数据类型用的模板实现
  3. #ifndef MY_TEMPLATE_H_2016_0123_1226
  4. #define MY_TEMPLATE_H_2016_0123_1226
  5. template<int iArySize>
  6. void fnTestDataType() {
  7. char szBuf[iArySize] = {'\0'};
  8. unsigned short wTmp = 0;
  9. int iTmp = 0;
  10. long lTmp = 0;
  11. float f = .0f;
  12. double dbl = .0;
  13. char szBuf1[iArySize] = {'\0'};
  14. unsigned short wTmp1 = 0;
  15. int iTmp1 = 0;
  16. long lTmp1 = 0;
  17. float f1 = .0f;
  18. double dbl1 = .0;
  19. printf("============================================================\n");
  20. printf("char array size = %d\n", iArySize);
  21. printf("------------------------------------------------------------\n");
  22. printf("&szBuf[0] = \t0x%p\n", &szBuf[0]);
  23. printf("&szBuf[%d] = \t0x%p\n", iArySize - 1, &szBuf[iArySize - 1]);
  24. printf("&wTmp = \t0x%p\n", &wTmp);
  25. printf("&iTmp = \t0x%p\n", &iTmp);
  26. printf("&lTmp = \t0x%p\n", &lTmp);
  27. printf("&f = \t\t0x%p\n", &f);
  28. printf("&dbl = \t\t0x%p\n", &dbl);
  29. printf("&szBuf1[0] = \t0x%p\n", &szBuf1[0]);
  30. printf("&szBuf1[%d] = \t0x%p\n", iArySize - 1, &szBuf1[iArySize - 1]);
  31. printf("&wTmp1 = \t0x%p\n", &wTmp1);
  32. printf("&iTmp1 = \t0x%p\n", &iTmp1);
  33. printf("&lTmp1 = \t0x%p\n", &lTmp1);
  34. printf("&f1 = \t\t0x%p\n", &f1);
  35. printf("&dbl1 = \t0x%p\n\n", &dbl1);
  36. };
  37. #endif // #ifndef MY_TEMPLATE_H_2016_0123_1226

测试代码和实验结论

  1. /// @file main.cpp
  2. /// @brief DEBUG模式下, 内存中的变量地址分析
  3. #include <iostream>
  4. using namespace std;
  5. #include "my_template.h"
  6. /** 试验结论 - <<DEBUG模式下, 内存中的变量地址分析>>
  7. 如果定义多个变量
  8. DEBUG模式下, 内存中的变量地址和定义的顺序相同
  9. 变量开始地址都是模式地址
  10. 先定义的变量在内存高地址
  11. 后定义的变量在内存低地址
  12. Release模式下, 经过优化, 变量的内存地址和变量定义的顺序不相同!
  13. 如果变量有越界访问的情况, Release模式下的越界访问情况未知.
  14. 好像有一些规律, 依赖于编译器的优化选项
  15. 依赖变量越界访问达成变量的存取, 在Release模式下会和Debug模式下的运行效果不相同.
  16. 运行结果肯定不对了. 是否报错,要看运气了. 如果不报错, 发布后会死的很惨
  17. */
  18. int main(int argc, char* argv[]) {
  19. /// 将函数模板特化的数组大小, 从1填到9来测试
  20. /// 不能直接在一个作用域内写多个函数模板的实例, 会全被替换成最后一个函数模板的实例
  21. fnTestDataType<9>();
  22. printf(""); //< bp
  23. /** IDE debug 变量内存地址分析
  24. ============================================================
  25. char array size = 9
  26. ------------------------------------------------------------
  27. &szBuf[0] =     0x0018FEE8
  28. &szBuf[8] =     0x0018FEF0
  29. &wTmp =         0x0018FEE4
  30. &iTmp =         0x0018FEE0
  31. &lTmp =         0x0018FEDC
  32. &f =            0x0018FED8
  33. &dbl =          0x0018FED0
  34. &szBuf1[0] =    0x0018FEC4
  35. &szBuf1[8] =    0x0018FECC
  36. &wTmp1 =        0x0018FEC0
  37. &iTmp1 =        0x0018FEBC
  38. &lTmp1 =        0x0018FEB8
  39. &f1 =           0x0018FEB4
  40. &dbl1 =         0x0018FEAC
  41. ============================================================
  42. char array size = 8
  43. ------------------------------------------------------------
  44. &szBuf[0] =     0x0018FEEC
  45. &szBuf[7] =     0x0018FEF3
  46. &wTmp =         0x0018FEE8
  47. &iTmp =         0x0018FEE4
  48. &lTmp =         0x0018FEE0
  49. &f =            0x0018FEDC
  50. &dbl =          0x0018FED4
  51. &szBuf1[0] =    0x0018FECC
  52. &szBuf1[7] =    0x0018FED3
  53. &wTmp1 =        0x0018FEC8
  54. &iTmp1 =        0x0018FEC4
  55. &lTmp1 =        0x0018FEC0
  56. &f1 =           0x0018FEBC
  57. &dbl1 =         0x0018FEB4
  58. ============================================================
  59. char array size = 7
  60. ------------------------------------------------------------
  61. &szBuf[0] =     0x0018FEEC
  62. &szBuf[6] =     0x0018FEF2
  63. &wTmp =         0x0018FEE8
  64. &iTmp =         0x0018FEE4
  65. &lTmp =         0x0018FEE0
  66. &f =            0x0018FEDC
  67. &dbl =          0x0018FED4
  68. &szBuf1[0] =    0x0018FECC
  69. &szBuf1[6] =    0x0018FED2
  70. &wTmp1 =        0x0018FEC8
  71. &iTmp1 =        0x0018FEC4
  72. &lTmp1 =        0x0018FEC0
  73. &f1 =           0x0018FEBC
  74. &dbl1 =         0x0018FEB4
  75. ============================================================
  76. char array size = 6
  77. ------------------------------------------------------------
  78. &szBuf[0] =     0x0018FEEC
  79. &szBuf[5] =     0x0018FEF1
  80. &wTmp =         0x0018FEE8
  81. &iTmp =         0x0018FEE4
  82. &lTmp =         0x0018FEE0
  83. &f =            0x0018FEDC
  84. &dbl =          0x0018FED4
  85. &szBuf1[0] =    0x0018FECC
  86. &szBuf1[5] =    0x0018FED1
  87. &wTmp1 =        0x0018FEC8
  88. &iTmp1 =        0x0018FEC4
  89. &lTmp1 =        0x0018FEC0
  90. &f1 =           0x0018FEBC
  91. &dbl1 =         0x0018FEB4
  92. ============================================================
  93. char array size = 5
  94. ------------------------------------------------------------
  95. &szBuf[0] =     0x0018FEEC
  96. &szBuf[4] =     0x0018FEF0
  97. &wTmp =         0x0018FEE8
  98. &iTmp =         0x0018FEE4
  99. &lTmp =         0x0018FEE0
  100. &f =            0x0018FEDC
  101. &dbl =          0x0018FED4
  102. &szBuf1[0] =    0x0018FECC
  103. &szBuf1[4] =    0x0018FED0
  104. &wTmp1 =        0x0018FEC8
  105. &iTmp1 =        0x0018FEC4
  106. &lTmp1 =        0x0018FEC0
  107. &f1 =           0x0018FEBC
  108. &dbl1 =         0x0018FEB4
  109. ============================================================
  110. char array size = 4
  111. ------------------------------------------------------------
  112. &szBuf[0] =     0x0018FEF0
  113. &szBuf[3] =     0x0018FEF3
  114. &wTmp =         0x0018FEEC
  115. &iTmp =         0x0018FEE8
  116. &lTmp =         0x0018FEE4
  117. &f =            0x0018FEE0
  118. &dbl =          0x0018FED8
  119. &szBuf1[0] =    0x0018FED4
  120. &szBuf1[3] =    0x0018FED7
  121. &wTmp1 =        0x0018FED0
  122. &iTmp1 =        0x0018FECC
  123. &lTmp1 =        0x0018FEC8
  124. &f1 =           0x0018FEC4
  125. &dbl1 =         0x0018FEBC
  126. ============================================================
  127. char array size = 3
  128. ------------------------------------------------------------
  129. &szBuf[0] =     0x0018FEF0
  130. &szBuf[2] =     0x0018FEF2
  131. &wTmp =         0x0018FEEC
  132. &iTmp =         0x0018FEE8
  133. &lTmp =         0x0018FEE4
  134. &f =            0x0018FEE0
  135. &dbl =          0x0018FED8
  136. &szBuf1[0] =    0x0018FED4
  137. &szBuf1[2] =    0x0018FED6
  138. &wTmp1 =        0x0018FED0
  139. &iTmp1 =        0x0018FECC
  140. &lTmp1 =        0x0018FEC8
  141. &f1 =           0x0018FEC4
  142. &dbl1 =         0x0018FEBC
  143. ============================================================
  144. char array size = 2
  145. ------------------------------------------------------------
  146. &szBuf[0] =     0x0018FEF0
  147. &szBuf[1] =     0x0018FEF1
  148. &wTmp =         0x0018FEEC
  149. &iTmp =         0x0018FEE8
  150. &lTmp =         0x0018FEE4
  151. &f =            0x0018FEE0
  152. &dbl =          0x0018FED8
  153. &szBuf1[0] =    0x0018FED4
  154. &szBuf1[1] =    0x0018FED5
  155. &wTmp1 =        0x0018FED0
  156. &iTmp1 =        0x0018FECC
  157. &lTmp1 =        0x0018FEC8
  158. &f1 =           0x0018FEC4
  159. &dbl1 =         0x0018FEBC
  160. ============================================================
  161. char array size = 1
  162. ------------------------------------------------------------
  163. &szBuf[0] =     0x0018FEF0
  164. &szBuf[0] =     0x0018FEF0
  165. &wTmp =         0x0018FEEC
  166. &iTmp =         0x0018FEE8
  167. &lTmp =         0x0018FEE4
  168. &f =            0x0018FEE0
  169. &dbl =          0x0018FED8
  170. &szBuf1[0] =    0x0018FED4
  171. &szBuf1[0] =    0x0018FED4
  172. &wTmp1 =        0x0018FED0
  173. &iTmp1 =        0x0018FECC
  174. &lTmp1 =        0x0018FEC8
  175. &f1 =           0x0018FEC4
  176. &dbl1 =         0x0018FEBC
  177. */
  178. printf("END, press any key to quit\n");
  179. return 0;
  180. }

http://blog.csdn.net/lostspeed/article/details/50569619