| 263 | | |
| | 264 | === Detect Uninitialized Data Access === |
| | 265 | |
| | 266 | Example code: (this code has a bug) |
| | 267 | {{{#!c |
| | 268 | #include <stdio.h> |
| | 269 | #include <stdlib.h> |
| | 270 | |
| | 271 | int main() { |
| | 272 | double * p = malloc(sizeof(double) * 10); |
| | 273 | if (p[0] < 1) { |
| | 274 | printf("p[0] < 1\n"); |
| | 275 | } else { |
| | 276 | printf("p[1] >= 1\n"); |
| | 277 | } |
| | 278 | return 0; |
| | 279 | } |
| | 280 | }}} |
| | 281 | |
| | 282 | {{{#!bash |
| | 283 | [fuji@cypress01-089 Valgrind]$ icc uninit.c |
| | 284 | [fuji@cypress01-089 Valgrind]$ ./a.out |
| | 285 | p[0] < 1 |
| | 286 | [fuji@cypress01-089 Valgrind]$ icc -O0 -g uninit.c |
| | 287 | [fuji@cypress01-089 Valgrind]$ valgrind ./a.out |
| | 288 | ==34643== Memcheck, a memory error detector |
| | 289 | ==34643== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al. |
| | 290 | ==34643== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info |
| | 291 | ==34643== Command: ./a.out |
| | 292 | ==34643== |
| | 293 | ==34643== Conditional jump or move depends on uninitialised value(s) |
| | 294 | ==34643== at 0x4005A9: main (uninit.c:6) |
| | 295 | ==34643== |
| | 296 | ==34643== Conditional jump or move depends on uninitialised value(s) |
| | 297 | ==34643== at 0x4005AB: main (uninit.c:6) |
| | 298 | ==34643== |
| | 299 | p[0] < 1 |
| | 300 | ==34643== |
| | 301 | ==34643== HEAP SUMMARY: |
| | 302 | ==34643== in use at exit: 80 bytes in 1 blocks |
| | 303 | ==34643== total heap usage: 1 allocs, 0 frees, 80 bytes allocated |
| | 304 | ==34643== |
| | 305 | ==34643== LEAK SUMMARY: |
| | 306 | ==34643== definitely lost: 80 bytes in 1 blocks |
| | 307 | ==34643== indirectly lost: 0 bytes in 0 blocks |
| | 308 | ==34643== possibly lost: 0 bytes in 0 blocks |
| | 309 | ==34643== still reachable: 0 bytes in 0 blocks |
| | 310 | ==34643== suppressed: 0 bytes in 0 blocks |
| | 311 | ==34643== Rerun with --leak-check=full to see details of leaked memory |
| | 312 | ==34643== |
| | 313 | ==34643== For counts of detected and suppressed errors, rerun with: -v |
| | 314 | ==34643== Use --track-origins=yes to see where uninitialised values come from |
| | 315 | ==34643== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 6 from 6) |
| | 316 | }}} |