c++ - increment pointer within if (pointer) condition -


i'm reading c++ code, developer uses kind of pattern:

float *_array;  //... while (expression) {      if (_array) {         // ...         _array += 1;     } else {         // ...     } } 

the outer while loop terminate independently _array points to. question if (_array) condition , incrementation within clause.

i first thought should check if pointer "ran out of" array, not seem case. tested simple snippet:

float *p = new float[5]; int = 0; (i = 0; < 10; i++) {     if (p) {         std::cout << "ok\n";     } else {         std::cout << "no\n";     }     p += 1; } 

this print 10 times "ok". if (pointer) evaluates true if pointer exceeded defined array length.

but else purpose of if (pointer) in context?

its purpose check whether pointer _array pointing null or not, i.e. check if null pointer.

new throws std::bad_alloc exception , therefore no need check null. in case of malloc, calloc, realloc or new(std::nothrow), if allocation fails return null. in such case need check null.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -