Creating C++ objects

I noticed that there are two ways to create C++ objects:

BTree *btree = new BTree;

and

BTree btree;

From what I can tell, the only difference is in how class objects are accessed (. vs. -> operator), and when the first way is used, private integers get initialized to 0.

Which way is better, and what's the difference?

How do you know when to use one or the other?


Two differences:

  • they create the objects in different parts of the memory (heap vs stack)

  • the object lifetime is different: In the first case, the code manages the memory allocation explicitly, and it must also manage the deallocation explicitly (using delete/delete[]).

    In the second case, the object is automatically deallocated at the end of its enclosing scope (either a method, a nested block within a method, or a class)

  • Which one you uses depends mostly on the lifetime of the object (if it should outlive the method in which it is created or not).


    The first form creates the object on the heap whereas the second creates it on the stack.

    The second from will be destroyed when the function is finished running. The first will remain alive until specifically deleted.

    The second form is best if you want to only use the object in the current scope. You don't need to worry about getting rid of it because it'll be done for you. Also note that some libraries don't work if there classes are created on the stack.

    If the object should outlive the function, the new form is a better option.


    Another difference between these two form is the time when storage for those objects are allocated. The form BTree bTree; names static allocation whose allocation accomplishes at compile time -- ie the compiler will arrange memory space for this object in memory when running. While, the allocation for BTree *pbTree = new BTree , the alleged dynamic allocation -- performs at runtime -- ie the momory will be allocated only when the running program reach this point.

    In this situation, the difference between static and dynamic allocation is not apparent. Consider the following circumstance where you need to allocate memory space for an integer array, but number of elements can be determined only at runtime, ie we can only know the exact memory space consumed by the array after the program begin to execute.

    // in this function, we want to return a copy of the parameter array
    int *array_cpy( int *arr, int num ){
        int *copy = new int[ num ];
    
        int i;
        for( i = 0; i < num; i++ ){
            copy[ i ] = arr[ i ];
        }
    
        return copy;
    }
    

    Here the definition int copy[ num ]; is not appropriate, one reason is for what I have stated above, the other is the lifetime of copy outlive the function. However, given VLA is permitted in recent language specification, the second reason is key to this problem.

    链接地址: http://www.djcxy.com/p/13798.html

    上一篇: C ++:是否有必要手动分配内存?

    下一篇: 创建C ++对象