What is the “assert” function?

后端 未结 9 2239
耶瑟儿~
耶瑟儿~ 2020-11-27 09:16

I\'ve been studying OpenCV tutorials and came across the assert function; what does it do?

9条回答
  •  悲哀的现实
    2020-11-27 09:38

    In addition, you can use it to check if the dynamic allocation was successful.

    Code example:

    int ** p;
    p = new int * [5];      // Dynamic array (size 5) of pointers to int
    for (int i = 0; i < 5; ++i) {
        p[i] = new int[3]; // Each i(ptr) is now pointing to a dynamic
                           // array (size 3) of actual int values
    }
    
    assert (p);            // Check the dynamic allocation.
    

    Similar to:

    if (p == NULL) {
        cout << "dynamic allocation failed" << endl;
        exit(1);
    }
    

提交回复
热议问题